Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 编程/算法/API  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:求助,关于java构造函数与继承的编译错误 [Re:maxterry]
maxterry





发贴: 10
积分: 0
于 2006-01-29 21:29 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
javac 的错误信息指出SrInherit1构造函数错误,不解?
为什么子类继承父类构造函数(super())可以,但子类创造自己的构造函数则会出错???

父类有自己的构造函数,
那继承该父类的子类想要有自己的构造函数
(而不是用super()继承父类构造函数)

那么以上代码该这么修改才正确呢,请“元老”指点~~


=====编译错误实例====[子类创造自己的构造函数]====
class SrInherit1
{
int x,y;
SrInherit1(int a,int b)
{
x=a;
y=b;
}
}
class SrInherit2 extends SrInherit1
{
SrInherit2(int a,int b)
{
x=a+3;
y=b+7;
}
}
public class SrInherit
{
public static void main(String args[])
{
int sum;
SrInherit2 Srv=new SrInherit2(40,80);
sum=Srv.x+Srv.y;
System.out.println("sum="+sum);
}
}

=====编译可通过实例=====[子类继承父类构造函数]===
class SrInherit1
{
int x,y;
SrInherit1(int a,int b)
{
x=a;
y=b;
}
}
class SrInherit2 extends SrInherit1
{
SrInherit2(int a,int b)
{
super(a,b);
}
}
public class SrInherit
{
public static void main(String args[])
{
int sum;
SrInherit2 Srv=new SrInherit2(40,80);
sum=Srv.x+Srv.y;
System.out.println("sum="+sum);
}
}


Please use [ code ] tag to format code listing in the future.


why edited on 2006-01-29 21:55


话题树型展开
人气 标题 作者 字数 发贴时间
8909 求助,关于java构造函数与继承的编译错误 maxterry 429 2006-01-29 18:12
6991 Re:求助,关于java构造函数与继承的编译错误 why 274 2006-01-29 20:18
7189 Re:求助,关于java构造函数与继承的编译错误 maxterry 1124 2006-01-29 21:29
7069 Re:求助,关于java构造函数与继承的编译错误 why 700 2006-01-29 21:49
7658 Re:求助,关于java构造函数与继承的编译错误 maxterry 30 2006-01-30 11:18

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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