Reconstruction related to inheritance (1)

Source: Internet
Author: User

 

This topic mainly involves several reconstruction of inheritance. Inheritance generally involves abstract classes and interfaces. We usually extract classes with similar func classes from the same class, that is, abstract interfaces or abstract classes; the benefits of doing so are self-evident, so that you can easily expand, modify, and maintain the common methods, attributes, indexes, and so on of sub-classes. The use of base classes not only links each subclass, but also reduces the coupling between each subclass and reflects the idea of interface-oriented and inherited programming. 1. Promotion Method: refers to the process of migrating a method to the upper layer of the inheritance chain. Usage scenario: When a method in subclass A needs to be called by multiple subclasses, You need to promote this method to the base class, instead, do not allow other subclasses to go to this method in call subclass. The only drawback of this is that it expands the interface of the base class and increases its complexity. Therefore, you need to use it with caution. Migration is required only when more than one subclass needs to use this method. 2. decentralization method usage scenario: This refactoring is the opposite of the above method. When a method in the base class only needs to be implemented by one or a few subclasses, you can consider placing the methods in the base class in the appropriate subclass to avoid unnecessary confusion. 3. Use Case for raising attributes: similar to the move-up method, the method is changed to the attribute here, and the attributes required for most subclass are upgraded to the base class. 4. Decentralization attribute usage scenario: This is the opposite of the Method for Improving attributes. Only the attributes that are used by some subclass are decentralized.
W-Y: hidden; "> The following describes Code Demo.

This article focuses on several reconstruction related to inheritance. Inheritance generally involves abstract classes and interfaces. We usually extract classes with similar func classes from the same class, that is, abstract interfaces or abstract classes; the benefits of doing so are self-evident, so that you can easily expand, modify, and maintain the common methods, attributes, indexes, and so on of sub-classes. The use of base classes not only links each subclass, but also reduces the coupling between each subclass and reflects the idea of interface-oriented and inherited programming.
1.Improvement Method: Refers to the process of migrating methods to the upper layer of the inheritance chain.

Usage scenarios: In subclassWhen a method needs to be called by multiple sub-classes, it needs to be upgraded to the base class.Instead of letting other subclasses go to this method in call subclass. The only drawback of this is that it expands the interface of the base class and increases its complexity. Therefore, you need to use it with caution. Migration is required only when more than one subclass needs to use this method.

2.Delegation Method: Refers to the process of migrating methods to the lower layer of the inheritance chain.

Usage scenarios: This refactoring is the opposite of the above method. When a method in the base classIt only needs to be implemented by one or a few subclasses.You can consider the methods in the base classPlace it in the appropriate subclassTo avoid unnecessary confusion.

3.Improve attributes: Refers to the process of migrating attributes to the upper layer of the inheritance chain.

Usage scenarios: Similar to the move-up method, the method is changed to an attribute here,Promotes the attributes required by most subclasses to the base class..

4.Delegate attributes: Refers to the process of migrating attributes to the lower layer of the inheritance chain.

Usage scenarios: Opposite to the method for improving attributes,Place the attributes that only need to be used by some sub-classes.

The following is a code demonstration of the four refactoring methods.

 

Code

  Public     Abstract     Class  Vehicle
{
Public String Driver { Get ; Set ;} // Property enhancement: All vehicles have drivers
Public Void Turn ()
{
// Improvement Method: all transportation tools can be switched
}
}
Public Class Car: Vehicle
{

}
Public Class Plane: Vehicle
{
Public Void Fly ()
{
// Decentralization method: Only planes can fly
}
}
Public Class SHIP: Vehicle
{
Public String Anchor { Get ; Set ;} // Attribute decentralization: only the ship has an anchor.
}

 

Note that all methods and classes here can be re-described using the new keyword in the subclass to reasonably place methods and attributes in inheritance, field location is a very important reconstruction method, which is easy to use :)

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.