Java thousand ask _05 Object Oriented (006) _IS-A,HAS-A,LIKE-A is what __java

Source: Internet
Author: User

Click to enter _ more _java thousand ask 1, is-a,has-a,like-a is what

In the field of object-oriented design, there are several kinds of design ideas, mainly in the following three kinds:
Is-a, Has-a, like-a
There are many embodiments of Java in classes, interfaces, and abstract classes.
Learn Java See here: what is Java
Learn about classes and objects look here: What's the secret to a class or object?
Understanding interfaces and abstract classes look here: What is the difference between an interface and an abstract class 2, what is Is-a

Is-a, as the name suggests, is one that represents an inheritance relationship.
If a is-a B, then B is the parent class of a.
A class completely contains all the properties and behavior of another class.
For example, PC is a computer, workstation is also a computer, PC and workstation are two different types of computers, but all inherit the common characteristics of the computer. So when implemented in the Java language, you should define your PC and workstation as two kinds, inheriting the computer class.
Learn more inheritance see here: what is the meaning of Java class inheritance 3, has-a is what

Has-a, as the name suggests, has one, represents the subordinate relationship.
If A has a B, then B is part of a.
Objects of the same kind are distinguished by the different values of their properties.
For example, the operating system of a PC is windows, and the operating system of another PC computer is Linux. Operating system is a member variable of PC, according to the different value of this member variable, can distinguish different PC object. 4. What is Like-a

Like-a, as the name suggests, is like one that represents a combination of relationships.
If A is like a B, then B is the interface of a.
The new type has an interface of the old type, but it also contains other functions, so it cannot be said to be identical.
For example, a mobile phone can be said to be a microcomputer, but the mobile phone's communication function is clearly not the behavior of the computer, so the phone inherits the characteristics of the computer, and the need to implement communication functions, and communication functions need to be a separate interface, rather than the behavior of the computer. 5. How to apply Is-a,has-a,like-a

If you are certain that two objects are is-a relationships, you should use inheritance at this point, such as diamonds, circles, and squares are all shapes, so they should all inherit from the shape class.
If you are sure that two objects are has-a, then you should use aggregations, such as the computer is made up of monitors, CPUs, hard drives, etc., so you should aggregate the display, CPU and hard disk into computer classes.
If you are certain that two objects are like-a, then you should use a combination; For example, the air conditioner inherits from the refrigerator, but it has a heating function at the same time, then you should put the air-conditioning into the refrigerator class and realize the heating interface.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.