在主函数public static void main(String[] args) 里只吊用了不带参数的构造函数Flower(),
当一类中有多个构造函数时,对象的构造只匹配其对应类型的构造函数。
若写为 Flower x = new Flower( “***” ) 就会执行你想显示的结果了
当一类中有多个构造函数时,对象的构造只匹配其对应类型的构造函数。
若写为 Flower x = new Flower( “***” ) 就会执行你想显示的结果了
解决方案 »
- 求FLEX日程管理源代码
- JDBC 关闭连接的问题
- ssh框架中的问题
- javabean输数函数
- Spring中使用注解的方式如何实现"Inner Bean的机制"?
- 请求帮助,JAVA连接数据库问题
- Tomcate启动时出错,并跳入调试界面.
- Struts2登录成功跳转页面,登录失败返回一个json,jsp页面要怎么解析?
- 文章发布功能有内容,有图片SSM框架mysql数据库
- springMVC+spring,mvc扫描的controller无法使用spring扫描的service实现类
- 【求教】关于EJB修改Oracle库表的问题 小弟初学 请多执教~在线等!~
- 问一个webservice的问题:请问我在webservice的服务器端能否取到客户端的IP地址?
为何可以显示
Flower(int petals)
{
petalCount = petals;
System.out.println("Construtor w/ int arg only , petalCount = " + petals);
} 的结果哪!
这个也是带类型的构造函数哪!
this("hi",47); //Flower(String s , int petals)
this(petals); //Flower(int petals)
Flower(String ss)
{
System.out.println("Construtor w/ String arg only , s= " + ss);
s = ss;
}的结果不能显示!Flower(int petals)
{
petalCount = petals;
System.out.println("Construtor w/ int arg only , petalCount = " + petals);
} 的结果能显示!两个都是带型参的构造函数,为什么会出现不同的结果?
Flower x = new Flower();
首先调用Flower();
就是这个
Flower()
{
this("hi",47);
System.out.println("default constructor (no args)");
}
然后this("hi",47);
会调用
Flower(String s , int petals)
{
this(petals);
this.s = s;
System.out.println("string & int args");
} this(petals)会调用下面的构造方法
Flower(int petals)
{
petalCount = petals;
System.out.println("Construtor w/ int arg only , petalCount = " + petals);
} 所以只有
Flower(String ss)
{
System.out.println("Construtor w/ String arg only , s= " + ss);
s = ss;
传递字符串的没有被调用。
this.s = s;这句不是调用Flower(String ss) 吗?
谢谢!!!!!