話不多說,之間上代碼:
interface Father {
public int strong();
}
interface Mother {
public int kind();
}
class FatherImpl implements Father {
public int strong() {
return 8;
}
}
class MotherImpl implements Mother {
public int kind() {
return 8;
}
}
class Son extends FatherImpl implements Mother {
public int strong() {
return super.strong() + 1;
}
public int kind() {
return new MotherSpecial().kind();
}
private class MotherSpecial extends MotherImpl {
public int kind() {
return super.kind() - 1;
}
}
}
class Daughter extends MotherImpl implements Father {
public int strong() {
return new FatherImpl() {
public int strong() {
return super.strong() - 2;
};
}.strong();
}
}
選自:《編寫高質量代碼 改善Java程序的151個建議》