在主函数public static void main(String[] args) 里只吊用了不带参数的构造函数Flower(),
当一类中有多个构造函数时,对象的构造只匹配其对应类型的构造函数。
若写为 Flower x = new Flower( “***” ) 就会执行你想显示的结果了
当一类中有多个构造函数时,对象的构造只匹配其对应类型的构造函数。
若写为 Flower x = new Flower( “***” ) 就会执行你想显示的结果了
解决方案 »
- 关于一个Filter无效的问题
- Static 变量为什么进不到background 里??
- struts的model疑惑
- 这种情况用哪种java web server比较好?
- hibernate.cfg.xml文件应放在什么位置
- 一个关于数据库连接池的问题
- 菜鸟呼救 如何在JBuilder中编写和调试 Tomcat中运行的JSP 和 JavaBean程序
- jb8的帮助那位大哥有,给提供一下,刚整了一个JB8安装后发觉帮助少了很多
- 请问怎样在jboss中启动tomcat,怎么设置?谢谢!
- hibernate4.0不再需要annotation了吗?
- 【求教】关于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) 吗?
谢谢!!!!!