关于构造的一个问题,始终得不到正确答案,请高手帮忙??

chl326 2002-01-09 10:58:12
whick statements are true regarding the creation of a default constructor??
the default constructor invokes the no-parameter constructor of the superclass

the default constructor initializes the instance variables delared in the class
...全文
2260 53 打赏 收藏 转发到动态 举报
写回复
用AI写文章
53 条回复
切换为时间正序
请发表友善的回复…
发表回复
hahaha88 2002-01-26
  • 打赏
  • 举报
回复
弟兄们,2个都对,哈哈哈哈哈
the default constructor invokes the no-parameter constructor of the superclass
对(请看我上面的帖子)

the default constructor initializes the instance variables delared in the class
对。
我把J2SE1。3编译出来的class反编译,发现编译器把对instance field的初始化工作
放在(编译进了)所有的构造器之中(包括缺省构造器),并放在靠前面的位置。
oldcat0076 2002-01-25
  • 打赏
  • 举报
回复
两个都错误,因为只要你实例化子类,无论是用什么构造函数,都会先执行父类的不带参数构造函数,并且初始化实例变量。
yanchang 2002-01-25
  • 打赏
  • 举报
回复
1正确!
night_knight 2002-01-25
  • 打赏
  • 举报
回复
class A
{
public A(int x)
{}
}
public class B extends A
{
public B()
{}
public A(int x)
{}
public static void main(String args[])
{
new B();
new B(5);
}
}
只要sub_class的constructor没有显示的声明,它都会调用super_class的默认的无参数的constructor,所以上面这段代码会出错,必须再定义一个无参数的constructor
xiekun 2002-01-16
  • 打赏
  • 举报
回复
第一个对,
如果super class定义了parameter constructor,
那么在sub class中,不定义constructor,那么编译器将会帮你加上一个default constructor,这时系统会提示出错
javalearner 2002-01-15
  • 打赏
  • 举报
回复
不明白的人可以看看----java 语言入门---里面有详解,o'relly出版社,中国电力出版社翻译的,
javalearner 2002-01-15
  • 打赏
  • 举报
回复
两句话都正确,顺序是一前一后
wangtaoyy 2002-01-14
  • 打赏
  • 举报
回复
这段代码
public class T{
int i = 2;
{
System.out.println("init i = " + i);
i++;
}
public T(){
System.out.println(i);
}
public T(String nothing){
System.out.println(i);
}
public static void main(String[] args){
new T();
new T("");

}
}
结果为:
init i = 2
3
init i = 2
3
表明成员变量初始化在任何构造器之前
所以只有A对

bjhz 2002-01-14
  • 打赏
  • 举报
回复
后面的对。
缺省构造函数是指没有参数的构造函数.
1.第一个错是因为缺省构造函数不一定会调用父类的无参构造函数,(在你显示的调用了父类的其他函数时就不再调用无参构造函数了。
2.第二个对是因为,从OOP概念上说,构造函数就是用来初始化实例变量的,不管是不是缺省构造函数.
////////////////////////////////////
对于这段代码
public class CTest
{
int i=2;

{
System.out.println(i);
i=5;
System.out.println(i);
}

CTest(){
System.out.println(i);
i=8;
System.out.println(i);
}

public static void main(String s[])
{
new CTest();
}
}
它确实可以运行,java和c++有很多细节地方不同,在C++中,类的成员变量不可以在声明时直接赋值,但JAVA中却可以在定义一个成员变量时顺便指定它的初值,还有,C++是不可以有在任何函数之外的代码的,而java中却可以,不知道这是不是一个漏洞.

yuanqingfei 2002-01-13
  • 打赏
  • 举报
回复
swdfsdf
hahaha88 2002-01-11
  • 打赏
  • 举报
回复
嘻嘻。。。我再仔细想一想。。。(我想好了)冤枉啊!!!!!哈哈

大家再看一看我举的例子(我再写一遍):
public class CTest
{
int i=2;

{
System.out.println(i);
i=5;
System.out.println(i);
}

CTest(){
System.out.println(i);
i=8;
System.out.println(i);
}

public static void main(String s[])
{
new CTest();
}
}

输出:
2
5
5
8

- 这说明“非default constructor”是在“int i=2;"之后被执行的
- 但确实存在”default constructor”是在“int i=2;"之前被执行的“
这种可能性,我再想一想怎么证明/证伪。。。嘻嘻。。。暂时用反证法:
假设JVM要为“非default constructor” 和 “default constructor”
安排不同的执行顺序,这样有什么特殊的好处呢?嘻嘻。。。
0legend 2002-01-11
  • 打赏
  • 举报
回复
if ur class has a unaided constructor ,and hope the user can use the class to
create an instance:
new ClassName()
then Java will create an default constructor(no Param) .
(not original text)
//come from Core Java 2,Volume I :Fundamentals
Writer : Cay S.Horstmann && Gray Cornell
0legend 2002-01-11
  • 打赏
  • 举报
回复
the default constructor initializes the variables to default value
//come from Core Java 2,Volume I :Fundamentals
Writer : Cay S.Horstmann && Gray Cornell
chl326 2002-01-11
  • 打赏
  • 举报
回复
the default constructor initializes the instance variables delared in the class
这句话在sl275里说过,肯定对。。。
chl326 2002-01-11
  • 打赏
  • 举报
回复
http://go2.163.com/chl886/qq35.gif
这道题是我昨天的考试题
我选的de,对了
怎么解释!!!
hahaha88 2002-01-11
  • 打赏
  • 举报
回复
上面这个例子不是非常严格,因为我自己写了CTest(){。。。},所以
它只是一个no-parameter constructor,而不是default constructor了,
但我认为真正的default constructor也是符合上述执行顺序的。
hahaha88 2002-01-11
  • 打赏
  • 举报
回复
我来试试,哈哈哈哈哈

1 the default constructor invokes the no-parameter constructor of
the superclass

因为你是不能书写default constructor的,所以你无法在其中指定用superclass中的哪一个
constructor,但是子类又必需调用一个父类的构造器,所以它肯定会去调用
superclass的no-parameter/null constructor。如果这时父类中不存在
no-parameter/null constructor,就会编译出错。

default constructor并不完全等同于no-parameter/null constructor,
default constructor是当你自己没有写任何构造器时,编译器自动生成的那个
no-parameter/null constructor。当你自己写了任何一个构造器后,这时就不存在
default constructor了(否则就有可能同时存在2个no-parameter/null constructor,
就乱了,嘻嘻)

下面这个是不能编译的:
class Father{
public Father(int i){}
}

class Son extends Father{
}



2 the default constructor initializes the instance variables delared
in the class
不对
instance variables的初始化工作是在任何构造器执行之前(也是在任何非静态初始化块被执行
之前)完成的。例子如下:

public class CTest
{
int i=2;

{
System.out.println(i);
i=5;
System.out.println(i);
}

CTest(){
System.out.println(i);
i=8;
System.out.println(i);
}

public static void main(String s[])
{
new CTest();
}
}

输出:
2
5
5
8

嘻嘻,Java很好嘛,谁说不好了?! 哈哈哈哈哈
mahongxi 2002-01-11
  • 打赏
  • 举报
回复
我认为如果是在C++中1,2都不适当。
何谓default constructor ?class A m;就是了,所以缺省构造函数一定是无参的了??
不知我理解的对不对?但在C++中“如果你没有定义构造函数,C++会为你定义一个”这句话只是从“概念“上讲,实际编译到底有没有合成出一个构造函数还要看具体情况。

在C++中,DELAULT构造函数不会初始化每个实例。

《深度探索C++对象模型》P47
C++新手一般有两个常见的误解:

1、任何CLASS如果没有定义DEFAULT构造函数,就会被合成出一个来。
2、编译器合成出来的DEFAULT 构造函数会明确设定“CLASS内每个DATE MEMBER的默认值”

如你所见,没有一个是真的!

chl326 2002-01-10
  • 打赏
  • 举报
回复
今天上午刚考完轼
第一个错
第二个对
yiyo2000_2001 2002-01-10
  • 打赏
  • 举报
回复
a
加载更多回复(33)

23,407

社区成员

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

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