1.对象的属性全部公共
public class Student { //这个类叫Student //一个类,必须由属性,构造器(,有参数,无返回值int/void,且须和类名一样)构成,方法可有可无 public int id; int age; public Student(){} //构造器,无返回值,0个参数 public Student(int id){ //构造器,无返回值,1个参数 this.age = id; //this代表当前类的对象 this.id = id; } public Student(int id ,int age){ //构造器,无返回值,2个参数 this.id = id; this.age = age; this.id = id; } }
这里我们建立三个具体实例,然后传递0个参数,一个参数,两个参数。
Student zhouhuahao = new Student(); //0个参数 Student jiazhengyang = new Student(2); //1个参数 Student liutao = new Student(3,16); //2个参数
System.out.println("周华浩的id:"+zhouhuahao.id+",年龄:"+zhouhuahao.age); System.out.println("贾正阳的id:"+jiazhengyang.id+",年龄:"+jiazhengyang.age); System.out.println("刘涛的id:&#