Abstract class and interface

xiaoxiao2021-04-04  236

note! The abstractions and interfaces I will be based herein are C #. I. Abstract class: Abstract class is a special class, but it is not possible to be instantiated; in addition to this, there are other features of the class; important, the abstract class can include abstract methods, which is the ordinary class. The abstract method can only be declared in the abstract class and does not contain any implementation, and the derived class must cover them.

In addition, the abstract class can be derived from an abstract class, and the abstract method of the base class can also be covered. If it is not overwritten, its derived class must cover them.

Abstract class instance

1 Public Abstract Class A 2 {3 private int? Num = null; 4 5 public int? Num 6 {7 get {return num;} 8 set {num = value;} 9} 1011 public virtual int? Getnum () 12 { 13 return num; 14} 1516 public void setnum (int? N) 17 {18 this.num = n; 19} 2021 public abstract void f (); 22} 2324 public Abstract Class B: a25 {26 // public override void F () 27 // {28 // Throw new Exception ("The Method or Operation IS Not Implement."); 29 //} 3031 Public Abstract Void E (); 32} 3334 Public Class C: B35 {36 Private C: B () 37 {38} 3940 Public Override Void E () 41 {42 Throw new Exception ("The Method or Operation IS Not Implement); 43} 4445 Public Override Void F () 46 {47 C C = New C ( ); 48 throw new exception ("The Method or Operation IS Not Implement). "); 49} 50}

Second, the interface: The interface is a reference type, similar to the class, more and the abstract class, so that many people have a relatively blurred differences in abstract classes and interfaces. There are three points from the abstract class: 1, can not be instantiated; 2, including unrealized methods declaration; Methods include other members);

In addition, the interface has the following characteristics: In addition to the method, the interface can contain attributes, indexers, events, and these members are defined as public. In addition, any other member cannot be included, for example: constant, domain, constructor, destructor, static member. A class can directly inherit multiple interfaces, but only directly inherit a class (including abstract classes). Interface instance

1 public delegate void del (); 2 public interface t = null; 5 6 INT A 7 {8 get; 9} 1011 void test (); 12 Event del Ondel; 13 int this [int INDEX] 14 {15 get; 16 set; 17} 18}

note! There is also another kind of category that all constructor is marked as private. This class cannot be instantiated. It is strictly that it cannot be instantiated outside the class, which can be instantiated in such internal The way can be used to implement a single piece design mode). Note that such a class cannot be inherited as a base class.

Third, the use of abstract classes and interfaces: Abstract classes are used to partially implement a class, and then the user performs different extensions and improvements in demand; the interface is only a specific or specification of a behavior. Abstract classes provide universal implementation features between all implementations of components; interfaces create functions used between large-scale total objects. The abstract class is mainly used for close objects; and the interface is suitable for unrelated classes to provide universal functions. The abstract class is mainly used to design large functional units; the interface is used to design small and concise function blocks. For example: Window form can be designed with an abstraction, you can put public operations and properties into an abstraction, allowing the form and dialog to inherit from this abstraction class, and expand and improve according to their own needs. Print operations can be provided as an interface to each form that requires this function, because the content of the form is different, according to their own requirements to realize their own print function. When printing, only the interface is used to call, and it doesn't matter if it is to print.

转载请注明原文地址:https://www.9cbs.com/read-131720.html

New Post(0)