this 代表正在執行方法/構造器的對象實例,非靜態上下文專用。
public class Person {
private String name; // 實例字段
public Person(String name) { // 參數遮蔽字段
this.name = name; // this.字段 = 參數
}
public void setName(String name) {
this.name = name; // 同理
}
}
規則:作用域最近者優先。無遮蔽時 this 可省略,但規範代碼建議保留 增強可讀性。
public class Book {
private String title;
private String author;
private int pages;
// 全參構造器:核心初始化邏輯
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
// 便利構造器:委託全參構造器
public Book(String title, String author) {
this(title, author, 0); // 必須為構造器第一條語句
}
public Book(String title) {
this(title, "Unknown");
}
}
this(...) 必須放在構造器第一行,一個構造器最多一條。super(...) 共存。public class StringBuilder {
private char[] buffer;
public StringBuilder append(String s) {
// 追加邏輯...
return this; // 返回自身,支持鏈式
}
}
// 使用
new StringBuilder().append("A").append("B").append("C");
public class EventSource {
public void register(Listener l) { ... }
}
public class MyComponent implements Listener {
public void init(EventSource source) {
source.register(this); // 傳遞自身作為監聽器
}
}
super 代表當前對象的父類部分,非靜態上下文專用。
public class Animal {
protected String species;
public Animal(String species) { this.species = species; }
}
public class Dog extends Animal {
private String name;
public Dog(String name) {
super("Canine"); // 顯式調用父類帶參構造器
this.name = name;
}
public Dog(String name, String species) {
super(species); // 透傳參數
this.name = name;
}
}
super()(調用父類無參構造器)。super(args)。this(...) 共存。public class Base {
public void greet() { System.out.println("Hello from Base"); }
}
public class Derived extends Base {
@Override public void greet() {
super.greet(); // 調用父類版本
System.out.println("Hello from Derived");
}
public void callBoth() {
greet(); // 調用覆寫版(多態)
super.greet(); // 顯式調用父類版
}
}
未覆寫時 super.method() 可省略(直接 method() 即調用父類)。
public class Base { protected int value = 1; }
public class Derived extends Base { private int value = 2; }
public class Test {
public static void main(String[] args) {
Derived d = new Derived();
System.out.println(d.value); // 2 (子類字段)
System.out.println(((Base)d).value); // 1 (父類字段,需強轉)
// 或在 Derived 內部:
// System.out.println(super.value); // 1
}
}
最佳實踐:避免字段遮蔽,字段命名區分層級或用 private 封裝 + getter/setter。
public class Example {
public static void staticMethod() {
// this.toString(); // 編譯錯誤:靜態方法無 this
// super.toString(); // 編譯錯誤:靜態方法無 super
}
public void instanceMethod() {
this.toString(); // ✓
super.toString(); // ✓
}
}
原因:靜態方法屬於類,不屬於具體實例,無 this/super 綁定對象。
public class Vehicle {
protected String brand;
protected int speed;
public Vehicle(String brand) {
this.brand = brand;
this.speed = 0;
}
public void accelerate(int increment) {
speed += increment;
}
}
public class Car extends Vehicle {
private int gear; // 子類特有字段
// 構造器鏈
public Car(String brand) {
this(brand, 1); // 委託全參
}
public Car(String brand, int gear) {
super(brand); // 父類初始化
this.gear = gear;
}
// 覆寫並擴展
@Override
public void accelerate(int increment) {
super.accelerate(increment); // 父類邏輯
if (speed > 60 && gear < 3) shiftGear(3); // 子類擴展
}
private void shiftGear(int g) { gear = g; }
}