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();
   

 

Posted on 2022-05-19 23:42  LutixiaGit  阅读(54)  评论(0编辑  收藏  举报