今天在SCJP的试题中看见关于‘is a relationship’ ,‘has a relationship’的试题,一开始没搞清楚怎么回事,就google了一下,找到如下文章,觉得挺有用的。跟大家分享下啊。
what is 'is a' and 'has a' relationship:
The ‘is a relationship’ is expressed with inheritance and ‘has a relationship’ is expressed with composition.
Both inheritance and composition allow you to place sub-objects inside your new class. Two of the main techniques for code reuse are class inheritance and object composition.
e.g.,
is a --- House is a Building
class Building {
.......
}
class House extends Building {
.........
}
has a -- House has a bathroom
class House
{
Bathroom room = new Bathroom() ;
....
public void getTotMirrors()
{
room.getNoMirrors();
....
}
}
Inheritance Vs Composition
Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses
‘extends’ key word. Composition: is used when House has a Bathroom. It is incorrect to say House is a Bathroom.
Composition simply means using instance variables that refer to other objects. The class House will have an instance variable, which refers to a Bathroom object.
分享到:
相关推荐
But every single chapter has been updated in some way, whether it is a new language feature like variable argument methods; the new enhanced for loop construct; a new class such as Formatter for ...
婚恋交友系统java源码 java mysql婚恋交友网站源码+论文(原订做价1k) 下载地址: 项目介绍 java mysql婚恋交友网站源码+论文(原订做价1k) 系统说明 设计说明 关键字:交友网站,Java编程语言,MySQL...
7. InputStreamReader is a Java class used to convert a byte stream (InputStream) into a character stream (Reader), facilitating the reading of text data in different character encodings. 8. In Java, ...
JUnit has matured to become the most important tool when it comes to automated developer tests in Java. Supported by all IDEs and build systems, it empowers programmers to deliver software features ...
The backend employs the SSM (Spring, SpringMVC, MyBatis) framework, which is a popular choice for building Java web applications due to its flexibility and modularity. Spring provides dependency ...
The main programming language used in this system is JSP, which stands for JavaServer Pages. It is a server-side scripting language that allows developers to create dynamic web content by embedding ...
This gives him an excellent all-round view of the role of a technical lead and its relationship with other roles as well as insight into every stage of project delivery, from initial analysis to long...
系统, and has corresponding operation permissions for the background. To realize the various functions of the drug sales system, the strong support of the background database is needed. The ...
基于java tcp socket通信的拆包和装包源码 Biz table data for any team FMSOP Dapp v1 Team Collaboration Customer Relationship Mangement Pre/After Sales Services 组成 微信小程序 for customer or employee ...