当前位置: 首页 > news >正文

windows C#-定义抽象属性

以下示例演示如何定义抽象属性。 抽象属性声明不提供属性访问器的实现,它声明该类支持属性,而将访问器实现留给派生类。 以下示例演示如何实现从基类继承抽象属性。

此示例由三个文件组成,其中每个文件都单独编译,产生的程序集由下一次编译引用:

  • abstractshape.cs:包含抽象 Area 属性的 Shape 类;
  • shapes.cs:Shape 类的子类;
  • shapetest.cs:测试程序,用于显示一些 Shape 派生对象的区域;

若要编译该示例,请使用以下命令:

csc abstractshape.cs shapes.cs shapetest.cs

这将创建可执行文件 shapetest.exe。

示例

此文件声明 Shape 类,该类包含 double 类型的 Area 属性。

// compile with: csc -target:library abstractshape.cs
public abstract class Shape
{private string name;public Shape(string s){// calling the set accessor of the Id property.Id = s;}public string Id{get{return name;}set{name = value;}}// Area is a read-only property - only a get accessor is needed:public abstract double Area{get;}public override string ToString(){return $"{Id} Area = {Area:F2}";}
}

属性的修饰符放置在属性声明中。 例如:

public abstract double Area

声明抽象属性时(如本示例中的 Area),只需指明哪些属性访问器可用即可,不要实现它们。 在此示例中,仅 get 访问器可用,因此该属性是只读属性。

下面的代码演示 Shape 的三个子类,并演示它们如何替代 Area 属性来提供自己的实现。

// compile with: csc -target:library -reference:abstractshape.dll shapes.cs
public class Square : Shape
{private int side;public Square(int side, string id): base(id){this.side = side;}public override double Area{get{// Given the side, return the area of a square:return side * side;}}
}public class Circle : Shape
{private int radius;public Circle(int radius, string id): base(id){this.radius = radius;}public override double Area{get{// Given the radius, return the area of a circle:return radius * radius * System.Math.PI;}}
}public class Rectangle : Shape
{private int width;private int height;public Rectangle(int width, int height, string id): base(id){this.width = width;this.height = height;}public override double Area{get{// Given the width and height, return the area of a rectangle:return width * height;}}
}

下面的代码演示一个创建若干 Shape 派生对象并输出其区域的测试程序。

// compile with: csc -reference:abstractshape.dll;shapes.dll shapetest.cs
class TestClass
{static void Main(){Shape[] shapes ={new Square(5, "Square #1"),new Circle(3, "Circle #1"),new Rectangle( 4, 5, "Rectangle #1")};System.Console.WriteLine("Shapes Collection");foreach (Shape s in shapes){System.Console.WriteLine(s);}}
}
/* Output:Shapes CollectionSquare #1 Area = 25.00Circle #1 Area = 28.27Rectangle #1 Area = 20.00
*/
http://www.lryc.cn/news/500736.html

相关文章:

  • ERROR: KeeperErrorCode = NoNode for /hbase/master
  • Deepin 23 踩坑记
  • mysql笔记——索引
  • 考研数据结构——简答题总结
  • Qt Creator 里面设置MSVC 为 utf-8
  • Java阶段三06
  • Helm安装Mysql8主从复制集群
  • 嵌入式基础:Linux C语言:Day7
  • Tablesaw封装Plot.ly实现数据可视化
  • RAG与Embedding:现代NLP的核心技术
  • 力扣每日一题 - 1812. 判断国际象棋棋盘中一个格子的颜色
  • Map 那些事儿
  • GCP Case:MountKirk Games
  • [创业之路-187]:《华为战略管理法-DSTE实战体系》-1-从UTStarcom的发展历程,如何辩证的看企业初期发展太顺利中的危机
  • 高级数据结构-树状数组
  • LeetCode279. 完全平方数(2024冬季每日一题 27)
  • Scala 隐式转换
  • K8S命令部署后端(流水线全自动化部署)
  • Ubuntu中配置交叉编译工具的三条命令的详细研究
  • 【PyQt5教程 二】Qt Designer 信号与槽的使用方法及PyQt5基本小部件说明
  • 编程语言中接口(Interface)介绍
  • 算法学习之贪心算法
  • 【jvm】垃圾回收的优点和原理
  • YOLO系列发展历程:从YOLOv1到YOLO11,目标检测技术的革新与突破
  • 深入浅出:序列化与反序列化的全面解析
  • word实践:正文/标题/表图等的共用模板样式设置
  • Blender中使用BlenderGIS插件快速生成城市建筑模型
  • 【单元测试】单元测试的重要性
  • Codeforces Round 992 (Div. 2)
  • el-table一键选择全部行,切换分页后无法勾选