在 Java 中,this
和 super
关键字用于引用当前对象和父类对象。它们在类的构造器、方法和变量中都有不同的用法。下面详细介绍这两个关键字的用法和示例。
this 关键字
this
关键字用于引用当前对象。它可以用于以下几个方面:
1. 引用当前对象的实例变量
- 当局部变量和实例变量名称相同时,使用 this 来区分它们。
- 示例:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
// 使用 this 引用实例变量
this.name = name;
// 使用 this 引用实例变量
this.age = age;
}
public void display() {
// 使用 this 引用实例变量
System.out.println("Name: " + this.name);
// 使用 this 引用实例变量
System.out.println("Age: " + this.age);
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person("Alice", 30);
person.display();
}
}
2. 调用当前对象的方法
- 在一个方法中调用同一个类的另一个方法。
- 示例:
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int multiply(int a, int b) {
return a * b;
}
public int complexOperation(int a, int b, int c) {
// 调用当前对象的方法
int sum = this.add(a, b);
// 调用当前对象的方法
return this.multiply(sum, c);
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
int result = calc.complexOperation(2, 3, 4);
// 输出 20
System.out.println("Result: " + result);
}
}
3.调用当前类的构造器
- 在一个构造器中调用同一个类的另一个构造器。
- 示例:
public class Person {
private String name;
private int age;
public Person() {
// 调用另一个构造器
this("Unknown", 0);
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Person person1 = new Person();
// 输出 Name: Unknown, Age: 0
person1.display();
Person person2 = new Person("Alice", 30);
// 输出 Name: Alice, Age: 30
person2.display();
}
}
super 关键字
super
关键字用于引用父类的对象。它可以用于以下几个方面:
1.引用父类的实例变量
- 在子类中引用父类的实例变量。
- 示例:
public class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
}
public class Dog extends Animal {
private String breed;
public Dog(String name, String breed) {
// 调用父类的构造器
super(name);
this.breed = breed;
}
public void display() {
// 引用父类的实例变量
System.out.println("Name: " + super.name);
System.out.println("Breed: " + breed);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Rex", "German Shepherd");
// 输出 Name: Rex, Breed: German Shepherd
dog.display();
}
}
2.调用父类的方法
- 在子类的方法中调用父类的方法。
- 示例:
public class Animal {
public void makeSound() {
System.out.println("Animal makes a sound");
}
}
public class Dog extends Animal {
@Override
public void makeSound() {
// 调用父类的方法
super.makeSound();
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
// 输出 Animal makes a sound, Dog barks
dog.makeSound();
}
}
3.调用父类的构造器
- 在子类的构造器中调用父类的构造器。
- 示例:
public class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public void display() {
System.out.println("Animal: " + name);
}
}
public class Dog extends Animal {
private String breed;
public Dog(String name, String breed) {
// 调用父类的构造器
super(name);
this.breed = breed;
}
public void display() {
// 调用父类的方法
super.display();
System.out.println("Breed: " + breed);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Rex", "German Shepherd");
// 输出 Animal: Rex, Breed: German Shepherd
dog.display();
}
}
总结
this
关键字:- 引用当前对象的实例变量。
- 调用当前对象的方法。
- 调用当前类的构造器。
super
关键字:- 引用父类的实例变量。
- 调用父类的方法。
- 调用父类的构造器。