1 /* 2 标准代码 3 4 学生类: 5 成员变量:name,age 6 构造方法:无参,带两个参 7 成员方法:getXxx()/setXxx(),show 8 9 给成员变量赋值:10 ①setXxx()灵活性高11 ②构造方法。12 13 输出成员变量值:14 ①getXxx()灵活性高15 ②通过调用show()方法。16 */17 18 class Student{19 private String name;20 private int age;21 22 public Student(){23 24 }25 26 public Student(String name,int age){27 this.name = name;28 this.age = age;29 30 }31 32 public String getName(){33 return name;34 }35 36 public int getAge(){37 return age;38 }39 40 public void setName(String name){41 this.name = name;42 }43 44 public void setAge(int age){45 this.age = age;46 }47 48 public void show(){49 System.out.println(name+"----"+age);50 }51 }52 53 class StudentTest2{54 public static void main(String[] args){55 //无参构造+setXxx ①+①56 Student s1 = new Student();57 s1.setName("小懒");58 s1.setAge(99);59 System.out.println(s1.getName()+"---"+s1.getAge());60 //①+②61 s1.show();62 63 //构造方法给值 ②+①64 Student s2 = new Student("小皮",99);65 System.out.println(s2.getName()+"---"+s2.getAge());66 //②+②67 s2.show();68 }69 }