a.process(); // 这一句为什么编译错误呢? 请指教

williamxa2008 2008-04-24 03:41:32
class A{
void process()throws Exception{
throw new Exception();
}
}
class b extends A{
void process(int a){
System.out.println("B");
}
}
public class J{
public static void main(String[] args){
A a=new b();
a.process(); // 这一句为什么编译错误呢? 请指教
}
}
a.process(); // 这一句为什么编译错误呢? 请指教
...全文
88 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
williamxa2008 2008-04-24
  • 打赏
  • 举报
回复
我刚才给你聊天呢 你怎么不说话了呀 哈哈 还是用QQ吧 你的是多少?我的是625767199
williamxa2008 2008-04-24
  • 打赏
  • 举报
回复
不能 所以给它改了
Shine_Panda 2008-04-24
  • 打赏
  • 举报
回复
static 怎么可以修饰类啊。

williamxa2008 2008-04-24
  • 打赏
  • 举报
回复
Given:
11. static classA {
12. void process() throws Exception { throw new Exception(); }
13. }
14. static class B extends A {
15. void process() { System.out.println("B "); }
16. }
17. public static void main(String[] args) {
18.A a=new B();
19. a.process();
20.}
What is the result?
A. B
B. The code runs with no output.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 15.
E. Compilation fails because of an error in line 18.
F. Compilation fails because of an error in line 19.
答案是F 可是依照刚才的程序,那错误不是在main()没有抛出异常吗 应该是17行了呀 哈哈
williamxa2008 2008-04-24
  • 打赏
  • 举报
回复
class A{
void process()throws Exception{
throw new Exception();
}
}
class b extends A{
void process(){
System.out.println("B");
}
}
public class J{
1:public static void main(String[] args)throws Exception{
2:A a=new b();
3:a.process();
}
}
输出的是B 正确.

那不是说是错误不是在第3行 而是第一行呀?
williamxa2008 2008-04-24
  • 打赏
  • 举报
回复
加上之后编译是能够通过 ,可是发生了运行时错误
Exception in thread "main" java.lang.Exception
at A.process(J.java:85)
at J.main(J.java:96)
williamxa2008 2008-04-24
  • 打赏
  • 举报
回复
哈哈 不好意思 那是我调试的程序 自己改的,下面的才是原程序
class A{
void process()throws Exception{
throw new Exception();
}
}
class b extends A{
void process(){
System.out.println("B");
}
}
public class J{
public static void main(String[] args){
A a=new b();
a.process(); // 这一句为什么编译错误呢? 请指教
}
}
a.process(); // 这一句为什么编译错误呢? 请指教
Shine_Panda 2008-04-24
  • 打赏
  • 举报
回复
public static void main(String[] args) throws Exception
Shine_Panda 2008-04-24
  • 打赏
  • 举报
回复
没有方法重写
不能发生多态。
a调不到 b类中 process(int a );
a实际调的是 A类中的process()方法。
所以要抛出异常。

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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