1.重载
重载(Overloading)是一种面向对象编程中的特性,允许在同一个作用域中定义多个同名但参数列表不同的方法或函数。重载可以使代码更具可读性和可维护性。它通常分为方法重载和运算符重载两种。
class MathOperations {
// 方法1:无参数
public void display() {
System.out.println("Display with no arguments");
}
// 方法2:一个参数
public void display(int num) {
System.out.println("Display with one argument: " + num);
}
// 方法3:两个参数
public void display(int num1, int num2) {
System.out.println("Display with two arguments: " + num1 + " and " + num2);
}
}
public class Main {
public static void main(String[] args) {
MathOperations obj = new MathOperations();
obj.display();
obj.display(10);
obj.display(10, 20);
}
}
2.构造器
没有返回值,利用构造器初始化对象(构造的时候对象已经存在了,后面只是在初始化)。
构造器是类中的一种特殊方法,用于初始化对象。每当创建一个对象时,构造器就会被自动调用,
用来为新对象分配初始值。构造器的名称必须与类名相同,并且没有返回类型(甚至不能声明为 void)。Java 支持两种类型的构造器:默认构造器和参数化构造器。
1. 无参构造器
class Person {
String name;
int age;
// 无参构造器
public Person() {
name = "Unknown";
age = 0;
System.out.println("无参构造器被调用");
}
public void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Person person1 = new Person(); // 调用无参构造器
person1.display();
}
}
2. 参数化构造器
class Person {
String name;
int age;
// 参数化构造器
public Person(String name, int age) {
this.name = name;
this.age = age;
System.out.println("参数化构造器被调用");
}
public void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Person person2 = new Person("Alice", 25); // 调用参数化构造器
person2.display();
}
}
在调用构造器前对象已经创建好了,实例化的对象的属性的内存大小也已经分配,接下来系统会自己调用构造器(可以自己定义,也可以使用系统的默认构造器),根据构造器初始话参数。
3.构造器重载
构造器说到底也是一个函数,也是可以重载的。
class Person {
String name;
int age;
// 无参构造器
public Person() {
name = "Unknown";
age = 0;
}
// 参数化构造器1
public Person(String name) {
this.name = name;
this.age = 0;
}
// 参数化构造器2
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Person person1 = new Person(); // 调用无参构造器
person1.display();
Person person2 = new Person("Bob"); // 调用带一个参数的构造器
person2.display();
Person person3 = new Person("Charlie", 30); // 调用带两个参数的构造器
person3.display();
}
}
3.加入构造器后一个对象生成的过程
Person p = new Person("小倩",20);
1.在方法区加载person类
2.在堆中创建对象,分配内存存储 age 和 name。先是赋予默认值 age = 0 ,name = null。
然后根据代码赋值 age = 90,name = null。由于字符串是引用数据类型其堆地址内只是一个地址指向常量池保存引用数据类型字符串。
3.系统启动构造器初始化,age和name的初值再次变化。age = 20,name = 小倩
4.然后将对象的地址赋值给栈里面的p,这里p保存的只是对象的地址。
5.person P2= p;这里相当于将p的地址赋值给P2.P2也指向这个对象。
///
4)this的引入
this的存在是为了确保能够准确地引用和操作对象的属性和方法。
那个对象调用this就代表那个对象,每个对象都有自己隐藏的this指向对象自己。
class Person {
String name;
int age;
//构造器也可以重载
public Person(String pname) {
name = pname; // 初始化名字
}
// 带名字和年龄的构造器
public Person(String pname, int page) {
name = pname; // 初始化名字
age = page; // 初始化年龄
}
class Person {
String name;
int age;
//构造器也可以重载
public Person(String name) {
this.name = name; // 初始化名字
}
// 带名字和年龄的构造器
public Person(String name, int age) {
this.name = name; // 初始化名字
this.age = age; // 初始化年龄
}
上面两段代码都能实现构造器功能,但是使用this可以更好的区分对象属性和函数输入的形参,使得代码可读性和可维护性大大提高。
this优点:在第二种实现中,this关键字用来明确区分构造器参数和类的属性。例如,this.name 表示类的属性 name,而 name 是构造器的参数。
this指向这个对象的地址,代表自己当前的地址,this.name 就是这个对象的属性,而 name就是函数输入的参数。
this引用使用 this 能够避免潜在的歧义,使代码更具可读性和可维护性。