Java Foundation 06 Combination

Source: Internet
Author: User
Tags class definition

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

We have tried to define the class. Defines a class, which is a new type. With the class, we then construct the object of the corresponding type. Further, each type should also have a clear interface (interface)for the user to use.

We can use other objects in the definition of a new class. This is the combination (composition). Composition is one of the basic means of implementing program reuse (Reusibility) in Java.

combination with "Has-a"

One object is a data member of another object. For example, we look at the example of the charging torch mentioned earlier:

A battery in a charging torch, an LED light, a button ... Can be an object. We can define a battery class to define and produce a battery object. In the class definition of the charging torch, a battery object can be used as its data member to represent the state of the battery portion.

We define a battery class below and use power to represent its power. A battery can be recharged (chargebattery) and Used (Usebattery). We use objects of the battery type as data members in the subsequent Torch class definition:

ClassBattery {Publicvoid Chargebattery (DoubleP) {if (This.power < 1.) {This.power =This.power +P } }PublicBoolean Usebattery (DoubleP) {if (This.power >=P) {This.power =This.power-PReturnTrue; }Else{This.power = 0.0;ReturnFalse; } }PrivateDouble power = 0.0;}Classtorch{
/**
* 10% power per hour use
* Warning when out of power
*/Publicvoid TurnOn (IntHours) {BooleanUsable usable =This.theBattery.useBattery (hours*0.1);if (Usable! = true); }}
/**
* 20% power per hour charge
*/public void Charge ( Int hours) {this.thebattery.chargebattery (hours*0.2< Span style= "color: #000000;" > ); } /** composition */ Span style= "color: #0000ff;" >private Battery Thebattery = new Battery ();}

The above new allocates memory for the Thebattery object, which is indispensable.

We define the battery class. The Torch class uses an object of type battery (thebattery) as a data member. In the torch approach, we implement the functionality (functionality) provided by the battery class by manipulating the interface of the Thebattery object.

We say that a Torch object owns (has-a) a battery object. The above relationship can be expressed as:

HAS-A: Flashlight has battery (note the diamond connection above)

By combining, we can reuse battery-related code. If we have other classes that use battery, such as mobile phones and calculators, we can combine battery objects. This makes it not necessary to write a separate function for each class.

We can add a test class to see the actual effect:

Class test{    voidnew Torch (); System.out.println ("Charge:2 hours"); Atorch.charge (2); System.out.println ("First Turn On:3 hours"); Atorch.turnon (3); System.out.println ("Second Turn on:3 Hours"); Atorch.turnon (3);}}       

The operation result of the above program:

Charge:2 hours
First Turn On:3 hours
Second Turn On:3 Hours
No more usable, must charge!

We use a combination of functions provided by the battery object, such as detecting whether the charge is exhausted (based on the return value of Usebattery ()).

Basic Type

From HelloWorld to object-oriented, we refer to int, float, double, boolean, etc. as the basic type (primitive type), which is the special class. We can interpret an integer as an object of type int. The int type can have an assignment, addition, subtraction and other operation interfaces. The normal type can be seen as an extension of the basic type. We've seen the basic type as a data member, a parameter to a method, a method's return value, and an automatic variable inside a method . Naturally, ordinary types of objects, such as battery and torch, can also be used in these places.

In the C language, the available data types (basically) are already preset, such as int, float. In Java, in addition to using these preset data types, we can customize the type of data we want by using a class, and then use it through a combination. But the basic type and the normal type are still different. Basic types are often used and occupy little memory space, so in Java, for efficiency, these basic types differ from the memory management of ordinary types (that is, custom classes). For example, once the base type is declared, the memory space is allocated, and the normal type needs to allocate memory space using the New keyword.

Java provides the appropriate generic type for each base type. For example, an int primitive type corresponds to an integer type. If you turn an object of the base type into a corresponding normal type variable, the so-called basic type becomes a generic type (no more memory-management differences).

In this way, wehave a deeper understanding of the concept of "all objects" in Java.

Summary

Combination, has-a

Basic type

Java Foundation 06 Combination

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.