今天我们来一起了解 JAVA 对象中的构造器和方法重载。
JAVA 对象通过构造器生成,假如一个对象没有指定构造器,则程序默认提供无参构造器,构造器方法必须为 public 且不能有返回值,方法名必须与类名一致。
public class Student {
String name;
String age;
/**
* 默认构造方法,在未指定构造方法时,程序默认提供
*/
public Student() {
}
public static void main(String[] args) {
Student student = new Student();
}
}
若自己指定了其他的构造器,则默认的无参构造器会失效。
public class Student {
String name;
String age;
public Student(String name, String age) {
this.name = name;
this.age = age;
}
}

如果想无参构造器与其他有参构造器同时生效,声明多个构造器即可。
public class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String name) {
this.name = name;
}
public Student(int age) {
this.age = age;
}
public Student() {
}
}

从上面的例子,我们还可以看到,构造器 Student 可以拥有多个同名的构造方法,这被称为“方法重载”。
方法重载必须满足以下几个条件:
1.重载方法的名称必须一致
2.重载方法的参数个数不同或者参数个数相同但同位置参数类型必须不同
3.不同的返回值不能作为重载方法的评判标准(因为返回值并不是调用方法的必要条件,编译器很难区分,我们经常只管调用,不关心方法的返回值,而且这样的代码可读性较差,容易误导维护者)
反面示例:
public String getInfo() {
String str = "info";
return str;
}
public int getInfo() {
int number = 10;
return number;
}
public static void main(String[] args) {
getInfo();
}
假设允许通过返回值来区分方法重载,在不使用返回值的时候,编译器很难知道 getInfo() 该具体调用哪个方法,故这类方案是行不通的。
在使用方法重载的过程中,有两类特殊情况:
(1)当传入的参数比方法规定的参数小,传入的参数类型会自动被提升为长度最接近的类型(char 类型较为特殊,会被直接提示为 int 类型)
public static void getInfo(int x) {
System.out.println("int======" + x);
}
public static void getInfo(long x) {
System.out.println("long======" + x);
}
public static void main(String[] args) {
short x1 = 10;
getInfo(x1);
}
int======10
(2)当传入的参数比方法规定的参数大,传入的参数类型必须手动进行窄化转换,否则编译无法通过
public static void getInfo(byte x) {
System.out.println("byte======" + x);
}
public static void getInfo(short x) {
System.out.println("short======" + x);
}
public static void getInfo(int x) {
System.out.println("int======" + x);
}
public static void main(String[] args) {
long x = 10L;
getInfo((int) x);
getInfo((short) x);
getInfo((byte) x);
}
int======10
short======10
byte======10
回到构造器的例子,构造器方法里出现了 this 关键字,这里 this 表示对当前对象的引用,只能在方法内部使用。当传入的变量名和类自身的变量名同名时,可以通过 this.变量名 表示类自身的变量,避免歧义。
public class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
此外 this 还可以用于将对自身对象的引用传递给其他方法,包括传递给自己调用。
(1)this 传递自身引用给其他方法
public class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student getStudent() {
return this;
}
}
public class Teacher {
String name;
public Teacher(String name) {
this.name = name;
}
public void printStudent(Student student) {
System.out.println(name + "的学生是" + student.name);
}
}
public static void main(String[] args) {
Student student = new Student("小明", 12);
Teacher teacher = new Teacher("张老师");
teacher.printStudent(student.getStudent());
}
张老师的学生是小明
(2) this 传递引用回自身
public class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student plusAge(Student student, int plusAge) {
student.age = student.age + plusAge;
System.out.println("age===="+student.age);
return this;
}
}
public static void main(String[] args) {
Student student = new Student("小明", 12);
student.plusAge(student, 1).plusAge(student, 2).plusAge(student, 3);
}
age====13
age====15
age====18
此外,this() 还能表示构造器,一个构造器内可以调用其他构造器,但却不能在一个构造器中调用2个构造器,而且 this() 必须放在方法内的首行,否则编译无法通过。
public class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String name) {
this.name = name;
}
public Student(int age) {
this("小王");
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public static void main(String[] args) {
Student student = new Student(12);
System.out.println(student.toString());
}
Student{name='小王', age=12}
本次分享至此结束,希望本文对你有所帮助,若能点亮下方的点赞按钮,在下感激不尽,谢谢您的【精神支持】。
若有任何疑问,也欢迎与我交流,若存在不足之处,也欢迎各位指正!
本文详细讲解了JAVA对象中的构造器,包括默认构造器、有参构造器及方法重载的规则,展示了如何声明和使用多个构造器,并强调了this关键字的作用。同时,介绍了构造器重载的条件和特殊情况,如类型提升与窄化转换。
16万+

被折叠的 条评论
为什么被折叠?



