C# 程序员参考--属性教程
|
本教程展示属性是如何成为 C# 编程语言必不可少的一个组成部分的。它阐释如何声明和使用属性。 此教程包括两个示例。第一个示例展示如何声明和使用读/写属性。第二个示例演示抽象属性并展示如何在子类中重写这些属性。 示例 1本示例展示一个 Person 类,它有两个属性:Name (string) 和 Age (int)。两个属性都是读/写属性。 软件开发网 www.mscto.com // person.cs
using System;
class Person
{
private string myName ="N/A";
private int myAge = 0;
// Declare a Name property of type string:
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}
// Declare an Age property of type int:
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");
// Create a new Person object:
Person person = new Person();
// Print out the name and the age associated with the person:
Console.WriteLine("Person details - {0}", person);
// Set some values on the person object:
person.Name = "Joe";
person.Age = 99;
Console.WriteLine("Person details - {0}", person);
// Increment the Age property:
person.Age = 1;
Console.WriteLine("Person details - {0}", person);
}
}
输出Simple Properties Person details - Name = N/A, Age = 0 Person details - Name = Joe, Age = 99 Person details - Name = Joe, Age = 100 软件开发网 www.mscto.com 代码讨论
示例 2下面的示例展示如何定义抽象属性。抽象属性声明不提供属性访问器的实现。本示例演示如何在子类中重写这些属性。 该示例包含三个文件。在“属性”示例中,这些文件被编译为单个编译;但在此教程中,每个文件都单独进行编译,且产生的程序集会由下一个编译引用:
若要编译该示例,请使用命令行: csc abstractshape.cs shapes.cs shapetest.cs 这样将生成可执行文件 shapetest.exe。
[1] [2]
软件开发网 www.mscto.com 文件 1:abstractshape.cs 该文件声明包含 double 类型的 // abstractshape.cs
// compile with: /target:library
// csc /target:library abstractshape.cs
using System;
public abstract class Shape
{
private string myId;
public Shape(string s)
{
Id = s; // calling the set accessor of the Id property
}
public string Id
{
get
{
return myId;
}
set
{
myId = 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 = " string.Format("{0:F2}",Area);
}
}
代码讨论
文件 2:shapes.cs 下面的代码展示 Shape 的三个子类,并展示它们如何重写 Area 属性来提供自己的实现。 // shapes.cs
// compile with: /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
{
// Given the side, return the area of a square:
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
{
// Given the radius, return the area of a circle:
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
{
// Given the width and height, return the area of a rectangle:
return myWidth * myHeight;
}
}
}
文件 3:shapetest.cs 软件开发网 www.mscto.com 以下代码展示一个测试程序,它创建若干 Shape 派生的对象,并输出它们的面积。 // shapetest.cs
// compile with: /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);
}
}
}
输出Shapes Collection Square #1 Area = 25.00 Circle #1 Area = 28.27 Rectangle #1 Area = 20.00 [1] [2] 软件开发网 www.mscto.com
源代码网推荐 源代码网供稿. |
