当一个类中,有多个同名的方法、但方法签名不同时,称为“方法重载”。:
下面一个方法重载的例子:
class Student{
//成员变量
private String stuId;
private String stuName;
private int stuAge;
//构造器(构造方法)
Student(){
System.out.println("无参构造器");
}
Student(String stuId){
this.stuId = stuId;
System.out.println("1个参数构造器");
}
Student(String stuId, String stuName){
this.stuId = stuId;
this.stuName = stuName;
System.out.println("2个参数构造器");
}
Student(String stuId, String stuName, int stuAge){
this.stuId = stuId;
this.stuName = stuName;
this.stuAge = stuAge;
System.out.println("3个参数构造器");
}
//读写方法:get和set
//。。。。
}
public class Demo7{
public static void main(String[] args){
Student stu = new Student();
Student stu01 = new Student("111");
Student stu02 = new Student("222","张三");
Student stu03 = new Student("333","李四",18);
}
}
//成员变量
private String stuId;
private String stuName;
private int stuAge;
//构造器(构造方法)
Student(){
System.out.println("无参构造器");
}
Student(String stuId){
this.stuId = stuId;
System.out.println("1个参数构造器");
}
Student(String stuId, String stuName){
this.stuId = stuId;
this.stuName = stuName;
System.out.println("2个参数构造器");
}
Student(String stuId, String stuName, int stuAge){
this.stuId = stuId;
this.stuName = stuName;
this.stuAge = stuAge;
System.out.println("3个参数构造器");
}
//读写方法:get和set
//。。。。
}
public class Demo7{
public static void main(String[] args){
Student stu = new Student();
Student stu01 = new Student("111");
Student stu02 = new Student("222","张三");
Student stu03 = new Student("333","李四",18);
}
}