谁能帮我详细讲讲java中“is a”和“has a”的概念和区别?

Python015

谁能帮我详细讲讲java中“is a”和“has a”的概念和区别?,第1张

is a 是对java的解释说明。 java is a... = java是(一个)...

has a 是说java拥有... 不过英语中表示某物“拥有...”时用 have/ has的情况比较少。一般都说there is/are...

is-a 是继承关系。在继承关系中,一个子类继承于父类,其本身可以称之为父类。例如:狗继承于动物,可以说狗是一种动物。

has-a 是组成关系,在组成关系中,一个对象将一个或者多个其它对象作为自己的成员。

1. IS-A, HAS-A两种经典OO模式:

1.1 You can just use IS-A to figure out the relationship of Subclass and Superclass. If B is a A, that means class B extends class A. That's TRUE everywhere in the inheritance tree.

Example: Canine(犬科动物) is-A Animal, So Class Canine extends AnimalWolf is-A Canine, So class wolf extends CanineBut note you can't change their position, Animal is-A Canine never happen, so class animal never extends Canine.

1.2 HAS-A, we can remember a case: Bathroom HAS-A Tub, Tub NEVER HAS-A Bathroom, that means class Bathroom has a instance variable(field) of class tub.

as we can see:

public class Bathroom()

{ private Tub tub

Tub.flush()}

public class Tub()

{ public void flush()

{ //more flush code here. } }

has-a 有一个, is-a 是一个

比如继承 ,有一个图形类和一个圆形类,圆形类继承于图形类。可以说圆形是一个图形,就是is-a的意思。

比如组合,一个汽车类和一个轮子类,汽车有轮子,就是has-a的意思。