Topic: 关于继承的问题?请大家帮忙!! |
Print this page |
1.关于继承的问题?请大家帮忙!! | Copy to clipboard |
Posted by: hamlet Posted on: 2005-09-25 10:27 class A { int i; A(int k) { this.i = k; } } class B extends A { public static void main(String args[]) { A a12 = new A(10); System.out.println(a12.i); } } 这是一个简单的类~运行时候有错误。。我只是实例化了父类对象A~难道子类也要写一个带参的构造函数吗?就是这里不理解~因为我没有实例化子类B~也不用上调用默认的无参构造函数,也就不用调用父类的无参构造函数啊 |
2.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: hamlet Posted on: 2005-09-25 10:31 为什么在父类里面加一个无参构造函数就没有问题呢 请高手指点小第啊。。。。。 |
3.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-09-25 10:40 Java only provides default constructor without parameter when you did not define any constructor. This is different than C++. C++ always provides a default constructor without parameter if you did not define one. |
4.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: hamlet Posted on: 2005-09-25 11:01 你的英文我读明白了 但是在原理上还是有点不明白啊 我实例化父类A的时候调用的是带参数的构造函数。 请高手在帮帮忙!!! |
5.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: ruoruowudi Posted on: 2005-09-25 13:25 class A { int i; A() { } A(int k) { this.i = k; } } class B extends A { public static void main(String args[]) { A a12 = new A(10); System.out.println(a12.i); } } 这样就对了。。 |
6.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: freelow Posted on: 2005-09-25 16:23 子类无条件继承父类的无参数的构造函数; 如果子类没有定义构造函数,它就继承父类的无参数构造函数作为自己的构造函数; 对于父类的带参数构造函数,子类可以通过在自己的构造函数中使用super来调用; 这个程序也可以改成: class A { int i; A(int k) { this.i = k; } } class B extends A { B(int m) { super; } public static void main(String args[]) { B a12 = new B(10); System.out.println(a12.i); } } OK! |
7.Re:关于继承的问题?请大家帮忙!! [Re: freelow] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-09-25 16:38 freelow wrote: 信口开河!!!!! Totally, 100% wrong!!!!! Constructors cannot be inherited, period!!!!! |
8.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-09-25 16:56 hamlet wrote: The problem of your code: 1) You did not define a constructor for B, compiler will define a default no-parameter constructor for you. 2) The compiler defined no-parameter constructor will auto-call superclass A no-parameter constructor. 3) The superclass A no-parameter constructor does not exist!!!! 4) Compile ERROR, big time!!!!!!!!!!!!!!!!!!! How to fix it, I give you three choices follows: solution #1, define a no-parameter constructor for A
solution #2, define a with-parameter constructor for B, which in turn calls with-parameter constructor for A
solution #3, define a no-parameter constructor for B, which in turn calls with-parameter constructor for A
They are all compilable, runnable, for illustrating concepts only. They are almost meaningless. |
9.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: freelow Posted on: 2005-09-26 11:36 Constructors cannot be inherited? Really? |
10.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-09-26 12:32 Read a book!!!! |
11.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: hamlet Posted on: 2005-09-26 13:00 知道了谢谢大家 |
12.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: freelow Posted on: 2005-09-26 16:40 子类可以继承父类的构造函数 java语言于面向对象程序设计 朱福喜 2002第一版 P97 书上的内容有误? |
13.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: wyql Posted on: 2005-09-26 17:23 构造函数的继承 子类可以继承父类的构造函数,构造函数的继承遵循以下原则 1 .子类无条件的继承父类的不含参数的构造函数; 2 . 如果子类没有自己的构造函数,则它将继承父类无参数的构造函数作为自己的构造函数;如果子类自己定义了构造函数,则在创建新对象时,它将先执行继承自父类的无参数构造函数,然后再执行自己的构造函数; 3 .对于父类的含参数的构造函数,子类可以通过在自己的构造函数中使用supper关键字来调用它,但这个调用语句必须是子类构造函数的第一个可执行语句。 <<java与面向对象程序设计教程>> 高等教育出版社 作者 : 印旻 |
14.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: panther Posted on: 2005-09-26 18:57 Post is deleted |
15.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-09-27 11:49 To freelow: I need to say I am very sorry. You did not 信口开河!!!!! To wyql and freelow: Thank you both for quoting these Chinese “books” to me. I can easily to prove them all wrong. It is those so-called books 信口开河!!!!! They also copy each other’s mistakes. I don’t know what to say to those so-called experts and authors. I will put my proof into my next post. Please bear with me… Thanks! freelow wrote: wyql wrote: |
16.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-09-27 12:09 This is a Java code. In class A, we have a no-param constructor, somehow, it is complicated. Our class B does not have constructors. Java compiler will create a default no-param constructor for B with empty body, the constructor of B should has no resemblance the one of class A no-param constructors. If my theory is proved, you all will know how 信口开河 they did. How will I prove it, easy, just disassemble the Java class files to see the assembly code. You can clearly to see the huge difference of A and B no-param constructors. You will know constructor cannot be inherited. What are followings? 1) My Java code 2) The dos window disassembly result for class A. 3) The dos window disassembly result for class B. If you have eyes, you can tell how信口开河 those so-called authors. I highly encourage you people to do and verify by your own Java Compiler. and Java disassemblers. My Java code
The dos window disassembly result for class A
The dos window disassembly result for class B
|
17.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-09-27 12:16 B's no-param constructor code only have 3 lines. A's no-param constructor code have 23 lines. Is there any possibility what those books said is true?? Please throw those so-called books into GC. Thanks! Sigh!!!!! |
18.Re:关于继承的问题?请大家帮忙!! [Re: panther] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-09-27 12:22 panther wrote: To panther Please don't put any trash comments here before you have done any research! Thanks! |
19.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: Jcat Posted on: 2005-09-27 18:06 从严格(any)意义上讲,构造方法是不能继承的。比如,父类Person有一个构造方法Person(String,int),不能说子类Student也自动有一个构造方法Student(String,int)。但是,这并不意味着子类不能调用父类的构造方法。 子类可以(must)用super来调用父类的构造方法。 注意:super不能在static环境中使用,因为它指的是“对象”(实例) (见《Java编程篇》电子工业出版社122页) 常见的一个应用:自定义异常 class JcatException extends Exception{ public JcatException(){ super(); } public JcatException(String msg){ super(msg); } |
20.Re:关于继承的问题?请大家帮忙!! [Re: Jcat] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-09-28 11:02 Jcat wrote: Corrections: 1) 从 any 意义上讲,构造方法是不能继承的。 2) 子类 must 调用父类的构造方法!!! Thanks! |
21.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: bukaoyan Posted on: 2005-09-29 11:37 在看了大家的许多意见,以及我自己调试代码,我也终于彻底的明白了怎么回事了 感谢你们 我是个初学者,小小的成功都可以让我惊喜老半天! |
22.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: chinamicrofox Posted on: 2006-01-05 03:00 子类继承父类的属性和方法,但是不会继承父类的构造函数,子类在实例化时如果子类自己没有构造函数会调用父类的无参数的构造函数,你的例子中,虽然没有实例化子类,但是你的子类是一个运行类(某个类包含main方法时,这个类就是一个运行类),运行时java虚拟机会装载并且实例它。所以调用父类构造函数,这样你运行就错了,在类的设计中,我们强调显示定义类的无参数构造函数。另外,大家不要老搞英文在上面,有点汉奸的感觉,sun也开始中文文档了不是?英文是要学习和使用的,但不是炫耀的,回答别人问题,又让别人读不懂,只是为了炫耀自己不好。 |
23.Re:关于继承的问题?请大家帮忙!! [Re: hamlet] | Copy to clipboard |
Posted by: blueoxygen Posted on: 2006-01-06 16:41 楼主没有借助好一点的IDE来写代码么? 虽然开始的时候编译打包等工作自己手动最好,但是用IDE能够帮助你更早发现问题。 你这段代码在IDEA里面根本就是提示错误的,你就更不会去编译然后产生疑问了。 而且从提示你就大概能猜出来怎么回事。下面是改进后的代码。 当你用原始代码的时候,IDE提示A没有默认构造函数。 因为如果类的定义中提供了带参数的构造器,则编译器不会再帮助你构造默认的无参构造器。此时如果你想调用new 一个class用无参的构造器就会出错。那么同理,此刻你的A提供了有参构造器,则A便不会在编译期间产生默认无参构造器,然后B继承了他,并且没有提供构造函数,编译器会试图产生一个无参构造器给他,但是第一步要默认调用父类无参构造器,可是A没有,这就是错误所在。 这些从IDE早期的错误提示中就可以自己揣摩出来。 感谢斑竹的耐心讲解,还是深入java虚拟机这本书好。
|
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |