当时,Guys在寻找组成和继承之间的区别时,我发现这个职位有些地方。
根据员额组成情况,在重新使用法典方面比继承更好。
class Fruit {
// Return int number of pieces of peel that
// resulted from the peeling activity.
public int peel() {
System.out.println("Peeling is appealing.");
return 1;
}
}
class Apple {
private Fruit fruit = new Fruit();
public int peel() {
return fruit.peel();
}
}
class Example2 {
public static void main(String[] args) {
Apple apple = new Apple();
int pieces = apple.peel();
}
}
改变后级
class Peel {
private int peelCount;
public Peel(int peelCount) {
this.peelCount = peelCount;
}
public int getPeelCount() {
return peelCount;
}
//...
}
class Fruit {
// Return int number of pieces of peel that
// resulted from the peeling activity.
public Peel peel() {
System.out.println("Peeling is appealing.");
return new Peel(1);
}
}
// Apple must be changed to accomodate // the change to Fruit
class Apple {
private Fruit fruit = new Fruit();
public int peel() {
Peel peel = fruit.peel();
return peel.getPeelCount();
}
}
// This old implementation of Example2 // still works fine.
class Example1 {
public static void main(String[] args) {
Apple apple = new Apple();
int pieces = apple.peel();
}
}
这是我根据该职位掌握的情况,即,“如果后端班的交接发生变化,上端班的实施也发生变化,但不是接口”。
<>interface, 在这里,?
THIS IS JUST MY ASSUMPTION TELL ME JUST IM IM WRONG?
Is the back-end class the interface because front-end class depends on it?
Is the implementation of front-end class the implementation code inside the front-end class?
Is front-end class also an interface because the code inside Example depends on it?
它们是否由于改为后端界面<>/strong>而有意改为front-end界面? 因此,取决于front-end界面的代码仍然有效。
If I use inheritance instead is subclass tightly coupled in superclass? Because change to superclass will also change to subclass so code that depends on subclass will may not work.
任何遗产都很薄弱。