关于constructor的一句话??

chl326 2002-01-08 08:03:41
the default constructor initializes the instance variables declared in the claaa
...全文
199 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
chl326 2002-01-09
  • 打赏
  • 举报
回复
抱歉,刚刚的例子有太多问题,你看看下面的。
class Super{
public Super(){
System.out.println("default");
}

public Super(int i){
System.out.println("none default");
}
}

public class Sub extends Super{


public Sub(){
super(3);
}

public static void main(String[] args)
{
Sub test = new Sub();
}

}

输出是:none default

chl326 2002-01-09
  • 打赏
  • 举报
回复
转帖
the default constructor invokes the no-parameter constructor of the superclasss说得不完全对。
public class Super{
public Super(){}

public Super(int i){
System.println(i);
}
}

public class Sub{

public Sub(){
Super(3);
}
}

0legend 2002-01-09
  • 打赏
  • 举报
回复
but the default constructor initializes the variables to default value
//come from Core Java 2,Volume I :Fundamentals
Writer : Cay S.Horstmann && Gray Cornell
wangtaoyy 2002-01-09
  • 打赏
  • 举报
回复
不对
instance variable不是在constructor中初始化的
反证:如果instance variable在Default constructor中初始化
那么如果使用其它constructor导致instance variable不被初始化


contributor 2002-01-09
  • 打赏
  • 举报
回复
Yes! if you doesn't use default constructor to create new instance. However, default constructor can still be used to create new instance even if it has nothing.
0legend 2002-01-09
  • 打赏
  • 举报
回复
thx.it means that it can do nothing and need nothing.
contributor 2002-01-09
  • 打赏
  • 举报
回复
I don't quit understand your question.
Default constructor can be empty when the class compiled.
0legend 2002-01-09
  • 打赏
  • 举报
回复
看来俺又错了,能在下面问个问题么?
Default constructor do need the instance variables initialized when the class compiled ?




contributor 2002-01-09
  • 打赏
  • 举报
回复
You are right!
Answer: C and E.
chl326 2002-01-09
  • 打赏
  • 举报
回复
请看http://go2.163.com/chl886/qq35.gif(关于constructor )
我认为选ce
各位怎么看???
binghuodao 2002-01-09
  • 打赏
  • 举报
回复
当然对
contributor 2002-01-09
  • 打赏
  • 举报
回复
Default constructor CAN initialize the instance variables declared in the class. However, it CANNOT do it if you doesn't write the expression of the assignment in the default constructor. Therefore, the statements is not 100% right, only partially.



0legend 2002-01-09
  • 打赏
  • 举报
回复
c & d
wangtaoyy 2002-01-09
  • 打赏
  • 举报
回复
有一个叫initialize的动作完成initialize instance variables 而不是
default constructor
0legend 2002-01-09
  • 打赏
  • 举报
回复
default constructor can initialize instance variables but when u use the
variable ,it will get the value u give it.
if u and the default constructor all don't initialize instance variables
how can u use the instance variables not initialized ?
wangtaoyy 2002-01-09
  • 打赏
  • 举报
回复
请看:
public class A
{
int b;
int a = 1
int c;
public A(int b){
this.b = b;
}
}

A test = new A(2);
没有default constructor但是test.a,和test.c都被正确初始化,
而且test.a不是default value .
0legend 2002-01-09
  • 打赏
  • 举报
回复
public class Sub extends Super{


public Sub(){
super(3);
}

public static void main(String[] args)
{
Sub test = new Sub();
}

}

// sub have no default constructor.
0legend 2002-01-08
  • 打赏
  • 举报
回复
找到个相关例子://括号内是和例子相关的
We give our class instance variables to support reading from and writing to (socket streams), and to (store details of the remote host to which we will connect0.

The constructor for our class (takes an IP address and a port number for a remote host and assigns them) to instance variables.

我想答案是肯定的了。
yhc0125 2002-01-08
  • 打赏
  • 举报
回复
关注
chl326 2002-01-08
  • 打赏
  • 举报
回复
the default constructor initializes the instance variables declared in the class..
这句话对不??

23,407

社区成员

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

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