C # basics -- abstract

Source: Internet
Author: User

Abstract modifiers can be used with classes, methods, attributes, indexers, and events. Abstract modifier in the class declaration to indicate that a class can only be the base class of other classes. Members marked as abstract or included in an abstract class must be implemented through classes derived from the abstract class.

Abstract classes have the following features: abstract classes cannot be instantiated; abstract classes can contain abstract methods and abstract accessors; abstract classes cannot be modified using the sealed modifier, because sealed will make the abstract classes unable to be inherited; A non-abstract class derived from an abstract class must include all the inherited abstract methods and the actual implementation of the abstract accessors.

Use the abstract modifier in a method or attribute declaration to indicate that a method or attribute does not contain an implementation. Abstract methods have the following features: abstract methods are implicit virtual methods. They can only be declared using abstract methods in abstract classes. Because abstract method declarations do not provide actual implementation, there is no method body, the method ends with a semicolon and there is no braces ({}) after the signature. This method is provided by override, which is a non-abstract class member; it is incorrect to use the static or virtual modifier in the abstract method declaration.

  1. // The abstract class cannot be sealed or static. // abstract sealed class TestClass {}// abstract static class TestClass {}
  2. Abstract class BaseClass {protected int _ x = 100; protected int _ y = 150; public abstract void AbstractMethod (); public abstract int X {get ;} public abstract int Y {get;} // static members cannot be marked as abstract/public static abstract void StaticMethod ();} class DerivedClass: BaseClass {public override void AbstractMethod () {_ x ++; _ y ++;} public override int X {get {return _ x + 10 ;}} public override int Y {get {return _ y + 10;} public static void Main () {DerivedClass c = new DerivedClass (); c. abstractMethod (); Console. writeLine ("x = {0}, y = {1}", c. x, c. y); Console. writeLine (); Console. writeLine ("Press Enter to close this window. "); Console. readLine ();}}

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.