代码入下:
class Wind
{
public string Color { get; set; }
public int Speed { get; set; } static void Main(string[] args)
{
Wind northWind = new Wind{ Color = "Blue",Speed=10};
Wind southWind = new Wind{ Color = "Red", Speed = 5 }; Console.WriteLine("north wind's speed:{0}",northWind.Speed);
Console.ReadKey();
}
问题:
1.这里 public string Color { get; set; } get和set什么也没有做吗?不应该写 return一个值,value=一个值么?
2.Wind northWind = new Wind{ Color = "Blue",Speed=10}; 这句实现了给northWind赋了2属性值,是怎么实现的?谢谢!
class Wind
{
public string Color { get; set; }
public int Speed { get; set; } static void Main(string[] args)
{
Wind northWind = new Wind{ Color = "Blue",Speed=10};
Wind southWind = new Wind{ Color = "Red", Speed = 5 }; Console.WriteLine("north wind's speed:{0}",northWind.Speed);
Console.ReadKey();
}
问题:
1.这里 public string Color { get; set; } get和set什么也没有做吗?不应该写 return一个值,value=一个值么?
2.Wind northWind = new Wind{ Color = "Blue",Speed=10}; 这句实现了给northWind赋了2属性值,是怎么实现的?谢谢!
这是一个虚属性,就好像是接口里面的方法,int GetValue(),什么都没有。只有在用的时候实现它。
public int GetValue()
{
return 1;
}
同理:属性也是一样的。
就像Int32和int,没有什么区别
你这里用的是Public 实际上就已经失去了使用属性的意义。
Color = "Blue",Speed=10;
因为你定义的属性是public 那么就可以直接赋值。在这里他其实就相当予你定义的两个个变量.
刚才查了一下,这个才是虚属性:
public abstract class Base
{
public abstract int Count
{
get;
}
public int Compute()
{
return Count;
}
}
public class Derived:Base
{
public override int Count
{
get {return 5;}
}
}
public interface IListSource
{
// 摘要:
// 获取表示集合是否是 System.Collections.IList 对象集合的值。
//
// 返回结果:
// 如果集合是 System.Collections.IList 对象集合,则为 true;否则,为 false。
bool ContainsListCollection { get; } // 摘要:
// 从不实现 System.Collections.IList 本身的对象返回可以绑定到数据源的 System.Collections.IList。
//
// 返回结果:
// 对象中可以绑定到数据源的 System.Collections.IList。
IList GetList();
}