难以理解扩展和实例化。 extends 和实例化一个 class 一样吗?
Trouble with understanding extends and instantiate. Is extends the same as instantiating a class?
我的问题是。这些是一样的吗?
public class Pet {
}
public class Fish extends Pet {
}
如果我将 class 宠物扩展到我的鱼 class,是否与我在我的鱼 class 中实例化宠物 class 一样?扩展在上面,实例化在下面。它们是一样的吗?
public class Pet {
}
public class Fish {
Pet myPet = new Pet ();
}
它们不一样。
在第一个示例中,通过继承,Fish
的实例可以访问它自己的所有属性和方法,包括从 Pet
继承的属性和方法,通过 this
或 self
,取决于语言。
在第二个例子中,myPet
只是一个变量,恰好是Pet
class的一个实例,但是Pet
和Fish
互无关系
不,它们完全不同。
在这个 public class Fish extends Pet { }
中,您使用继承将 Pet class 扩展到 Fish class,这意味着 Fish 是 Pet 的子class 它会继承宠物class.
然而在这一个
public class Fish {
Pet myPet = new Pet (); }
你正在创建一个名为 Fish 的全新对象,它不从任何东西扩展,只是它有一个 class 级别的对象,它是一个 Pet,所以你可以使用 Pet 对象方法变量等通过myPet 对象,但是它没有被 Fish 继承,所以 Fish 将是它自己的对象,而不是 Pet.
的子class
这些是区别。
至于何时应该使用哪个,一般规则如下:如果您要增强 class,那么您应该使用继承,但是如果您只是要使用 class对于它的特定功能,那么您应该将其实例化为 class.
中的变量
他们完全不同。 extends 是 'is-a' relationship ,而 later (composition) 是 'has-a'。查看here了解更多详情。
第一个示例描述了继承,第二个示例描述了组合。这是两个 OOP 概念。它们允许程序员重用通用逻辑。你应该更喜欢使用组合而不是继承。
从其他 SO 复制 answer:
They are absolutely different. Inheritance is an "is-a" relationship.
Composition is a "has-a".
You do composition by having an instance of another class C as a field
of your class, instead of extending C. A good example where
composition would've been a lot better than inheritance is
java.util.Stack, which currently extends java.util.Vector. This is now
considered a blunder. A stack "is-NOT-a" vector; you should not be
allowed to insert and remove elements arbitrarily. It should've been
composition instead.
Unfortunately it's too late to rectify this design mistake, since
changing the inheritance hierarchy now would break compatibility with
existing code. Had Stack used composition instead of inheritance, it
can always be modified to use another data structure without violating
the API.
I highly recommend Josh Bloch's book Effective Java 2nd Edition
Item 16: Favor composition over inheritance
Item 17: Design and document for inheritance or else prohibit it
Good object-oriented
design is not about liberally extending existing classes. Your first
instinct should be to compose instead.
我的问题是。这些是一样的吗?
public class Pet {
}
public class Fish extends Pet {
}
如果我将 class 宠物扩展到我的鱼 class,是否与我在我的鱼 class 中实例化宠物 class 一样?扩展在上面,实例化在下面。它们是一样的吗?
public class Pet {
}
public class Fish {
Pet myPet = new Pet ();
}
它们不一样。
在第一个示例中,通过继承,Fish
的实例可以访问它自己的所有属性和方法,包括从 Pet
继承的属性和方法,通过 this
或 self
,取决于语言。
在第二个例子中,myPet
只是一个变量,恰好是Pet
class的一个实例,但是Pet
和Fish
互无关系
不,它们完全不同。
在这个 public class Fish extends Pet { }
中,您使用继承将 Pet class 扩展到 Fish class,这意味着 Fish 是 Pet 的子class 它会继承宠物class.
然而在这一个
public class Fish {
Pet myPet = new Pet (); }
你正在创建一个名为 Fish 的全新对象,它不从任何东西扩展,只是它有一个 class 级别的对象,它是一个 Pet,所以你可以使用 Pet 对象方法变量等通过myPet 对象,但是它没有被 Fish 继承,所以 Fish 将是它自己的对象,而不是 Pet.
的子class这些是区别。
至于何时应该使用哪个,一般规则如下:如果您要增强 class,那么您应该使用继承,但是如果您只是要使用 class对于它的特定功能,那么您应该将其实例化为 class.
中的变量他们完全不同。 extends 是 'is-a' relationship ,而 later (composition) 是 'has-a'。查看here了解更多详情。
第一个示例描述了继承,第二个示例描述了组合。这是两个 OOP 概念。它们允许程序员重用通用逻辑。你应该更喜欢使用组合而不是继承。
从其他 SO 复制 answer:
They are absolutely different. Inheritance is an "is-a" relationship. Composition is a "has-a".
You do composition by having an instance of another class C as a field of your class, instead of extending C. A good example where composition would've been a lot better than inheritance is java.util.Stack, which currently extends java.util.Vector. This is now considered a blunder. A stack "is-NOT-a" vector; you should not be allowed to insert and remove elements arbitrarily. It should've been composition instead.
Unfortunately it's too late to rectify this design mistake, since changing the inheritance hierarchy now would break compatibility with existing code. Had Stack used composition instead of inheritance, it can always be modified to use another data structure without violating the API.
I highly recommend Josh Bloch's book Effective Java 2nd Edition
Item 16: Favor composition over inheritance
Item 17: Design and document for inheritance or else prohibit it
Good object-oriented design is not about liberally extending existing classes. Your first instinct should be to compose instead.