作者tanx (arctanx)
看板java
标题Re: [问题] constructor error(extends时)
时间Mon Apr 16 22:31:53 2007
※ 引述《adrianshum (Alien)》之铭言:
: ※ 引述《tanx (arctanx)》之铭言:
: : 新手发问
: : 这是课本後面习题
: : class A
: : {
: : int i;
: : public A(int i)
: : {
: : this.i=i;
: : }
: : }
: : public class Test extends A{
: : public static void main(String[] arg){
: : A aaa=new A(10);
: : System.out.println(aaa.i);
: : }
: : }
: : error message:
: : D:\java\debug>javac Test.java
: : Test.java:12: cannot find symbol
: : symbol : constructor A()
: : location: class A
: : public class Test extends A{
: : ^
: : 1 error
: : 请问
: : 为什麽有错ㄚ? 先谢谢搂~
: 这问题该被收到了吧? o_oa
: child class 的 constructor 如果在起
: 初没有用 super(....) 去 invoke parent class
: 的某 constructor, 就会被自动加入 invoke super()
: (无参数 constructor)
: 由於你的 parent class 没有提供无参数 constructor
: 所以出问题了.
: 解决方法我想已经有很大提示了, 自己试试吧
: Alien
请问
是不是我想的这样
class Test
没有定constructor
所以compile时会插入一个default constructor (无参数)
由於Test 继承 class A
而这Test的 default constructor虽然没内容
但是compile会自动呼叫 A的default constructor
而class A里面由於没有定义无参数的constructor
所以错误?
而只要在Test理定义一个constructor
内容去call super(参数)
这样就OK?
不知道想的对不对
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.112.201.115
※ 编辑: tanx 来自: 59.112.201.115 (04/16 22:34)