// 导入必要的命名空间
using System;
using System.Collections.Generic;

// 定义抽象类 Shape
public abstract class Shape { }

// 定义正方形类 Square,继承自 Shape
public class Square : Shape
{
   // 定义正方形的边长属性
   public double Side { get; set; }
}

// 定义圆形类 Circle,继承自 Shape
public class Circle : Shape
{
   // 定义圆形的半径属性
   public double Radius { get; set; }
}

// 定义三角形类 Triangle,继承自 Shape
public class Triangle : Shape
{
   // 定义三角形的高度属性
   public double Height { get; set; }
}

// 定义程序类 Program
class Program
{
   // 定义 Main 方法,程序入口
   static void Main()
   {
      // 创建一个 List<Shape> 类型的 shapes 列表
      var shapes = new List<Shape>();

      // 向 shapes 列表中添加一个 Circle 对象、一个 Square 对象、一个 Triangle 对象和一个 nullSquare 对象
      shapes.Add(new Circle() { Radius = 7 });
      shapes.Add(new Square() { Side = 5 });
      shapes.Add(new Triangle() { Height = 4 });
      var nullSquare = (Square)null;
      shapes.Add(nullSquare);

      // 使用 foreach 循环遍历 shapes 列表中的每个元素
      foreach (var shape in shapes)
      {
         // 使用 switch 语句判断 shape 的类型并执行相应的操作
         switch (shape)                // 对 shape 变量进行类型和值判断
         {
            // 如果 shape 是 Circle 类型
            case Circle circle:        // 等价于 if(shape is Circle)
               Console.WriteLine($'This shape is a circle of radius { circle.Radius }');
               break;

            // 如果 shape 是 Square 类型,并且 square 的 Side 大于 10
            case Square square when square.Side > 10: // 仅匹配 Square 类型中满足条件的子集
               Console.WriteLine($'This shape is a large square of side { square.Side }');
               break;

            // 如果 shape 是 Square 类型
            case Square square:
               Console.WriteLine($'This shape is a square of side { square.Side }');
               break;

            // 如果 shape 是 Triangle 类型
            case Triangle triangle:    // 等价于 if(shape is Triangle)
               Console.WriteLine($'This shape is a triangle of side { triangle.Height }');
               break;

            // 注释掉的代码将引发编译错误,因为在 switch 语句中,类型检查条件只能出现在第一个 case 条件中
            // case Triangle triangle when triangle.Height < 5: // Compile error
               // Console.WriteLine($'This shape is a triangle of side { triangle.Height }');
               //break;

            // 如果 shape 为 null
            case null:
               Console.WriteLine($'This shape could be a Square, Circle or a Triangle');
               break;

            // 如果 shape 不满足以上条件
            default:
               throw new ArgumentException(
               message: 'shape is not a recognized shape',
               paramName: nameof(shape));
         }
      }
   }
}

该代码定义了一个抽象类 Shape 和三个继承自 Shape 的子类 Square、Circle 和 Triangle。代码中创建了一个 List 类型的 shapes 列表,并向其中添加了一个 Circle 对象、一个 Square 对象、一个 Triangle 对象和一个 nullSquare 对象。

在 foreach 循环中,对 shapes 列表中的每个元素进行类型判断,并根据不同的类型进行不同的操作。具体判断的过程如下:

  1. 如果 shape 是 Circle 类型,执行以下代码块: Console.WriteLine($'This shape is a circle of radius { circle.Radius }'); 输出该 shape 是一个半径为 circle.Radius 的圆。

  2. 如果 shape 是 Square 类型,并且 square 的 Side 大于 10,执行以下代码块: Console.WriteLine($'This shape is a large square of side { square.Side }'); 输出该 shape 是一个边长大于 10 的大正方形。

  3. 如果 shape 是 Square 类型,执行以下代码块: Console.WriteLine($'This shape is a square of side { square.Side }'); 输出该 shape 是一个边长为 square.Side 的正方形。

  4. 如果 shape 是 Triangle 类型,执行以下代码块: Console.WriteLine($'This shape is a triangle of side { triangle.Height }'); 输出该 shape 是一个边长为 triangle.Height 的三角形。

  5. 如果 shape 为 null,执行以下代码块: Console.WriteLine($'This shape could be a Square, Circle or a Triangle'); 输出该 shape 可能是一个正方形、圆形或三角形。

  6. 如果 shape 不满足以上条件,抛出一个 ArgumentException 异常,提示 'shape is not a recognized shape'。

C# 代码示例:使用 switch 语句判断形状类型并输出信息

原文地址: http://www.cveoy.top/t/topic/ckWq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录