看看这段程序,为什么这样的“继承”会出错? 这是因为01的带参数构造函数把默认不带参数的构造函数Overloading了,而03的构造函数没有调用带参的01构造函数,所以出错,在03构造函数里面加上 super(int r,String s),可以了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 abstract class Glyph 相当于把 Glyph 抽象出来. class RoundGlyph03 extends RoundGlyph01 RoundGlyph01也是继承 Glyph. 楼主可以试一下把 RoundGlyph01 abstract一下 你就知道为啥了 RoundGlyph03(int r,String s) { radius = r; this.s = s; System.out.println("RoundGlyph03.RoundGlyph(),radius = "+radius+" "+s); }在RoundGlyph03 类的构造函数中,如果不显式的调用其继承类的构造函数的话,系统默认在RoundGlyph03 类的构造函数中调用继承类无参数构造函数即:RoundGlyph01(), 而类RoundGlyph01还没有定义无参数构造函数,所以就报错了。而继承抽象类Glyph就不一样了,其有无参数构造函数。 你可以如下修改: RoundGlyph03(int r,String s) { super(r, s); System.out.println("RoundGlyph03.RoundGlyph(),radius = "+radius+" "+s); } 你要明白这样一个道理,就是每个子类的构造方法必须调用父类的构造方法。如果你没有在你子类中写明调用父类的那个构造方法,就默认要调用没有参数的那个,这个时候,如果你的父类没有没有参数的构造方法,就会出错。class RoundGlyph03 extends RoundGlyph01 ,但是RoundGlyph01没有0参数的构造方法,所以出错。你可以选择帮RoundGlyph01 加一个0参数的构造方法,也可以像HawaiiLeo(罗马数字) 所说的那样,在RoundGlyph03指定调用父类的那个构造方法: RoundGlyph03(int r,String s) { super(r, s); System.out.println("RoundGlyph03.RoundGlyph(),radius = "+radius+" "+s); } Glyph类是抽象类抽象类是不可以继承的。怎么能继承抽象类呢? 简单,在函数:RoundGlyph03(int r,String s) { radius=r; this.s=s; System.out.println("RoundGlyph03.RoundGlyph(),radius="+radius+" "+s); }的第一句里加上super(r,s)就行了。你的父类没有默认构造函数,那么必须在子类构造函数的第一句中显示调用父类构造函数。 呵呵, maowu(猫呜) 已经说过了,没注意。sorry 基本概念不清,也能写出这样的程序.牛.------------------------------------体验速度,体验CSDN新版论坛助手:http://community.csdn.net/Expert/TopicView.asp?id=3108679 昕旸软件隆重推出,特别推荐:小灰狗助手 请教!为什么编译带有JDBC的JAVA文件会出现ClassNotFoundException错误? 关于JTree的问题(急!我最多只能给一百分) 求资料,JBUILDER如何开发调试WAP 如何用JAVA打印复杂的财务报表,我做的是JSP的财务程序,以网页显示,现要打印,谢谢帮助 jb和MS SQL SERVER的问题 请教高手,如何在一个string 中加入硬回车 初学java,兄弟们,帮我看一段代码? 谁有Jbulider5的注册吗?谢谢 一个简单类设计问题 请为什么这个双检测单例对象不行 为什么我用的jbuilder,不能输入中文?
class RoundGlyph03 extends RoundGlyph01 RoundGlyph01也是继承 Glyph. 楼主可以试一下把 RoundGlyph01 abstract一下 你就知道为啥了
radius = r;
this.s = s;
System.out.println("RoundGlyph03.RoundGlyph(),radius = "+radius+" "+s);
}
在RoundGlyph03 类的构造函数中,如果不显式的调用其继承类的构造函数的话,
系统默认在RoundGlyph03 类的构造函数中调用继承类无参数构造函数即:
RoundGlyph01(), 而类RoundGlyph01还没有定义无参数构造函数,所以就报错了。
而继承抽象类Glyph就不一样了,其有无参数构造函数。
RoundGlyph03(int r,String s) {
super(r, s);
System.out.println("RoundGlyph03.RoundGlyph(),radius = "+radius+" "+s);
}
如果你没有在你子类中写明调用父类的那个构造方法,就默认要调用没有参数的那个,
这个时候,如果你的父类没有没有参数的构造方法,就会出错。
class RoundGlyph03 extends RoundGlyph01 ,
但是RoundGlyph01没有0参数的构造方法,所以出错。你可以选择帮RoundGlyph01 加一个0参数的构造方法,也可以像HawaiiLeo(罗马数字) 所说的那样,在RoundGlyph03指定调用父类的那个构造方法: RoundGlyph03(int r,String s) {
super(r, s);
System.out.println("RoundGlyph03.RoundGlyph(),radius = "+radius+" "+s);
}
怎么能继承抽象类呢?
RoundGlyph03(int r,String s) {
radius=r;
this.s=s;
System.out.println("RoundGlyph03.RoundGlyph(),radius="+radius+" "+s);
}
的第一句里加上super(r,s)就行了。你的父类没有默认构造函数,那么必须在子类构造函数的第一句中显示调用父类构造函数。
------------------------------------
体验速度,体验CSDN新版论坛助手:http://community.csdn.net/Expert/TopicView.asp?id=3108679