java构造函数中的this_关于构造函数中使用this的问题
第一種方式:
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; ???????? } ??????? ???? }
這兩種方式哪個(gè)更好些?還是說只是習(xí)慣性的問題?
總結(jié)
以上是生活随笔為你收集整理的java构造函数中的this_关于构造函数中使用this的问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python post请求参数为list
- 下一篇: java做条形图_使用Jfree实现吧条