package CAK1; public class Student { private String name; private int age; public Student(){ System.out.println("无参构造方法"); } public void show(){ System.out.println(name +"--- age :" + age); } }
package CAK1; public class StudentDemo { public static void main(String[] args) { Student stu = new Student(); stu.show(); } }
带参数的构造方法
package CAK1; public class Student { private String name; private int age; public Student(String name){ //System.out.println("无参构造方法"); this.name = name; } public void show(){ System.out.println(name +"--- age :" + age); } }
package CAK1; public class StudentDemo { public static void main(String[] args) { Student stu = new Student("XT"); stu.show();