Ⅰ. Abstract class
1. Abstract classes, abstract methods
Abstract class: Typically, there are abstract methods in it, and the class is preceded by a definition of an abstract
Abstract method: Defines only the name of the method and does not define the contents of the method
2. Why use abstract classes, abstract methods?
All for the sake of inheritance.
3. How to define abstract classes, abstract methods?
abstract + class name, abstract+ Method Name
4. Features:
?. Abstract classes cannot be instantiated ;
?. the function of abstract method is to provide a uniform interface for subclasses, and there is only declaration in the abstract aspect and no implementation;
?. Once a class inherits an abstract class, it must implement the implementation of all the abstract methods in the abstract class;
public abstract class person {public abstract void A (); public abstract void B (); }
Person
public class C:P Erson {public override void A () { Console.WriteLine ("China"); } public override void B () { Console.WriteLine ("Nihao"); } public static void Main () { c me = new C (); Me. A (); } }
class C:P Erson
?. an abstract class can have non-abstract methods, but once a class has an abstract method, the class is an abstract class;
?. the abstract method is declared with a ; end, no need to use {};
?. The abstract method does not need to be declared as a virtual method at the time of overwrite;
Ⅱ. Interface
Interface: can be regarded as a more strict abstract class, when it is an interface, it can only be an abstract method, but not a non-abstract method
1, how to define an interface?
Interface + class name
2, how to define the method inside the interface?
eg
void A ();
You can also add the parameter void A (string name);
Note: You must not write this, void B () {};//cannot write this, because all of the interfaces are defined only by the method name, and no specific implementation process
4. Features:
?. Interfaces are used to implement multiple inheritance, a class can have only one parent class, but may inherit from multiple interfaces class A:b,c
(Can inherit this interface class Animal:interface1, also can inherit another one by one interface class Animal:interface1,interface2)
?. Interface is a special kind of abstract class, it is more strict in the rules, the inside of the method can only be abstract method
?. When defining an interface, it is generally the default, that is, the interface + class name, preceded by the public abstract, when defining the method in the interface, it is only necessary to write the return type + method name, because the default is public abstract
?. All the members inside the interface are public.
?. When a class inherits from an interface, it must complete the implementation of all the methods in that interface.
?. Interfaces cannot contain fields, constructors, static variables, or constants, etc.
5. Where is the interface used?
Interface is multi-inheritance, the role of the interface is equivalent to a factory to produce sockets, provide a model, the model does not provide concrete methods of implementation, concrete implementation process, just define a model.
6. Why should I use an interface?
is to inherit, unify the norm.
Interface Interface1 { // interface can only be used in abstract way void A (); void B (string Name); // The string name parameter can be written }
Interface1
class Animal:interface1 { // A class inherits an interface, it is necessary to complete all the override in the interface, must override public override void A () {} public override< /span> void B () {} public Animal () {Console.WriteLine ( " I'm an animal! ); } }
Animal:interface1 (Inheritance interface)
Ⅲ. What is the difference between an abstract class and an interface?
Difference:
1. Abstract class : There are abstract methods, there are non-abstract methods;
Interface: is the more strict meaning of the abstract method, which must be all abstract methods;
2. Abstract class: The members inside the abstract class can be private, public, protected protected;
Interface: All the members inside are public;
Shared
1. A class inherits an abstract class or an interface, and it must override all methods in the abstract class/interface.
C#[abstract class, interface]