C# 关键字Abstract

  • 您不能创建一个抽象类的实例。
  • 您不能在一个抽象类外部声明一个抽象方法。
  • 通过在类定义前面放置关键字 sealed,可以将类声明为密封类。当一个类被声明为 sealed 时,它不能被继承。抽象类不能被声明为 sealed。
1 namespace FirstCode.EX1
2 {
3     abstract public class Shape
4     {
5         abstract public int area();
6     }
7 }
 1 namespace FirstCode.EX1
 2 {
 3     public class Rectangle : Shape
 4     {
 5         public int width;
 6         public int length;
 7 
 8         public Rectangle(int a,int b)
 9         {
10             width =a;
11             length = b;
12         }
13         public override int area()=>width*length;
14 
15     }
16 }
 1 namespace FirstCode
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             Console.WriteLine("Hello World!");
 8 
 9             Rectangle rect1 = new Rectangle(2,3);
10             Console.WriteLine(rect1.area().ToString());
11         }
12     }
13 }

猜你喜欢

转载自www.cnblogs.com/YourDirection/p/12328968.html
今日推荐