第一种方式:
class Motorcycle { public int driverIntensity; public string driverName;
public void SetDriverName(string name) { this.driverName = name; }
public void PopAWheely() { for (int i = 0; i <= driverIntensity; i++) { Console.WriteLine("Yeeeeeee Haaaaaeewww!"); } } public Motorcycle(int intensity) { if(intensity>10) { intensity = 10; } driverIntensity = intensity; } public Motorcycle(int intensity,string name):this(intensity) { driverName = name; } }
第二种方式:
class Motorcycle { public int driverIntensity; public string driverName;
public void SetDriverName(string name) { this.driverName = name; }
public void PopAWheely() { for (int i = 0; i <= driverIntensity; i++) { Console.WriteLine("Yeeeeeee Haaaaaeewww!"); } }
public Motorcycle(int intensity) : this(intensity, "") { Console.WriteLine("In ctor taking an int"); }
public Motorcycle(int intensity, string name) { Console.WriteLine("In master ctor "); if (intensity > 10) { intensity = 10; } driverIntensity = intensity; driverName = name; } }
这两种方式哪个更好些?还是说只是习惯性的问题?
第一種好。:this中的方法,應該 最好是 小於等於 前面方法中的參數。
Java转过来的么……完全是Java的写法。
class Motorcycle
{
public int DriverIntensity { get; private set; }
public string DriverName { get; set; }
public Motorcycle(int intensity) : this(intensity, "") { }
public Motorcycle(int intensity, string name)
{
this.DriverIntensity = intensity;
this.DriverName = name;
}
}
你把driverIntensity做成公开字段的话,那你的 if (intensity > 10) 还有什么用啊,随便改啊。
不是java转过来的,第二种方法是我从C#与.net4高级程序设计中转过来的,第一种方法是我根据这个类再结合C#图解教程中介绍的怎么用this串联构造函数中的思想改编的,就是想问问哪种编程方式比较好~话说又是你帮我解决问题,哈哈,本人菜鸟一个..