“is a”代表的是类之间的继承关系,比如PC机是计算机,工作站也是计算机。PC机和工作站是两种不同类型的计算机,但都继承了计算机的共同特性。因此在用 Java语言实现时,应该将PC机和工作站定义成两种类,均继承计算机类。
“has a”代表的是对象和它的成员的从属关系。同一种类的对象,通过它们的属性的不同值来区别。比如一台PC机的操作系统是Windows,另一台PC机的操作系统是Linux。操作系统是PC机的一个成员变量,根据这一成员变量的不同值,可以区分不同的PC机对象。
再比如张三和李四都是人,但他们的名字不一样,可以以此区分这两个具体的人。名字应该作为人的成员变量。如果将名字叫“张三”的人和名字叫“李四”的人分别定义成两个类,均继承“人”这个类,显然是不合理的。
is a 的意识是如果A是B,那么B就是A的基类。比如:等边三角形是使是三角形,那么,三角形就是等边三角形的基类。
has a 是如果A中有B,那么,B就是A的组成部分
如果你确定两件对象之间是is-a的关系,那么此时你应该使用继承;比如菱形、圆形和方形都是形状的一种,那么他们都应该从形状类继承而不是聚合。
如果你确定两件对象之间是has-a的关系,那么此时你应该使用聚合;比如电脑是由显示器、CPU、硬盘等组成的,那么你应该把显示器、CPU、硬盘这些类聚合成电脑类,而不是从电脑类继承。
摘自:http://wenku.baidu.com/view/d9665979168884868762d685.html
分享到:
相关推荐
hprof-conv.exe The connection to adb is down, and a severe error has occured
可参考链接:is-a和has-a的区别 1、has a 关系 has a :一个类中使用了另外一种自定义的类型 类型: 系统类型 str、 int、 float、 list 、dict 、 tuple、 set 自定义类型 算是自定义的类,都可以将其当成一...
03-11_面向对象与is-a_has-a_7题定义.pdf
03-11_面向对象与is-a_has-a_7题[归纳].pdf
Kenneth has developed this is a great little application which reads a Microsoft Exchange mailbox and lets you know via playing a .WAV file when you have mail.
NULL 博文链接:https://navylee.iteye.com/blog/2076985
Here_is_a_collection_of_repository_that_has_applie_Software-CopyrightSoftware-Copyright.zip
This is a very good tic tac toe game for 2 players with the option to select who goes first. Also has good sound effects and graphics. Good for beginners to learn.
掌握这些基本的be动词和have/has的用法,以及如何进行否定句和一般疑问句的转换,是学习英语动词基础的重要步骤,对于日常对话和书面表达都至关重要。通过不断的练习和应用,可以更好地理解和运用这些语法知识。
Fortunately, Bill has a working stopwatch, so they can record their speed and the total time they have driven. Unfortunately, their record keeping strategy is a little odd, so they need help ...
in mathematics, it is beautiful and compelling, and there is a plethora of material. The literature in complex variables is vast and diverse. There are a great many textbooks in the subject, but each ...
A8105 Data Sheet, 2.4GHz FSK/GFSK SOC ... A8105 has various operating modes, making it highly suited for systems where ultra-low power consumption is required. The device is in QFN5X5 40 pin package
The text has a large number of diagrams and helpful margin notes, and uses many graded examples and exercises, often with complete solutions, to guide students through the tricky points. It is ...
THE Java™ programming language is a general-purpose, concurrent, classbased, object-oriented language. It is designed to be simple enough that many programmers can achieve fluency in the language. ...
A binary search algorithm (or binary chop) is a technique for finding a particular value in a sorted list. It makes progressively better guesses, and closes in on the sought value, by comparing an ...
)和"He has a red cup."(他有一个红杯子。) 2. 它们还可以用于表示"吃"或"喝"的动作。例如:"I have breakfast."(我吃早饭。)和"We have tea."(我们喝茶。) 3. 此外,"have"和"has"还可以表示"拿"、"取得...
Ipopt (Interior Point OPTimizer, pronounced eye-pea-Opt) is a software package for large-scale nonlinear optimization. Ipopt is written in C++ and is released as open source code under the Eclipse...
This is useless, and has no use to anyone