11月23日Java学习

创建与初始化对象

  • 使用new关键字创建对象

  • 使用new关键字创建的时候,除了分配内存空间之外,还会给创建好的对象进行默认的初始化以及对类中构造器的调用。

//学生类
public class Student {
   //属性
       String name ;
       int age;

       public  void study() {
           System.out.println(this.name+"正在学习");
      }

}

//一个项目只有一个main方法

public class Application {
   public static void main(String[] args) {
       //类:抽象的,实例化
       //类实例化过后会返回一个自己的对象!
       //student对象是一个Student类的具体实例!
       Student xiaoMing = new Student();
       Student xh = new Student();

       xiaoMing.name = "小明";
       xiaoMing.age = 3;

       System.out.println(xiaoMing.age);
       System.out.println(xiaoMing.name);

       xh.age = 3;
       xh.name = "小红";
       System.out.println(xh.name);
       System.out.println(xh.age);

  }
}

 

posted @ 2021-11-23 22:59  Violetjf  阅读(13)  评论(0)    收藏  举报