类与对象的创建

 

 1 package com.oop.demo2;
 2 //一个项目应该只存一个main方法
 3 public class Application {
 4     public static void main(String[] args) {
 5         //类:抽象的,需要实例化
 6         //类实例化后会返回一个自己的对象!
 7         //student对象就是要给Student类的具体实例!
 8 
 9         Student xiaoming = new Student();
10         Student xh = new Student();
11 
12         xiaoming.name="小明";
13         xiaoming.age=3;
14         System.out.println(xiaoming.name);
15         System.out.println(xiaoming.age);
16 
17         xh.name="小红";
18         xh.age=3;
19         System.out.println(xh.name);
20         System.out.println(xh.age);
21     }
22 }
 1 package com.oop.demo2;
 2 //学生类
 3 public class Student {
 4     //属性:字段
 5     String name;//null
 6     int age;//0
 7 
 8     //方法
 9     public void study(){
10         System.out.println(this.name+"在学习");
11     }
12 }

 

posted @ 2021-02-17 11:32  奔啵儿灞  阅读(76)  评论(0)    收藏  举报