11月22日Java学习

类与对象的关系

类是一种抽象的数据类型,它是对某一类事物整体描述/定义,但是并不能代表一个具体的事物。

  • 动物,植物,手机...

  • person类,Pet类,Car类

对象是抽象概念的具体实例

  • 张三就是人的一个具体的实例,张三家的狗就是狗的一个具体实例

  • 能够体现出特点,展现出功能的是具体的实例,而不是一个抽象的概念。


public class Demo02 {
   public static void main(String[] args) {
       //方法的调用
      //对象类型 对象名   对象值;
       Student student = new Student();//实例化这个类
       student.say();

  }

}

public class Student {
   //静态方法,直接调用Student.say

   //非静态方法,要实例化这个类 new.
   public void say(){
       System.out.println("Violet说话了");
  }
   //和类一起加载的
   public static void a(){
       //b();要二者等同才能直接调用。
       c();
  }
   //类实例化之后才有的
   public void b() {
  }
   public static void c(){

  }


}


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

       System.out.println(a);//1

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


//引用传递: 对象,本质还是值传递
public class Demo05 {
   public static void main(String[] args) {
       person person = new person();
       System.out.println(person.name);
       change(person);
       System.out.println(person.name);
  }

   public  static void change(person person) {
       //person是一个对象,指向的是一个具体的 person person = new person();
       person.name = "Violet";
  }
}
 //定义了一个类person,有name这个属性。
class person{
   String name;
}

 

posted @ 2021-11-22 22:02  Violetjf  阅读(99)  评论(0)    收藏  举报