Some fragment of thinking in Java Part5

Source: Internet
Author: User

1. Delegation

A relationship that's not directly supported by Java. This is a midway between inheritance and composition, because your place a number object in the class you were building (like Composition), but at the same time you expose all the methods from the member object in your new class (like inheritance)

public class Spaceshipdelegation {

private String name;

Private Spaceshipcontrols controls = new Spaceshipcontrols ();

Public spaceshipdelegation (String name) {

THIS.name = name;

}

Delegated methods:

public void back (int velocity) {

Controls.back (velocity);

}

public void dowm (int velocity) {

Controls.down (velocity);

}

public void forward (int velocity) {

Controls.forward (int velocitly);

}

......

public static void Main (string[] args) {

Spaceshipdelegation protector = new Spaceshipdelegation ("Nsea protector");

Protector.forward (100);

}

}

2. guaranteening proper cleanup

You must also pay attention to the calling order for the Base-class and number object cleanup methods in case one SUBOBJEC T depends on another. Should follow the same form that's imposed by a C + + complier on its destructors:reverse the order of CRE ation.

3. Name hiding

If a Java base class has method name this is overloaded several times, redefineing the method name in the Derived-class W Ould not hide any of the Base-class versions (unlike C + +), Thus overloading works regardless of whether the method was Defi Ned at the-or in the base class

Class Homer {

Char doh (char c) {

Print ("Doh (char)");

return ' d ';

}

float Doh (float f) {

Print ("Doh (float)");

return 1.0f;

}

}

Class Milhouse {}

Class Bart extends Homer {

void Doh (Milhouse m) {//overloading between in inheritance? I ' m not sure

Print ("Doh (Milhouse)");

}

}

public class Hide {

public static void Main (string[] args) {

Bart B = new Bart ();

B.doh (1);

B.doh (' x ');

B.doh (1.0f);

B.doh (New Milhouse ());

}

}/*output:

Doh (float)

Doh (char)

Doh (float)

Doh (Milhouse)

*/

3. chossing Composition vs Inheritance

Composition is generally used if you want the functionality of a existing class inside your new class, but not it int Erface. That's, you embed a object so this can it to implement features in your new class, but the user of your new class SE Es the interface you ' ve defined for the new class rather than the interface from the embedded object. For the effect, you embed private objects of existing classes inside your new class.

The is-a relationship is expressed with inheritance, and the has-a relationship are expressed with composition.

4. Protected

The protected keyword is a nod to pragmatism. Available to anyone who inherits from this class or anyone else in the same package (protected also provides package access )

Some fragment of thinking in Java Part5

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.