Abstract classes and abstract methods
1. Writing specification:
By adding the abstract keyword to the class, an abstract class is added, and the abstract keyword is used before a method to become an abstraction . (Abstract methods cannot have implementation methods, directly appended with semicolons)
Cases:
Abstract class Fruit // abstract class { public abstractvoid Growinarea (); // abstract Method }
2, the abstract class notes:
1) When a class becomes an abstract class, it cannot be used as an object and cannot be new.
2) Abstract methods must be placed in abstract classes (but abstract methods are not necessarily abstract)
3) No sub-class can run normally
4) methods inside the abstract class can only be called in this class, and if called in other classes, it is necessary to pass the subclass
3. Precautions for abstract methods :
1) Abstract method cannot write content (cannot create object)
2) must override in subclass to run
3) There are several abstract methods, you must write a few in the subclass, otherwise error
4. The method of overriding the abstract base class in a subclass:
class apple:fruit{ publicoverridevoid fangfa () { Console.WriteLine ("tasty! " ); }}
5. Refer to the subclass object of the abstract class in the function:
Abstract class abstract class variable name = new inherits from the specific subclass name of this abstract class ();
Cases:
New Apple (); F.fangfa (); // Print result is tasty!
Note: Classes that contain abstract methods must be abstract classes, but methods in an abstract class are not necessarily abstract methods.
Interface
When there is a relationship between a class and multiple classes, simply using a parent-child relationship can not be expressed, in order to solve this problem, introduced the concept of interface, so that a class can correspond to multiple interfaces .
An interface can be regarded as a "pure" abstract class, and all its methods are abstract methods.
1, the definition and use of the interface:
Define the interface with the keyword interface . Cases:
PublicIterface iswim{voidSwim ();Cannot write segment, method, Swim () in interfaceequivalent to an abstract method that needs to be overridden in a class that inherits the interface } public iterface ifood{ void Cook ();
The implementation interface can be used with the same syntax as inheritance. Cases:
1Defines an abstract class that enables duck to inherit from an abstract class, implementing two interfaces2PublicAbstractClassBird3{4PublicAbstractvoidFly ();5}6PublicClassDuck:bird,iswim,ifood7{8Implementing the Iswim Interface9PublicvoidSwim ()10{Console.WriteLine ("Swim");12}13Implementing the Ifood Interface14PublicvoidCook ()15{Console.WriteLine ("Cook");17}18Implementing abstract methods in abstract class bird19PublicOverridevoidFly ()20{Console.WriteLine ( "fly "22 }23}
The use of interfaces requires a formula: the interface type name Variable name = new implements the type name of the interface (), such as:
New Duck ();d. Fly ();d. Cook ();d. Swim ();
Object-oriented Fundamentals (vi)