我做了一些阅读成分通过的Java世界的文章:http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=1继承虽然组成?
在这篇文章中,它指导如何做到继承,而不使用extends关键字。我正在那样做。这可以吗?它似乎为我工作。
下面是代码:
的遗传代码的例子:第2页的文章\ 根据这篇文章,它工作正常:
class Fruit {
public int peel() {
System.out.println("Peeling is appealing.");
return 1;
}
}
class Apple extends Fruit {
}
class Example1 {
public static void main(String[] args) {
Apple apple = new Apple();
int pieces = apple.peel();
}
}
修改后,它不再OK:
class Peel {
private int peelCount;
public Peel(int peelCount) {
this.peelCount = peelCount;
}
public int getPeelCount() {
return peelCount;
}
}
class Fruit {
// Return a Peel object that
// results from the peeling activity.
public Peel peel() {
System.out.println("Peeling is appealing.");
return new Peel(1);
}
}
// Apple still compiles and works fine
class Apple extends Fruit {
}
// This old implementation of Example1
// is broken and won't compile.
class Example1 {
public static void main(String[] args) {
Apple apple = new Apple();
int pieces = apple.peel();
}
}
现在通过组合物重用:
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();
}
}
后来它可以通过界面做的多晶型:见和javaworld 4页的“使用接口设计:”我不能在这里发布另一个链接。
interface Peelable {
int peel();
}
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 implements Peelable {
private Fruit fruit = new Fruit();
public int peel() {
return fruit.peel();
}
}
class FoodProcessor {
static void peelAnItem(Peelable item) {
item.peel();
}
}
class Example5 {
public static void main(String[] args) {
Apple apple = new Apple();
FoodProcessor.peelAnItem(apple);
}
}
关键是代替继承,我们创建了一个新的超类实例。而不是传统的多态。据说使用这种方法比继承更好,因为继承被打破了。
编辑:以后,如果我们想的水果晶型成不同的类型,而不是苹果公司,我们可以这样做:
class Banana implements Peelable {
private Fruit fruit = new Fruit();
public int peel() {
return fruit.peel();
}
}
这是怎么多态性是由组成完成。所以,如果一个方法需要一个可剥离的水果,我们只需要将Peelable对象传递给它而不是水果。
请阅读第3个答案。我为此主题添加了更多信息。
你是什么意思“遗传被打破”?它有什么坏处? – Neowizard 2010-11-04 02:47:14
''是的,这不是我的代码,是瘸子...它是继承是破产和单身,SUCK大的时间。 :P' ' –
rsenna
2010-11-04 03:11:06
呃,对不起。我应该说清楚。它像Java一样通过扩展继承。请阅读下面的第三个答案了解更多详情。 – Amumu 2010-11-04 04:39:45