- Student类:
package com.zws04;
/*
* 构造方法的使用:
*/
public class Student {
private String name;
private int age;
public Student(){
}
public Student(int age) {
this.age = age;
}
public Student(String name) {
this.name = name;
}
public Student(String name,int age) {
this.name = name;
this.age = age;
}
public void show() {
System.out.println("姓名是:"+name+",年龄是:"+age);
System.out.println("-------------------");
}
}
- StudentDemo类:
package com.zws04;
public class StudentDemo {
public static void main(String[] args) {
Student stu = new Student();
stu.show();
Student stu1 = new Student(28);
stu1.show();
Student stu2 = new Student("张大壮");
stu2.show();
Student stu3 = new Student("张大壮",28);
stu3.show();
}
}
结果:
姓名是:null,年龄是:0
-------------------
姓名是:null,年龄是:28
-------------------
姓名是:张大壮,年龄是:0
-------------------
姓名是:张大壮,年龄是:28
-------------------