1 package pack1; 2 public class Student { 3 private String name; 4 private int age; 5 6 //构造方法 7 //如果没有任何构造函数,系统会自动给一个无参构造函数 8 public Student(){ 9 this.name=""; 10 this.age=0; 11 } 12 public Student(String name,int age){ 13 this.name=name; 14 this.age=age; 15 } 16 17 public void show(){ 18 System.out.println(name+","+age); 19 } 20 }
package pack1; //测试学生类 public class StudentDemo { public static void main(String[] args) { // 创建对象 Student s1=new Student(); Student s2=new Student("njt",22); s1.show(); // ,0 s2.show(); // njt,22 } }