Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define the composite class of Java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to define the combination class of Java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to define the combination class of Java".

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

Combination and "has-a"

One object is a data member of another object. For example, let's take a look at the example of a rechargeable flashlight mentioned earlier:

A battery in a rechargeable flashlight, LED lights, buttons. Can be an object. We can define a Battery class to define and generate battery objects. In the class definition of a rechargeable flashlight, a battery object can be used as its data member to represent the state of the battery part.

Let's define a Battery class and use power to represent its power. An Battery can be charged (chargeBattery) and used (useBattery). We use objects of type Battery as data members in the subsequent Torch class definition:

Class Battery {public void chargeBattery (double p) {if (this.power)

< 1.) { this.power = this.power + p; } } public boolean useBattery(double p) { if (this.power >

= p) {this.power = this.power-p; return true;} else {this.power = 0; return false;}} private double power = 0;} class Torch {/ * 10% power per hour use * warning when out of power * / public void turnOn (int hours) {boolean usable Usable = this.theBattery.useBattery (hours*0.1); if (usable! = true) {System.out.println ("No more usable, must charge!");}} / * 20% power per hour charge * / public void charge (int hours) {this.theBattery.chargeBattery (hours*0.2) } / * * composition * / private Battery theBattery = new Battery ();}

The above new allocates memory for theBattery objects, 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 method, we implement the function provided by the Battery class (functionality) by manipulating the interface of the theBattery object.

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

Has-a: the flashlight has a battery (note the diamond connection above)

Through combination, 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. In this way, you don't have to write the relevant functionality for each class separately.

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

Public class Test {public static void main (String [] args) {Torch aTorch = new 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 running result of the above program:

Charge: 2 hours

First Turn On: 3 hours

Second Turn On: 3 hours

No more usable, must charge!

We combine to use the functionality provided by the battery object, such as detecting whether the battery is running out (based on the return value of useBattery ()).

Basic type

From HelloWorld to object-oriented, we refer to int, float, double, boolean and so on as primitive type, that is, special classes. We can think of an integer as an object of type int. Int types can have operation interfaces such as assignment, addition, subtraction, and so on. Ordinary types can be seen as an extension of basic types. We have seen primitive types as data members, parameters of methods, return values of methods, and automatic variables within methods. Naturally, common types of objects, such as those of the Battery and Torch classes, can also be used in these places.

In C language, the available data types are (basically) preset, such as int, float. In Java, in addition to using these preset data types, we can also customize the data types we want through classes, and then use them through combinations. But there is still a difference between the basic type and the ordinary type. Basic types are often used and take up a small amount of memory space, so in Java, these basic types are managed differently from normal types (that is, custom classes) for efficiency. For example, a basic type is allocated memory space once declared, while a normal type needs to allocate memory space using the new keyword.

Java provides a corresponding generic type for each basic type. For example, the int base type corresponds to the Integer type. If you convert an object of a basic type to a corresponding normal type variable, the so-called basic type becomes a type in the general sense (there is no difference in memory management).

Thank you for your reading, the above is the content of "how to define the combination class of Java". After the study of this article, I believe you have a deeper understanding of how to define the combination class of Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report