详解C#中的属性和属性的使用

2019-12-30 11:39:54于丽


((Employee)m1).Name = "Mary";

在此例中,Cube 和 Square 这两个类实现抽象类 Shape,并重写它的抽象 Area 属性。注意属性上 override 修饰符的使用。程序接受输入的边长并计算正方形和立方体的面积。它还接受输入的面积并计算正方形和立方体的相应边长。


abstract class Shape
{
  public abstract double Area
  {
    get;
    set;
  }
}

class Square : Shape
{
  public double side;

  public Square(double s) //constructor
  {
    side = s;
  }

  public override double Area
  {
    get
    {
      return side * side;
    }
    set
    {
      side = System.Math.Sqrt(value);
    }
  }
}

class Cube : Shape
{
  public double side;

  public Cube(double s)
  {
    side = s;
  }

  public override double Area
  {
    get
    {
      return 6 * side * side;
    }
    set
    {
      side = System.Math.Sqrt(value / 6);
    }
  }
}

class TestShapes
{
  static void Main()
  {
    // Input the side:
    System.Console.Write("Enter the side: ");
    double side = double.Parse(System.Console.ReadLine());

    // Compute the areas:
    Square s = new Square(side);
    Cube c = new Cube(side);

    // Display the results:
    System.Console.WriteLine("Area of the square = {0:F2}", s.Area);
    System.Console.WriteLine("Area of the cube = {0:F2}", c.Area);
    System.Console.WriteLine();

    // Input the area:
    System.Console.Write("Enter the area: ");
    double area = double.Parse(System.Console.ReadLine());

    // Compute the sides:
    s.Area = area;
    c.Area = area;

    // Display the results:
    System.Console.WriteLine("Side of the square = {0:F2}", s.side);
    System.Console.WriteLine("Side of the cube = {0:F2}", c.side);
  }
}

输出:


  Enter the side: 4
  Area of the square = 16.00
  Area of the cube = 96.00

  Enter the area: 24
  Side of the square = 4.90
  Side of the cube = 2.00
 

注:相关教程知识阅读请移步到c#教程频道。