C # syntax exercise (12): class [4]-abstract class and abstract member, seal class and seal Member

Source: Internet
Author: User
Abstract classes cannot be directly instantiated:
 
Using system; abstract class myclass {} class program {static void main () {/* abstract class cannot be directly instantiated. the following error occurs */myclass OBJ = new myclass (); console. readkey ();}}

  

However, abstract classes can be instantiated by subclass:

 
Using system; abstract class parent {} class child: parent {} class program {static void main () {parent OBJ = new child (); console. writeline (obj. tostring (); // child console. readkey ();}}

  

Abstract METHODS can only be included in abstract classes:

Using system; abstract class parent {/* the abstract method is an implicit virtual method, but it cannot be modified using static or virtual */public abstract void Method1 (); /* abstract classes can contain non-Abstract METHODS */Public void method2 () {console. writeline ("method2");}/* can even contain static methods */public static void method3 () {console. writeline ("method3") ;}} class child: parent {/* use override */Public override void Method1 () {console. writeline ("Method1") ;}} class program {static void main () {parent. method3 (); // method3 child. method3 (); // method3 parent OBJ = new child (); obj. method1 (); // Method1 obj. method2 (); // method2 console. readkey ();}}

  

A derived class must implement the abstract method of the parent class, unless it is also an abstract class:

Using system; abstract class parent {public abstract void Method1 ();} abstract class Child1: parent {} class child2: Child1 {public override void Method1 () {console. writeline ("Method1") ;}} class program {static void main () {parent OBJ = new child2 (); obj. method1 (); // Method1 console. readkey ();}}

  

Abstract attributes:

 
Using system; abstract class shape {public abstract int area {Get ;}} class rectangle: Shape {private int fwidth, fheight; Public rectangle (int w, int H) {fwidth = W; fheight = H;} public override int area {get {return fwidth * fheight ;}} class program {static void main () {rectangle rect = new rectangle (20, 10); console. writeline (rect. area); // 200 console. readkey ();}}

  

Sealing class and sealing members:

Using system; Class Parent {Public Virtual void Method1 () {console. writeline ("Method1");} Public Virtual void method2 () {console. writeline ("method2");} Public Virtual void method3 () {console. writeline ("method3") ;}} class Child1: parent {/* the following two methods can continue to overwrite */Public override void Method1 () {console. writeline ("New Method1");} public override void method2 () {console. writeline ("New method2");}/* this method has been disabled by sealed, that is, the feature of the virtual function */Public sealed override void method3 () {console. writeline ("New method3");}/* This class uses sealed as a sealed class and cannot have a derived class. Regardless of its internal nature, */sealed class child2: child1 {public override void Method1 () {console. writeline ("New Method1");} public sealed override void method2 () {console. writeline ("New method2") ;}} class program {static void main () {child2 OBJ = new child2 (); obj. method1 (); // New Method1 obj. method2 (); // New method2 obj. method3 (); // new method3 console. readkey ();}}
 
   
 

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.