this和super对比(Java)
一. super和this
指向对象:
this:当前对象实例
super:父类对象实例
用途对比:
this:1.访问当前对象类
2.调用当前类楼早方法
3.返回当前对象实例
super:
1.访问父类对象
2.调用父类构造方法
3.返回父类对象实例
代码详解:
this
public class ThisExample {
private String name;
private int age;
// 1. 解决局部变量与成员变量同名的问题
public ThisExample(String name, int age) {
this.name = name; // this.name 表示成员变量,name 表示参数
this.age = age; // this.age 表示成员变量,age 表示参数
}
// 2. 在方法中返回当前对象(用于链式调用)
public ThisExample setName(String name) {
this.name = name;
return this; // 返回当前对象实例
}
public ThisExample setAge(int age) {
this.age = age;
return this; // 返回当前对象实例
}
// 3. 在方法中传递当前对象
public void printInfo() {
printObject(this); // 将当前对象作为参数传递
}
private void printObject(ThisExample obj) {
System.out.println("Name: " + obj.name + ", Age: " + obj.age);
}
}
public class ConstructorChaining {
private String name;
private int age;
private String address;
// 1. 无参构造方法
public ConstructorChaining() {
this("Unknown", 0); // 调用双参构造方法
System.out.println("无参构造方法被调用");
}
// 2. 双参构造方法
public ConstructorChaining(String name, int age) {
this(name, age, "Unknown Address"); // 调用三参构造方法
System.out.println("双参构造方法被调用");
}
// 3. 三参构造方法(主要构造方法)
public ConstructorChaining(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
System.out.println("三参构造方法被调用");
}
public void display() {
System.out.printf("Name: %s, Age: %d, Address: %s%n", name, age, address);
}
public static void main(String[] args) {
System.out.println("创建对象:");
ConstructorChaining obj = new ConstructorChaining();
obj.display();
System.out.println("\n创建带参数的对象:");
ConstructorChaining obj2 = new ConstructorChaining("Alice", 25, "New York");
obj2.display();
}
}
输出结果:
创建对象:
三参构造方法被调用
双参构造方法被调用
无参构造方法被调用
Name: Unknown, Age: 0, Address: Unknown Address
创建带参数的对象:
三参构造方法被调用
Name: Alice, Age: 25, Address: New York
super
// 父类
class Animal {
protected String name = "Animal";
protected String sound = "Makes sound";
public void makeSound() {
System.out.println(sound);
}
protected void eat() {
System.out.println("Animal is eating");
}
public Animal getParent() {
return this;
}
}
// 子类
class Dog extends Animal {
private String name = "Dog"; // 隐藏父类的name字段
private String sound = "Bark";
public void displayNames() {
System.out.println("子类name: " + this.name); // 子类的name
System.out.println("父类name: " + super.name); // 父类的name
System.out.println("子类sound: " + this.sound); // 子类的sound
System.out.println("父类sound: " + super.sound); // 父类的sound
}
@Override
public void makeSound() {
System.out.println("Dog says: " + this.sound);
System.out.print("Parent says: ");
super.makeSound(); // 调用父类的方法
}
public void eatAndPlay() {
super.eat(); // 调用父类的方法
System.out.println("Dog is playing");
}
// 不能通过super.super访问更上层的父类
// public void invalidAccess() {
// super.super.makeSound(); // 编译错误
// }
public static void main(String[] args) {
Dog dog = new Dog();
dog.displayNames();
System.out.println();
dog.makeSound();
System.out.println();
dog.eatAndPlay();
}
}
输出结果:
子类name: Dog
父类name: Animal
子类sound: Bark
父类sound: Makes sound
Dog says: Bark
Parent says: Makes sound
Animal is eating
Dog is playing
// 父类
class Vehicle {
private String brand;
private int maxSpeed;
// 父类无参构造方法
public Vehicle() {
this.brand = "Unknown";
this.maxSpeed = 100;
System.out.println("Vehicle无参构造方法");
}
// 父类有参构造方法
public Vehicle(String brand, int maxSpeed) {
this.brand = brand;
this.maxSpeed = maxSpeed;
System.out.println("Vehicle有参构造方法");
}
public void display() {
System.out.printf("Brand: %s, Max Speed: %d km/h%n", brand, maxSpeed);
}
}
// 子类
class Car extends Vehicle {
private int doors;
private String fuelType;
// 子类构造方法1 - 调用父类无参构造方法(隐式调用)
public Car(int doors, String fuelType) {
// 这里隐含了 super(); 调用父类无参构造方法
this.doors = doors;
this.fuelType = fuelType;
System.out.println("Car双参构造方法");
}
// 子类构造方法2 - 显式调用父类有参构造方法
public Car(String brand, int maxSpeed, int doors, String fuelType) {
super(brand, maxSpeed); // 必须放在第一行
this.doors = doors;
this.fuelType = fuelType;
System.out.println("Car四参构造方法");
}
@Override
public void display() {
super.display(); // 调用父类方法
System.out.printf("Doors: %d, Fuel Type: %s%n", doors, fuelType);
}
public static void main(String[] args) {
System.out.println("创建Car对象1:");
Car car1 = new Car(2, "Electric");
car1.display();
System.out.println("\n创建Car对象2:");
Car car2 = new Car("Toyota", 180, 4, "Hybrid");
car2.display();
}
输出结果:
创建Car对象1:
Vehicle无参构造方法
Car双参构造方法
Brand: Unknown, Max Speed: 100 km/h
Doors: 2, Fuel Type: Electric
创建Car对象2:
Vehicle有参构造方法
Car四参构造方法
Brand: Toyota, Max Speed: 180 km/h
Doors: 4, Fuel Type: Hybrid
重要规则:
1. this() 和 super() 在构造方法中必须放在第一行,且不能同时出现
2. 静态方法中不能使用 this 和 super
3. super 不能跳过直接父类访问更上层的祖先类
4. 合理使用 this 和 super 可以提高代码的可读性和维护性
更多推荐



所有评论(0)