03 回顾方法的调用

回顾方法的调用

image

package com.zhan.base05Oop;

public class Test03 {
    // 回顾方法的调用
    public static void main(String[] args) {

        // 静态方法 static
        Test03_Student.say1(); // 类名.方法名();



        // 非静态方法,没有static
        // 实例化这个类 new (就是实例化这个类,创建这个类对应的具体的对象)
        // 对象类型     对象名   = 对象值
        Test03_Student student = new Test03_Student();
        // new Test03_Student(); 一般不这么用,而是上面的
        student.say2();



    }
}
package com.zhan.base05Oop;

public class Test03_Student {

    // 静态方法     和类一起加载的
    public static void say1(){
        System.out.println("这是静态方法");
        // 静态方法     和类一起加载的
        // 非静态方法    类实例化后才存在
        // 因此,static方法say1() 不能调用 非静态方法 say2();

    }

    // 非静态方法    类实例化后才存在
    public void say2(){
        System.out.println("这是非静态方法");

    }
}
package com.zhan.base05Oop;

public class Test03_01 {
    // java是值传递
    // 引用传递,传递一个对象,本质还是值传递
    public static void main(String[] args) {   // 一个类中只能有一个 public class
        int a=1;
        System.out.println(a);
        change(a);
        System.out.println(a);
    }

    public static void change(int a){
        a=10;
    }



}
package com.zhan.base05Oop;

public class Test03_02 {
    // 引用传递,传递一个对象,本质还是值传递
    // 对象!内存!
    public static void main(String[] args) {    // 一个类中只能有一个 public class
        Person person = new Person();
        System.out.println(person.name); // null
        change(person);
        System.out.println(person.name);  // 詹建海

    }

    public static void change(Person person){   // 这也是方法 (参数类型 参数名字)
        person.name="詹建海";   // 指向的是对象person,而不是形参
    }

}

// 定义一个Person类。有一个属性:name
class Person{   // 一个类中可以有无数个 class
    String name; // null
}
posted @ 2023-02-04 18:37  被占用的小海海  阅读(15)  评论(0编辑  收藏  举报