I. abstract class:
Abstract classes are special classes, but they cannot be instantiated. In addition, they have other characteristics of the class. It is important that abstract classes can include abstract methods, which are not supported by common classes. Abstract METHODS can only be declared in abstract classes and do not contain any implementations. The Derived classes must overwrite them. In addition, an abstract class can be derived from an abstract class. It can overwrite the abstract methods of the base class or overwrite them. If not, its derived class must overwrite them.
Using system;
Using system. collections. generic;
Using system. text;
Using system. threading;
Namespace apptest
{
Class demo_abstract
{
Static void main (string [] args)
{
Double len = 2.5;
Double wid = 3.0;
Double rad = 4.1;
Rectangle arect = new rectangle ();
Arect. length = len;
Arect. width = wid;
Circle acirc = new circle (rad );
Console. writeline ("area of rect is: {0}", arect. area ());
Console. writeline ("area of circ is: {0}", acirc. area ());
// Reflect the advantages of abstract, and different functions are available for instances that are not needed
// The result is the same as that of the rectangle instance.
Shape = new rectangle (len, wid );
Console. writeline ("area of shape is: {0}", shape. area ());
Thread. sleep (3*1000); // pause for 3 seconds to view the result
}
}
// Graphic
Abstract class shape // abstract base class, which cannot be instantiated
{
Public const double pi = 3.14; // constant
Protected double x, y; // Private, can inherit Variables
Public shape () // default constructor
{
X = y = 0;
}
Public shape (double x, double y) // constructor with Parameters
{
This. x = x;
This. y = y;
}
Public abstract double area (); // specifies the abstract method, which must be overloaded.
}
// Square
Class rectangle: shape
{
Public rectangle (): base (){}
Public rectangle (double x, double y): base (x, y) {}// use the base class Constructor
Public override double area () // function overload
{
Return (x * y );
}
Public double length // attribute: rectangular length
{
Get
{
Return x;
}
Set
{
If (value> 0) {x = value ;}
}
}
Public double width // attribute: rectangle width
{
Get
{
Return y;
}
Set
{
If (value> 0) {y = value ;}
}
}
}
// Elliptic
Class ellips tutorial e: shape
{
Public ellipse (double x, double y): base (x, y) {}// use the constructors of the base class shape
Public override double area () // function overload
{
Return pi * x * y;
}
}
// Circle
Class circle: ellipse
{
Public circle (double r): base (r, 0) {}// use the base class ellipse Constructor
Public override double area () // function overload
{
Return pi * x;
}
}
}
Ii. interface:
The interface is of reference type. It is similar to a class and has three similarities with the abstract class:
1. It cannot be instantiated;
2. contains an unimplemented method statement;
3. The derived class must implement unimplemented methods. The abstract class is an abstract method, and the interface is all members (not only the methods include other members );
In addition, interfaces have the following features:
In addition to methods, interfaces can also contain attributes, indexers, and events, and these members are defined as common. It cannot contain any other Members, such as constants, fields, constructors, destructor, and static members. A class can directly inherit multiple interfaces, but can only inherit one class (including abstract classes ).
Interface isampleinterface
{
Void samplemethod ();
}
Class implementationclass: isampleinterface
{
// Explicit interface member implementation:
Void isampleinterface. samplemethod ()
{
// Method implementation.
}
Static void main ()
{
// Declare an interface instance.
Isampleinterface obj = new implementationclass ();
// Call the member.
Obj. samplemethod ();
}
}