【发布时间】:2015-12-13 19:57:56
【问题描述】:
实际上不是 2 个构造函数,而是 3 个,但其中 2 个被重载了。
-
这是我之前看过的一些教程的原创。
public Animal() { this.height=0; this.weight=0; this.name="No Name"; this.sound = "No Sound"; } public Animal(double height, double weight, string name, string sound) { this.height=height; this.weight=weight; this.name=name; this.sound = sound; } -
我在软件开发课程中记得这一点,所以我想知道这样写是否有意义,如果不是这样,如果我的代码上有什么问题?
public Animal(double height=0, double weight=0, string name="No Name", string sound="No Sound") { this.height=height; this.weight=weight; this.name=name; this.sound = sound; }
因为我真的不知道我是否可以像这样在类构造函数中放置默认值。
【问题讨论】:
-
编译一下就知道了。你有什么问题? :)
-
你可以试一试......它会告诉你是否可以将默认值放入构造函数中...... :)
-
我的意思是它有效,但我想知道它是否没有任何副作用,比如以这种方式或类似的方式这样做是否不是一个坏习惯。