[Original] object-oriented understanding · understanding and using abstract classes and derived classes

Source: Internet
Author: User

author: pfeng, email: pfengk@hotmail.com
recently, we found that different functional blocks can be implemented using the same interface. So I thought about how to reuse Code to speed up development. Remember that one concept in the object-oriented Program design is class polymorphism. If we use this concept, we can solve this problem, but how can we implement it? It took a lot of time to check the information and finally understood the concept and usage of abstract classes and derived classes. I felt that this was what I was looking. You don't need to try it out now... OK.
when these concepts are applied to a project, it is found that the design is more complex and rigorous than the process-oriented design. The time spent in the design process is greatly increased, it focuses more on requirements, analysis, and design. Of course, its advantages are self-evident. Increase code reusability, reduce code development, and reduce project maintenance costs. A good design will increase the robustness of the system.
abstract class concept: abstract classes cannot be instantiated. Abstract classes are used to provide public definitions of the base classes that can be shared by multiple Derived classes. For example, a class library can define an abstract class as a parameter of multiple of its functions, and require programmers to use this library to provide their own class implementation by creating a derived class.
concept of a derived class: the new class (that is, a derived class) obtains all non-private data and behaviors of the base class and all other data or behaviors defined by the new class. Therefore, the new class has two valid types: the type of the new class and the type of the class it inherits.


1 Public   Abstract Class A
2 {
3 Public   Abstract A () {} ;
4 Public   Abstract   Void A1 ();
5
6 }
7
8 Public   Class B:
9 {
10 Public Overide B () {}
11 Public Overide Void B1 ();
12
13 }
14
15

Class B is both valid B and valid. When accessing object B, you can use the forced conversion operation to convert it to object. Forced conversion does not change B object, but your B object view limits the data and behavior to. After B is forcibly converted to a, the can be forcibly converted to B. If Class B is used as Class B, data and behavior of Class A and Class B can be obtained at the same time. Objects can represent multiple types of capabilities called polymorphism.
by understanding these concepts, you can define the interface as the presentation layer, business processing as the business logic layer, and database operations as the data operation layer. A abstract class A is defined in the business logic layer. Business Processing of different functions is generated by. The interface presentation layer defines the AI variable when the type is. When entering different functions, a new corresponding derived class is forcibly converted to a to assign AI, and all operations on the interface layer call the AI method. In this way, you must use the same interface to implement different functions. In the future, if there is a function similar to the new one, you only need to implement a derived class BX in the business logic layer. Add a new instance Bix to the page. If the business logic is not very complex, it takes 5 to 10 minutes to complete the development of this function.

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.