关于constructor的问题

printblocks 2014-03-16 05:50:56
代码如下:

public class Flower {
int petalCount = 0;
String s = "initial value";
Flower(int petals) {
petalCount = petals;
print("Constructor w/ int arg only, petalCount= "
+ petalCount);
}
Flower(String ss) {
print("Constructor w/ String arg only, s = " + ss);
s = ss;
}

Flower(String s, int petals) {
this(petals);
//! this(s); // Can’t call two!
this.s = s; // Another use of "this"
print("String & int args");
}
Flower() {
this("hi", 47);
print("default constructor (no args)");
}
void printPetalCount() {
//! this(11); // Not inside non-constructor!
print("petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.printPetalCount();
}
}

运行结果:
Constructor w/ int arg only, petalCount= 47
String & int args
default constructor (no args)
petalCount = 47 s = hi

我想请问为什么string ss 的内容不显示出来? 是不是因为第一个constructor的参数已经设定成int的关系?
...全文
131 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
-江沐风- 2014-03-17
  • 打赏
  • 举报
回复
引用 6 楼 printblocks 的回复:
感谢5楼, 终于明白了.
这些都是基础,任何一本Java书都有的!可以找本书看看
printblocks 2014-03-17
  • 打赏
  • 举报
回复
感谢5楼, 终于明白了.
-江沐风- 2014-03-16
  • 打赏
  • 举报
回复
public class Flower
{
	int petalCount = 0;
	String s = "initial value";

	Flower(int petals)
	{
		petalCount = petals;
		System.out.println("Constructor w/ int arg only, petalCount= "
				+ petalCount);
	}

	Flower(String ss)
	{
		System.out.println("Constructor w/ String arg only, s = " + ss);
		s = ss;
	}

	Flower(String s, int petals)
	{
		this(petals);     //第三步:由于petals变量是int类型的,所以调用构造方法Flower(int petals);
		this.s = s; 
		System.out.println("String & int args");
	}

	Flower()
	{
		this("hi", 47);     //第二步:由于是两个参数,调用Flower(String s, int petals);
		System.out.println("default constructor (no args)");
	}

	void printPetalCount()
	{
		System.out.println("petalCount = " + petalCount + " s = " + s);
	}

	public static void main(String[] args)
	{
		Flower x = new Flower();     //第一步:调用无参数构造方法Flower();
		x.printPetalCount();         //第四步:调用方法;
	}
}
printblocks 2014-03-16
  • 打赏
  • 举报
回复
又仔细回去看了下书, 好像发现问题所在了: 第一个含int的构造函数能被调用是不是因为第一句就赋值了,所以后面能被调用. 第二个含String的构造函数不被调用是不是因为第一句没有赋值,而是在第二句才赋值,所以不行?
printblocks 2014-03-16
  • 打赏
  • 举报
回复
感谢二楼的回答,但是还是有点不明白. Flower(String ss) { print("Constructor w/ String arg only, s = " + ss); s = ss; } Flower(String s, int petals) { this(petals); //! this(s); // Can’t call two! this.s = s; // Another use of "this" print("String & int args"); } 这一句不是调用String s么? 那么为什么第一个含int参数的构造函数能被调用?
-江沐风- 2014-03-16
  • 打赏
  • 举报
回复
参数匹配,,
Chopin_Chen 2014-03-16
  • 打赏
  • 举报
回复
Flower x = new Flower();会调用你的默认构造函数 在你默认构造函数里面调用了含有两个参数的构造函数 Flower() { this("hi", 47); print("default constructor (no args)"); } 在含有两个参数的构造函数中只调用了参数为int型的构造函数,你的String ss没有调用到,所有不会有输出。 Flower(String s, int petals) { this(petals); //! this(s); // Can’t call two! this.s = s; // Another use of "this" print("String & int args"); }

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧