Abstract classes and interfaces in asp.net C #

Source: Internet
Author: User
Tags abstract constructor

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 ();
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.