C # basic syntax Learning (6 ),

Source: Internet
Author: User

C # basic syntax Learning (6 ),

Abstract class and seal class

  When defining a class, you can use the abstract keyword to declare that this class is abstract. The abstract class cannot be instantiated, that is, the instance of the abstract class cannot be created. Abstract Classes Act as the base classes of other classes.

The concept of a seal class is just opposite to that of an abstract class. This class cannot act as the base class of another class. The concept of "sealing" is that it is not allowed to derive other classes from this class. When defining a class, use the sealed keyword to indicate that the class is sealed.

The following two classes are declared: an abstract class and a seal class.

 

1 public abstract class Shape // abstract class 2 {3 /// 4} 5 6 public sealed class Rectangle: Shape // seal class 7 {8 9}

 

If you attempt to create an instance of an abstract class or derive another class from a seal class in violation of the abstract class and seal class rules, a compilation error will occur.

Abstract classes can contain abstract methods. Abstract METHODS only have method signatures and no method bodies. The syntax for declaring an abstract method is as follows:

Access modifier abstract return value type method name (parameter list );

Abstract METHODS can only be included in abstract classes, and the derived classes must override abstract methods in the parent class. Abstract METHODS cannot contain method bodies or even braces {}. They must be followed by semicolons directly after the parameter list.

1 public abstract class Shape 2 {3 public abstract double calculateArea (); 4} 5 6 public sealed class Triangle: Shape 7 {8 private double edge, high; 9 public Triangle (double e, double h) 10 {11 edge = e; 12 high = h; 13} 14 15 public override double calculateArea () 16 {17 return edge * high/2; 18} 19} 20 public sealed class Rectangle: Shape21 {22 private double length, width; 23 public Rectangle (double l, double w) 24 {25 length = l; 26 width = w; 27} 28 29 public override double calculateArea () 30 {31 return length * width; 32} 33} 34 35 public sealed class Circle: Shape36 {37 private double radius; 38 public Circle (double r) 39 {40 radius = r; 41} 42 43 public override double calculateArea () 44 {45 return Math. PI * radius; 46} 47} 48 49 static void Main (string [] args) 50 {51 Shape s; 52 53 s = new Triangle (10, 15 ); 54 printArea (s); 55 s = new Rectangle (10, 15); 56 printArea (s); 57 s = new Circle (10); 58 printArea (s ); 59 60 Console. readLine (); 61} 62 63 static void printArea (Shape s) 64 {65 Console. writeLine ("this image is:" + s. getType (). toString (); 66 Console. writeLine ("the image area is:" + s. calculateArea (). toString (); 67} 68}

Running result

The figure is ShapeExample. the area of the Triangle image is 75. The image is ShapeExample. the area of the Rectangle image is: 150. The image is ShapeExample. the area of the image Circle is 314.159265358979 square meters.

 

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.