Object-oriented abstract class, interface, class Library, Five Principles

Source: Internet
Author: User
Tags iusb

Abstract class

An abstract class that appears only for inheritance, does not define specific content, and only defines what it is.
Only abstract methods are placed in general abstract classes, only the return type and parameters are specified
Like what:
People--there's a way to eat and sleep
Men--Heir to the abstract class, must realize the subject of eating, sleeping methods
Woman-Heir abstract class, must realize the subject of eating, sleeping methods

Abstract classes can have normal properties, using subclasses to

1.Key words:Abstract
2.Abstract classes can beinclude abstract methodsAndCommon Methods
3.The abstract keyword can define a method as an abstraction,abstract methods can have no function body
4.Abstract classcannot be instantiated, abstract classes are mainlyas a base class, let other classes inherit
5.sealed and abstractKey wordscannot appear at the same time
6.If a subclass inherits from an abstract class, thenall the abstract methods must be implemented in subclasses
7.if the subclass does not have an abstract method that implements the parent class, the subclass must be an abstract class
8.If a class contains abstract methods, then the class must be an abstract class

Abstract methods must be abstract classes.
Abstract class, there is not necessarily an abstract method

    //Defining abstract Classes     Public Abstract classDongwu { Public voidRun () {} Public Abstract voidEat (); }    //be a human being to inherit abstract classes     Public classRen:dongwu { Public Override voidEat () {Console.WriteLine ("Eat cooked food"); }    }

Interface

A class that is extremely abstract

function: You can split the program into multiple modules, defining the functions that subclasses must implement

Like what:

Head Office--established rules and regulations (interface)--the company must take attendance of employees

Subsidiary company to follow the company's rules and regulations-the implementation of time-specific attendance-punch
Subsidiary 2--follow the company's rules and regulations--to achieve the specific attendance--roll Call

The difference between an interface and an abstract class:

1. Differences in wording
  Keyword: interface
  There is no class keyword that starts with I
  Do not write public because it is public, do not write the abstract interface inside all are pumping
Elephant's

2. The interface cannot contain ordinary members
3. All classes that inherit the interface must implement the method inside the interface .

Because team development, each person is responsible for a module, I only responsible for the basic life part, another person responsible for the work part, as well as the individual responsible for recreational activities part

    //Defining Interfaces    InterfaceIusb {//start reading USB        voidStart (); //Turn off USB        voidStop (); }    //do a mouse class to implement USB interface    classShubiao:iusb { Public voidStart () {Console.WriteLine ("the mouse is activated."); }         Public voidStop () {Console.WriteLine ("the mouse stopped."); }    }

Class Library

Referencing a class written by someone else

1. Source code Method :
Can be directly written in the . CS Source code files , added into my Solution folder, in Solution Explorer,
Right-click on the item → add → existing item to add the use of this. cs source code file, you need to introduce the appropriate namespace

2. Class Library Methods :
a DLL file , which is a class library
It creates a new class library, writes classes and corresponding methods in it, generates a DLL file, takes it, and puts it in its own
Program folder, right-click on the project → add reference → find this DLL class library file add, and then reference the namespace , you can call the corresponding method in the class

Note Class must be public access rights

The use of class libraries is the way that multi-company co-development is used, because each company has its own core technology that I allow you to use, but not allow you
Know how I write, so I need DLL class library file, because DLL file is the source code files compiled after the file, do not see the source code,
So you can only call not allowed to change

Five main principles

1. Single Duty principle SRP (Responsibility Principle)

refers to the function of a class to be single , not all-encompassing. Like a person, the distribution of work can not be too much, otherwise busy all the way, but the efficiency is not high up.

2. Open Closure principle OCP (open-close Principle)

A module should be open in terms of extensibility and should be closed in terms of change . Example inheritance. For example: A network module, the original service-only function, and now to join the client function,
Then, without modifying the service-side function code, you can increase the client function implementation code, which requires that at the beginning of the design, the server should be separated from the client, the public part of the abstraction.

3. Richter replacement principle (the Liskov Substitution Principle LSP)

subclasses should be able to replace the parent class and appear anywhere the parent class can appear. For example: The company engaged in annual party, all employees can participate in the lottery, so whether the old staff or new employees,
Whether it is the headquarters staff or the outside staff, should be able to participate in the lottery, or the company will not be harmonious.

4. Dependency Inversion principle (the Dependency inversion Principle DIP)

depending on the abstraction, the upper layer relies on the lower layer . Suppose B is a lower module, but B needs to use the function of a,
At this time, B should not directly use the specific class in a: Instead, a abstract interface should be defined by B, and a to implement this abstract interface, B only use this abstract interface: this will achieve
For the purpose of dependency inversion, B also relieves dependency on a, which in turn is an abstract interface dependent on the B definition. It is difficult to avoid relying on the lower module through the upper module, if B is also directly dependent on the implementation of a, then it may cause cyclic dependence. A common problem is that when compiling a module, you need to include the CPP file directly into the B module, while compiling B will also include the CPP file of a directly.

5. Dimitri (Law of Demeter)

Also known as the least knowledge principle (Least knowledge Principle shorthand LKP), which means that an object should have as little knowledge of other objects as possible and not talk to strangers.

The English shorthand is: LoD. The Dimitri Law can be simply said: Talk is only to your immediate friends. For Ood, it is also explained in the following ways: a software entity should interact with as few interactions as possible with other entities. Each software unit has only minimal knowledge of the other units, and is limited to the software units that are closely related to the unit.
The purpose of the Dimitri law is to reduce the coupling between classes. Since each class minimizes its reliance on other classes, it is easy to make the functional modules of the system independent of each other, and there is no (or little) dependency between them.
The Dimitri law does not want the class directly to establish direct contact. If there really is a need to establish a connection, it is also hoped that it will be conveyed through its friend class. Therefore, one of the possible consequences of applying the Dimitri law is that there are a large number of intermediary classes in the system that exist solely to convey the mutual invocation relationship between classes-which in some way increases the complexity of the system.
It is interesting to study the design pattern of the façade mode (facade) and the mediation Mode (mediator), which are examples of Dimitri Law application.

Object-oriented abstract class, interface, class Library, Five Principles

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.