先是一段代码package exceptions;
//: exceptions/StormyInning.java
// Overridden methods may throw only the exceptions
// specified in their base-class versions, or exceptions
// derived from the base-class exceptions.class BaseballException extends Exception {}
class Foul extends BaseballException {}
class Strike extends BaseballException {}abstract class Inning {
public Inning() throws BaseballException {}
public void event() throws BaseballException {
// Doesn't actually have to throw anything
}
public abstract void atBat() throws Strike, Foul;
public void walk() {} // Throws no checked exceptions
}class StormException extends Exception {}
class RainedOut extends StormException {}
class PopFoul extends Foul {}interface Storm {
public void event() throws RainedOut;
public void rainHard() throws RainedOut;
}
public class StormyInning extends Inning implements Storm {
// OK to add new exceptions for constructors, but you
// must deal with the base constructor exceptions:
public StormyInning()
throws RainedOut, BaseballException {}
public StormyInning(String s)
throws Foul, BaseballException {}
// Regular methods must conform to base class:
//! void walk() throws PopFoul {} //Compile error
// Interface CANNOT add exceptions to existing
// methods from the base class:
//! public void event() throws RainedOut {}
// If the method doesn't already exist in the
// base class, the exception is OK:
public void rainHard() throws RainedOut {}
// You can choose to not throw any exceptions,
// even if the base version does:
public void event() {}
// Overridden methods can throw inherited exceptions:
public void atBat() throws PopFoul {}
public static void main(String[] args) {
try {
StormyInning si = new StormyInning();
si.atBat();
} catch(PopFoul e) {
System.out.println("Pop foul");
} catch(RainedOut e) {
System.out.println("Rained out");
} catch(BaseballException e) {
System.out.println("Generic baseball exception");
}
// Strike not thrown in derived version.
try {
// What happens if you upcast?
Inning i = new StormyInning();
i.atBat();
// You must catch the exceptions from the
// base-class version of the method:
} catch(Strike e) {
System.out.println("Strike");
} catch(Foul e) {
System.out.println("Foul");
} catch(RainedOut e) {
System.out.println("Rained out");
} catch(BaseballException e) {
System.out.println("Generic baseball exception");
}
}
} ///:~书上写到
“接口Storm值得注意,因为他包含了一个在Inning中定义的方法Event()和一个不在Inning中定义的方法rainHard()。这俩个方法都抛出新的异常RainOut。如果StormyInning类在扩展Inning类的同时又实现了Storm接口,那么Storm里的event()方法就不能改变在Inning中的event()方法的异常接口。”
这段话实在没看懂在讲什么,主要是“那么Storm里的event()方法就不能改变在Inning中的event()方法的异常接口”这句话不知所云啊,求点播
//: exceptions/StormyInning.java
// Overridden methods may throw only the exceptions
// specified in their base-class versions, or exceptions
// derived from the base-class exceptions.class BaseballException extends Exception {}
class Foul extends BaseballException {}
class Strike extends BaseballException {}abstract class Inning {
public Inning() throws BaseballException {}
public void event() throws BaseballException {
// Doesn't actually have to throw anything
}
public abstract void atBat() throws Strike, Foul;
public void walk() {} // Throws no checked exceptions
}class StormException extends Exception {}
class RainedOut extends StormException {}
class PopFoul extends Foul {}interface Storm {
public void event() throws RainedOut;
public void rainHard() throws RainedOut;
}
public class StormyInning extends Inning implements Storm {
// OK to add new exceptions for constructors, but you
// must deal with the base constructor exceptions:
public StormyInning()
throws RainedOut, BaseballException {}
public StormyInning(String s)
throws Foul, BaseballException {}
// Regular methods must conform to base class:
//! void walk() throws PopFoul {} //Compile error
// Interface CANNOT add exceptions to existing
// methods from the base class:
//! public void event() throws RainedOut {}
// If the method doesn't already exist in the
// base class, the exception is OK:
public void rainHard() throws RainedOut {}
// You can choose to not throw any exceptions,
// even if the base version does:
public void event() {}
// Overridden methods can throw inherited exceptions:
public void atBat() throws PopFoul {}
public static void main(String[] args) {
try {
StormyInning si = new StormyInning();
si.atBat();
} catch(PopFoul e) {
System.out.println("Pop foul");
} catch(RainedOut e) {
System.out.println("Rained out");
} catch(BaseballException e) {
System.out.println("Generic baseball exception");
}
// Strike not thrown in derived version.
try {
// What happens if you upcast?
Inning i = new StormyInning();
i.atBat();
// You must catch the exceptions from the
// base-class version of the method:
} catch(Strike e) {
System.out.println("Strike");
} catch(Foul e) {
System.out.println("Foul");
} catch(RainedOut e) {
System.out.println("Rained out");
} catch(BaseballException e) {
System.out.println("Generic baseball exception");
}
}
} ///:~书上写到
“接口Storm值得注意,因为他包含了一个在Inning中定义的方法Event()和一个不在Inning中定义的方法rainHard()。这俩个方法都抛出新的异常RainOut。如果StormyInning类在扩展Inning类的同时又实现了Storm接口,那么Storm里的event()方法就不能改变在Inning中的event()方法的异常接口。”
这段话实在没看懂在讲什么,主要是“那么Storm里的event()方法就不能改变在Inning中的event()方法的异常接口”这句话不知所云啊,求点播
StormInning类继承了Inner,同时实现了Storm,那么它的event()函数抛出什么类型的异常?是按照Inner类的event()抛出BaseballException还是按照Storm接口event()抛出Rainout呢?那句话的意思就是,抛出的异常类型按照父类方法而不是接口来。回到问题本身,最好在设计上避免出现父类和接口具有同名函数的情况。
改成 public void event() throws RainedOut{}
Eclipse中提示
Exception RainedOut is not compatible with throws clause in Inning.event()
改成 public void event() throws BaseballException {
Eclipse中提示
Exception BaseballException is not compatible with throws clause in Storm.event()
为什么我感觉抛出的异常类型同时要兼顾父类方法和接口,因为”异常说明的接口“会缩小,为了同时兼顾,所以在这个导出类中的event方法中不能声明抛出任何的Exception