用中文逐行注释以下代码:using System;using SystemCollectionsGeneric;public abstract class Shape public class Square Shape public double Side get; set; public class Circle Shape public double Radius get;
该代码定义了一个抽象类Shape和三个继承自Shape的子类Square、Circle和Triangle。代码中创建了一个List
在foreach循环中,对shapes列表中的每个元素进行类型判断,并根据不同的类型进行不同的操作。具体判断的过程如下:
-
如果shape是Circle类型,执行以下代码块: Console.WriteLine($"This shape is a circle of radius { circle.Radius }"); 输出该shape是一个半径为circle.Radius的圆。
-
如果shape是Square类型,并且square的Side大于10,执行以下代码块: Console.WriteLine($"This shape is a large square of side { square.Side }"); 输出该shape是一个边长大于10的大正方形。
-
如果shape是Square类型,执行以下代码块: Console.WriteLine($"This shape is a square of side { square.Side }"); 输出该shape是一个边长为square.Side的正方形。
-
如果shape是Triangle类型,执行以下代码块: Console.WriteLine($"This shape is a triangle of side { triangle.Height }"); 输出该shape是一个边长为triangle.Height的三角形。
-
如果shape为null,执行以下代码块: Console.WriteLine($"This shape could be a Square, Circle or a Triangle"); 输出该shape可能是一个正方形、圆形或三角形。
-
如果shape不满足以上条件,抛出一个ArgumentException异常,提示"shape is not a recognized shape"。
原文地址: http://www.cveoy.top/t/topic/jgEa 著作权归作者所有。请勿转载和采集!