_c# tutorial on abstract usage in C #

Source: Internet
Author: User
Tags abstract constructor modifier

Abstract can be used to modify classes, methods, attributes, indexers, and time, not including fields. A class decorated with abstrac that can only serve as the base class for other classes, cannot be instantiated, and that the members of the abstract decoration must be implemented in all derived classes, not partial implementations, or compile exceptions. Such as:

Using System;
Namespace ConsoleApplication8 
{ 
class program 
{ 
static void Main (string[] args) 
{ 
Bclass b = New Bclass (); 
B.M1 (); 
} 
Abstract class AClass 
{public 
abstract void M1 (); 
public abstract void M2 (); 
} 
Class Bclass:aclass 
{public 
override void M1 () 
{ 
throw new notimplementedexception (); 
} 
public override void M2 () 
//{ 
//throw new NotImplementedException (); 
//} 
} 

Abstract classes have the following features:

Abstract classes have the following characteristics:

1, an abstract class cannot be instantiated, but it can have an instance constructor, and whether the class can be instantiated depends on having an instantiated permission (the permissions for the abstract class are abstract, no instantiation),
The compiler also provides a default constructor, even if no constructor is provided;

2, abstract classes can contain abstract methods and accessors;

3, abstract class cannot use sealed adornment, sealed meaning cannot be inherited;

4, all non-abstract classes that inherit from the abstract class must implement all of the abstract members, including methods, attributes, indexers, events;

The method of abstract modification has the following characteristics:

1, the abstract method is the virtual method (implied);

2, abstract methods can only be declared in abstract classes;

3, because the abstract method simply declares and does not provide an implementation, the method ends with only a semicolon, no method body, i.e. no curly braces;

public abstract void MyMethod();

The 4,override-Modified overlay method provides implementations and can only be a member of a non-abstract class;

5, you cannot use virtual or static adornments on the declaration of an abstract method. That is, it cannot be static, and because abstract is already virtual, there is no need to emphasize in virtual.
Abstract properties, although similar in behavior to abstract methods, have the following differences:

1, you cannot apply the abstract modifier on a static property;

2, abstract attributes overwrite overrides in non-abstract derived classes, using the override modifier;

Abstract classes and Interfaces:

1, the abstract class must provide the implementation of all interface members;

2, an abstract class that inherits an interface can map the member of an interface to a bit abstract method.

Such as:

Interface I {void M (); 
Abstract class C:i {public abstract void M (); 
Abstract class instance://Abstract_keyword.cs//abstract class using System; 
The abstract class BaseClass//abstract class {protected int _x = 100;//The abstract class can define fields, but not abstract fields, and there is no such argument. 
protected int _y = 150; public BaseClass (int i)//You can define an instance constructor that is invoked only by derived non-abstract classes; 
The constructor is explicitly provided here, and the compiler will no longer provide a default constructor. 
{Fielda = i; 
Public BaseClass () {} private int fielda; 
public static int FIELDSA = 0; public abstract void Abstractmethod (); 
Abstract method Public abstract int X. {get;}//abstract property public abstract int Y {get;} Public abstract string Idxstring {get; set;}//Abstract property public abstract char This[int i] {get;}//Abstract indexer} class Derivedc 
Lass:baseclass {private string idxstring; 
private int fieldb; If no parameterless constructor is defined in the base class, but there is a constructor with parameters,//So the derived class has to call the constructor of the base class, otherwise the compilation error public derivedclass (int p): base (p)//here: Base (P) 
can be omitted because the base class defines the default parameterless constructor {fieldb = p; 
public override string idxstring//Overwrite Reset property {get {return idxstring; } set {idxstring =Value 
Override Char This[int i]//overwrite rewrite indexer {get {return idxstring[i];} 
public override void Abstractmethod () {_x++; 
_y++;
 public override int X//Overwrite overriding property {get {return _x + 10;
 Override int Y//Overrides overriding property {get {return _y + 10;
 } static void Main () {derivedclass o = new DerivedClass (1);
 O.abstractmethod ();
Console.WriteLine ("x = {0}, y = {1}", o.x, O.Y); }
}

The above is a small set to introduce the abstract of C # in the use of detailed explanation, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.