从实际意义来分析是使用属性还是方法
比如 汽车
他的 大小 颜色 发动机的马力 都可作为他的属性
但是 他运行时的速度 最好不要使用 Speed 属性 而使用GetSpeed()方法
但是他的最高时速 又一般使用属性 MaxSpeed 来处理使用属性还是方法有时候很难区分的,没有一个绝对的 :)
比如 汽车
他的 大小 颜色 发动机的马力 都可作为他的属性
但是 他运行时的速度 最好不要使用 Speed 属性 而使用GetSpeed()方法
但是他的最高时速 又一般使用属性 MaxSpeed 来处理使用属性还是方法有时候很难区分的,没有一个绝对的 :)
解决方案 »
- List<T>和collection<T> 的优点缺点?
- C#实时读取网页上的更新数据
- 如果解决WIN7下:应用程序试图执行安全策略不允许的操作
- 请问,如何在一个文本文件中的最后添加亲的文本内容,并保存?
- C#winform程序引用office组件的clickonce发布
- 菜鸟提问,关于时间相减的...
- 如何用DataSet表示业务实体
- 关于动态控件数组的卸载
- !!~@@最后的10分 穷死了 可问题真的非解决不可啊 求各位给我答案好吗? c# 问题
- datagridview DataGridViewComboBoxCell 下拉修改后点击其他单元格,修改的值变回item[0]的值
- 代码太长我想分两段写,怎么换行?
- 一个学习方向的问题-菜!
此教程包括两个示例。第一个示例展示如何声明和使用读/写属性。第二个示例演示抽象属性并展示如何在子类中重写这些属性。示例 1
本示例展示一个 Person 类,它有两个属性:Name (string) 和 Age (int)。两个属性都是读/写属性。// 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);
}
}