【C#】学习笔记 abstract、virtual、interface使用的一些栗子

0
8

上🌰

abstract (C# Reference)

Abstract classes(抽象类) have the following features:

  • An abstract class cannot be instantiated.①

  • An abstract class may contain abstract methods and accessors. ②

  • It is not possible to modify an abstract class with thesealedmodifier because the two modifiers have opposite meanings.Thesealedmodifier prevents a class from being inherited and theabstractmodifier requires a class to be inherited. ③

  • A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors. ④

①抽象类无法被实例化

  

②抽象类可以包含抽象方法和访问器

③抽象类无法用sealed修饰,因为这俩是相反的。sealed是用来阻止被继承的,而abstract是需要一个类来继承的。

④一个非抽象派生类必须实现继承来的所以抽象方法和访问器。

 1 namespace AbstractDemo
 2 {
 3     abstract class Shape
 4     {
 5         public abstract int GetArea();
 6 
 7     }
 8     class Square : Shape
 9     {
10         int side;
11         public Square(int n)
12         {
13             side = n;
14         }
15         //简写: public Demo(int n) => side = n;
16 
17         public override int GetArea()
18         {
19             return side * side;
20         }
21         //简写: public override int GetArea() => side * side;
22     }
23 }

 1 namespace AbstractDemo
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             Square square = new Square(4);
 8             System.Console.WriteLine(square.GetArea()); // 16
 9         }
10     }
11 }

Abstract methods(抽象方法) have the following features:

  • An abstract method is implicitly a virtual method.①

  • Abstract method declarations are only permitted in abstract classes.②

  • Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no curly braces ({ }) following the signature.③

  • It is an error to use thestaticorvirtualmodifiers in an abstract method declaration.④

Abstract properties behave like abstract methods, except for the differences in declaration and invocation syntax.

  • It is an error to use theabstractmodifier on a static property.⑤

  • An abstract inherited property can be overridden in a derived class by including a property declaration that uses theoverridemodifier.⑥

①抽象方法是隐式的虚方法

②抽象方法只能在抽象类中进行声明。

③因为抽象方法声明却没有具体的实现,所以是没有方法体的“{ }”。以“;”结尾

④抽象方法声明时不能用static或virtual来修饰

⑤abstract不能用来修饰静态属性

⑥override用来修饰抽象属性派生类中的属性声明。


// virtual
1 using System; 2 namespace InterfaceDemo1 3 { 4 public class ConcreteRegularPolygon 5 { 6 public int NumberOfSides { get; set; } 7 public int SideLength { get; set; } 8 9 public ConcreteRegularPolygon(int sides, int length) 10 { 11 NumberOfSides = sides; 12 SideLength = length; 13 } 14 public double GetPerimeter() 15 { 16 return NumberOfSides * SideLength; 17 } 18 // 这里定义了一个virtual函数 19 public virtual double GetArea() 20 { 21 throw new NotImplementedException(); 22 } 23 } 24 }

 1 namespace InterfaceDemo1
 2 {
 3     public class Square : ConcreteRegularPolygon
 4     {
 5         public Square(int length) : base(4, length)
 6         {
 7 
 8         }
 9         // 在这里进行覆盖重写
10         public override double GetArea()
11         {
12             return SideLength * SideLength;
13         }
14     }
15 }

abstract

抽象方法必须在抽象类中。

继承它就必须override抽象方法,假如不写会报错。

interface

同样也没有具体的实现。

在调用接口的同时要实现方法。

暂时先写这么多。

<

发布回复

请输入评论!
请输入你的名字