如何将静态方法转换为非静态方法.与java oop相关的小解释/示例

lan*_*ape 1 java dynamic

我不知道如何使用/创建oop代码而不使用静态字.我阅读了Sun教程,有书和例子.我知道有构造函数,然后"指针"这个等我可以使用return语句创建一些简单的非静态方法.真正的问题是,我只是不明白它是如何工作的.我希望有些沟通能让我继续前进.如果有人问,这不是功课.我只是想学习如何编码.以下代码是静态方法和一些非常基本的算法.我想知道如何使用逻辑步骤将其更改为非静态代码(请.).第二个代码显示了一些我可以编写但不完全理解的非静态代码,也不会将其用作模板来重写第一个代码.提前感谢任何提示.

    import java.util.Scanner;

/**
 *
 * @author
 */

public class NumberArray2{

    public static int[] table() {
        Scanner Scan = new Scanner(System.in);
        System.out.println("How many numbers?");
        int s = Scan.nextInt();
        int[] tab = new int[s];

        System.out.println("Write a numbers: ");
        for(int i=0; i<tab.length; i++){
           tab[i] = Scan.nextInt();
        }
        System.out.println("");
        return tab;
    }

    static public void output(int [] tab){
        for(int i=0; i<tab.length; i++){
            if(tab[i] != 0)
                System.out.println(tab[i]);
        }

    }

    static public void max(int [] tab){
       int maxNum = 0;
        for(int i=0; i<tab.length; i++){
            if(tab[i] > maxNum)
                maxNum = tab[i];

        }
    //return maxNum;
       System.out.println(maxNum);
    }

    static public void divide(int [] tab){
        for(int i=0; i<tab.length; i++){
            if((tab[i] % 3 == 0) && tab[i] != 0)
                System.out.println(tab[i]);
        }
    }

    static public void average(int [] tab){
          int sum = 0;
          for(int i=0; i<tab.length; i++)
            sum = sum + tab[i];
            int avervalue = sum/tab.length;
            System.out.println(avervalue);

}




        public static void isPrime(int[] tab) {
        for (int i = 0; i < tab.length; i++) {
            if (isPrimeNum(tab[i])) {
                System.out.println(tab[i]);
            }
        }


    }

    public static boolean isPrimeNum(int n) {
        boolean prime = true;
        for (long i = 3; i <= Math.sqrt(n); i += 2) {
            if (n % i == 0) {
                prime = false;
                break;
            }
        }
        if ((n % 2 != 0 && prime && n > 2) || n == 2) {
            return true;

        } else {
            return false;
        }
    }




    public static void main(String[] args) {
        int[] inputTable = table();

        //int s = table();


        System.out.println("Written numbers:");
        output(inputTable);
        System.out.println("Largest number: ");
        max(inputTable);
        System.out.println("All numbers that can be divided by three: ");
        divide(inputTable);
        System.out.println("Average value: ");
        average(inputTable);
        System.out.println("Prime numbers: ");
        isPrime(inputTable);


    }
}
Run Code Online (Sandbox Code Playgroud)

第二个代码

public class Complex {
// datové složky

    public double re;
    public double im;
// konstruktory

    public Complex() {
    }

    public Complex(double r) {
        this(r, 0.0);
    }

    public Complex(double r, double i) {
        re = r;
        im = i;
    }


    public double abs() {
        return Math.sqrt(re * re + im * im);
    }

    public Complex plus(Complex c) {
        return new Complex(re + c.re, im + c.im);
    }

    public Complex minus(Complex c) {
        return new Complex(re - c.re, im - c.im);
    }


    public String toString() {
        return "[" + re + ", " + im + "]";
    }
}
Run Code Online (Sandbox Code Playgroud)

Tof*_*eer 5

让我们从一个简单的例子开始:

public class Main
{
    public static void main(final String[] argv)
    {
        final Person personA;
        final Person personB;

        personA = new Person("John", "Doe");
        personB = new Person("Jane", "Doe");

        System.out.println(personA.getFullName());
        System.out.println(personB.getFullName());
    }
}

class Person
{
    private final String firstName;
    private final String lastName;

    public Person(final String fName,
                  final String lName)
    {
        firstName = fName;
        lastName  = lName;
    }

    public String getFullName()
    {
        return (lastName + ", " + firstName);
    }
}
Run Code Online (Sandbox Code Playgroud)

我现在要对getFullName方法做一个小改动:

public String getFullName()
{
    return (this.lastName + ", " + this.firstName);
}
Run Code Online (Sandbox Code Playgroud)

注意"这个".我现在用的.

问题是"这个"来自哪里?它没有在任何地方被声明为变量 - 所以它就像魔术一样.事实证明,"this"是每个实例方法的隐藏参数(实例方法是一种非静态方法).你基本上可以认为编译器接受你的代码并像这样重写它(实际上这不是发生的事情 - 但我希望编译代码):

public class Main
{
    public static void main(final String[] argv)
    {
        final Person personA;
        final Person personB;

        personA = new Person("John", "Doe");
        personB = new Person("Jane", "Doe");

        System.out.println(Person.getFullName(personA));
        System.out.println(Person.getFullName(personB));
    }
}

class Person
{
    private final String firstName;
    private final String lastName;

    public Person(final String fName,
                  final String lName)
    {
        firstName = fName;
        lastName  = lName;
    }

    public static String getFullName(final Person thisx)
    {
        return (thisx.lastName + ", " + thisx.firstName);
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,当您查看代码时,请记住实例方法有一个隐藏参数,告诉它变量属于哪个实际对象.

希望这能让你朝着正确的方向前进,如果是这样的话,你可以尝试使用对象重写第一堂课 - 如果你遇到了你尝试过的东西,如果你完成了所有的方式发布它,我相信我们会帮助你看看你是否做对了.