Summary:
- what vb.net tutorial Are interfaces and abstract classes
- Interfaces and abstractions c# tutorial Class is "the product of software engineering"
- Concrete class -- > abstract class -- > interface: more and more abstract, internal implementation python basic tutorial There are fewer and fewer things
- Abstract classes are incompletely implemented logic java basic course Class of collection (can have fields and non-public members, which represent "concrete logic")
- Abstract classes are born for reuse: specifically as base classes sql tutorial It also has decoupling function
- Encapsulate deterministic, open uncertain, and defer implementation to appropriate subclasses
- Interfaces are "classes" that do not implement logic at all ("pure virtual classes"; only function members; all members are public (implicit public))
- The interface is born for decoupling: "high cohesion, low coupling", which is convenient for unit testing;
- The interface is a "contract", which has long been known to industrial production (where there is division of labor, there must be cooperation, and where there is cooperation, there must be a contract)
- They cannot be instantiated, but can only be used to declare variables and reference instances of concrete class es
abstract class
Examples: the "abstract class" and the "open / close principle" for the base class
abstract class Student{ abstract public void Study(); }
Abstract class: a class whose function members are not fully implemented; (abstract, and must not be private)
There is no implementation, no method body and no curly braces in the abstract function;
Abstract classes cannot be instantiated;
Since it cannot be instantiated, what is the function?
1. Make the base class and let the derived class implement it;
2. You can use the variables of the parent class to refer to the instances of the child class;
Abstract methods are also called pure virtual methods in some programming languages;
Open / close principle
The principle of opening and closing is a natural pair with the abstract class;
Don't always modify the code of a class if it's not for fixing bug s or adding new functions:
-
We should encapsulate some inconvenient, stable, fixed and definite members;
-
Those that may change are declared as abstract members and left to subclasses to implement;
example:
Both cars and trucks have stop methods, which are put into a base class;
abstract class Vehicle{ public void Stop(){ Console.WriteLine("Stopped!") } //This violates the opening and closing principle. If you want to add a new type of car, you must modify the code; public void Run(string type){ if(type == "car"){ Console.WriteLine(Car is running...); }else if(type == "truck"){ Console.WriteLine("Truck is running..."); } } //Change to public virtual void Run(){ Console.WriteLine("Vehicle is running..."); } //Change to public abstract void Run(); } class Car:Vehicle{ public override void Run(){ Console.WriteLine("Car is running..."); } } class Truck:Vehicle{ // public override void Run(){ Console.WriteLine("Truck is running..."); } } //Add a racing car without moving the methods in the base class, which conforms to the opening and closing principle class RaceCar:Vehicle{ //The override keyword is also used to implement abstract classes public override void Run(){ Console.WriteLine("RaceCar is running..."); } } class Program{ static void Main(){ //The parent class variable references a child class instance Vehicle v = new Car();//Here v only the stop method can be called. There are two solutions //1. Using virtual method v.Run();//The result here is Car is running Vehicle v2 = new Truck(); v2.Run();//The result here is Truck is running } }
All members of an abstract class are abstract;
//Pure abstract class abstract class VehicleBase{ abstract public void Stop(); abstract public void Fill(); abstract public void Run(); } //Partial abstract class abstract class Vehicle:VehicleBase{ public override void Stop(){ Console.WriteLine("Stopped!") } public override void Fill(){ Console.WriteLine("Stopped!") } } //car implements the run method class Car:Vehicle{ public override void Run(){ Console.WriteLine("Car is running..."); } }
//Pure abstract classes, in C#, are interfaces
//The interface must be pure abstract and public
interface IVehicle{ void Stop(); void Fill(); void Run(); } //Partial abstract class abstract class Vehicle:IVehicle{ //Implementation interface public void Stop(){ Console.WriteLine("Stopped!") } //Implementation interface public void Fill(){ Console.WriteLine("Stopped!") } //There is another one that has not been implemented. It is also written as abstract public abstract public void Run(); }
Reference: Liu tiemeng's C# basic teaching video