ylbtech-LanguageSamples-Porperties(属性)

ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Porperties(属性)

 

1.A,示例(Sample) 返回顶部

“属性”示例

本示例演示属性为何是 C# 编程语言必不可少的一个组成部分。它演示了如何声明和使用属性。有关更多信息,请参见属性(C# 编程指南) 。

安全说明

提供此代码示例是为了阐释一个概念,它并不代表最安全的编码实践,因此不应在应用程序或网站中使用此代码示例。对于因将此代码示例用于其他用途而出现的偶然或必然的损害,Microsoft 不承担任何责任。

在 Visual Studio 中生成并运行“属性”示例

  1. 在“解决方案资源管理器”中,右击“Person”项目并单击“设为启动项目”。

  2. 在“调试”菜单上,单击“开始执行(不调试)”。

  3. 对 shapetest 重复前面上述步骤。

从命令行生成并运行“属性”示例

  1. 使用“更改目录”命令转到“person”目录。

  2. 键入以下命令:

    csc person.cs
    person
  3. 使用“更改目录”命令转到“shapetest”目录。

  4. 键入以下命令:

    csc abstractshape.cs shapes.cs shapetest.cs
    shapetest
1.B,person 示例代码(Sample Code)返回顶部

1.B.1, person.cs

// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。

// person.cs
using System;
class Person
{
    private string myName ="N/A";
    private int myAge = 0;

    // 声明 string 类型的 Name 属性:
    public string Name
    {
        get 
        {
           return myName; 
        }
        set 
        {
           myName = value; 
        }
    }

    // 声明 int 类型的 Age 属性:
    public int Age
    {
        get 
        { 
           return myAge; 
        }
        set 
        { 
           myAge = value; 
        }
    }

    public override string ToString()
    {
        return "Name = " + Name + ", Age = " + Age;
    }

    public static void Main()
    {
        Console.WriteLine("Simple Properties");

        // 创建新的 Person 对象:
        Person person = new Person();

        // 打印出与该对象关联的姓名和年龄:
        Console.WriteLine("Person details - {0}", person);

        // 设置 Person 对象的某些值:
        person.Name = "Joe";
        person.Age = 99;
        Console.WriteLine("Person details - {0}", person);

        // 递增 Age 属性:
        person.Age += 1;
        Console.WriteLine("Person details - {0}", person);
    }
}
View Code

1.B.2,

1.B.EXE,

Simple Properties
Person details - Name = N/A, Age = 0
Person details - Name = Joe, Age = 99
Person details - Name = Joe, Age = 100
请按任意键继续. . .

1.B

1.B,shapetest 示例代码2(Sample Code)返回顶部

1.B.1, abstractshape.cs

// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。

// abstractshape.cs
// 编译时使用:/target:library
// csc /target:library abstractshape.cs
using System;

public abstract class Shape
{
   private string myId;

   public Shape(string s)
   {
      Id = s;   // 调用 Id 属性的 set 访问器
   }

   public string Id
   {
      get 
      {
         return myId;
      }

      set
      {
         myId = value;
      }
   }

   // Area 为只读属性 - 只需要 get 访问器:
   public abstract double Area
   {
      get;
   }

   public override string ToString()
   {
      return Id + " Area = " + string.Format("{0:F2}",Area);
   }
}
View Code

1.B.2, shapes.cs

// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。

// shapes.cs
// 编译时使用:/target:library /reference:abstractshape.dll
public class Square : Shape
{
   private int mySide;

   public Square(int side, string id) : base(id)
   {
      mySide = side;
   }

   public override double Area
   {
      get
      {
         // 已知边长,返回正方形的面积:
         return mySide * mySide;
      }
   }
}

public class Circle : Shape
{
   private int myRadius;

   public Circle(int radius, string id) : base(id)
   {
      myRadius = radius;
   }

   public override double Area
   {
      get
      {
         // 已知半径,返回圆的面积:
         return myRadius * myRadius * System.Math.PI;
      }
   }
}

public class Rectangle : Shape
{
   private int myWidth;
   private int myHeight;

   public Rectangle(int width, int height, string id) : base(id)
   {
      myWidth  = width;
      myHeight = height;
   }

   public override double Area
   {
      get
      {
         // 已知宽度和高度,返回矩形的面积:
         return myWidth * myHeight;
      }
   }
}
View Code

1.B.3, shaptest.cs

// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。

// shapetest.cs
// 编译时使用:/reference:abstractshape.dll;shapes.dll
public class TestClass
{
   public 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);
      }
         
   }
}
View Code

1.B.EXE,

Shapes Collection
Square #1 Area = 25.00
Circle #1 Area = 28.27
Rectangle #1 Area = 20.00
请按任意键继续. . .

1.B,

1.C,下载地址(Free Download)返回顶部

 

warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on 2015-01-01 23:40  ylbtech  阅读(378)  评论(0编辑  收藏  举报