Method call: Recursive

1, static methods and non-static methods
distinguish static methods and non-static methods are:
Static methods are static; there is no non-static method.
Such as: static methods:

public class students {
    //静态方法
    public static void say(){
        System.out.println("学生说话了");
    }
}

Call the static method:

public static void main(String[] args) {
        students.say();
    }

Non-static method:

public class Student {
    //非静态方法
    public void say(){
        System.out.println("学生说话了");
    }

Non-static method call:

public static void main(String[] args) {
        //实例化这个类 new
        //对象类型 对象名=对象值
        //
        Student student=new Student();

        student.say();
    }

Are non-static method or static method, the method can be called a direct method b:

public void a(){
        b();
    }
    public void b(){

    }

If a static, b is not, not:

 //和类一起加载的
    public static void a(){
        b();
    }
    //类实例化之后才存在
    public void b(){

    }

2, the argument between Form

public class Demo03 {

    public static void main(String[] args) {

        //实际参数和形式参数的类型要对应;
        int add = new Demo03().add(1, 2);
        System.out.println(add);

    }

    public int add(int a,int b){
        return a+b;

    }


}

a, b parameter representation, the parameters 1 and 2 show the actual;

3, the reference value is passed passed
passed by value, the end result is still 1,1

//值传递
public class Demo04 {
    public static void main(String[] args) {
        int a = 1 ;
        System.out.println(a);
        Demo04.change(a);
        System.out.println(a);
    }
    //返回值为空
    public static void change(int a){
        a = 10;
    }
}

Passed by reference, the final out of the result is null, Mike;

public class Demo05 {
    public static void main(String[] args) {
        Cat cat=new Cat();
        System.out.println(cat.name);
        Demo05.change(cat);
        System.out.println(cat.name);
    }
    public static void change(Cat cat){
        //cat是一个对象,指向的是Cat cat=new Cat();这是一个具体的猫可以改变属性
        cat.name = "小李";
    }
}
//定义了一个类Cat,有一个属性:name
class Cat{
    String name;
}

Released eight original articles · won praise 0 · Views 144

Guess you like

Origin blog.csdn.net/leelelea/article/details/104187641