Java:捕获块可以是多态的吗? [英] Java: Can catch blocks be polymorphic?

查看:110
本文介绍了Java:捕获块可以是多态的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我要参加重复考试的论文中,我被问到捕获块可以是多态的吗?".

In a paper I'm going over for a repeat exam, I'm asked "Can catch blocks be polymorphic?".

如果为true,则对多个catch块进行多态调用对我来说没有任何意义.如果catch块不能命名并且仅在其方法头中包含参数,那是否是多态性?

If true, it doesn't make sense to me to call multiple catch blocks polymorphic. Is it polymorphism if catch blocks cannot be named and only contain parameters in their method header?

例如:

try {

//...

} catch (FileNotFoundException e) {
    System.err.println("FileNotFoundException: " + e.getMessage());
    throw new SampleException(e);

} catch (IOException e) {
    System.err.println("Caught IOException: " + e.getMessage());
}

在这两个catch块是多态的吗?

In this, are these two catch blocks polymorphic?

推荐答案

您发布的示例应描述为超载.与重载唯一不同的是 readability 要求,即子类出现在其超类之前.

The example you posted should be described as overloading. The only thing different from overloading is a readability requirement that subclasses appears before its superclass.

语句多形抓块"需要一些体操来解析.

The statement "Polymorphic catch blocks" requires a bit of gymnastics to parse.

完成上述体操后,我将其解释为

After completing said gymnastics I would interpret it as

  • 一个catch块,它根据要捕获的对象的类型提供不同的实现.

类似于

try{


} catchwith ( catchHandlerObject );

并且catchHandlerObject是多态的. IE.根据catchHandlerObject的(运行时)类型不同地处理相同的异常.

And catchHandlerObject is polymorphic. I.e. handles the same exception differently depending on the (runtime) type of catchHandlerObject.

class C{
   public void m(Object o){ .... };
}

m(Object o)是多态的吗?我要说的共识是,不必在此描述中包含多态性.呼叫m( stringObject ).并不表示多态性.

Is m(Object o) polymorphic? I would say that the consensus is that it is unnecessary to include polymorphism in to this description. A call m( stringObject ). is not indicative of polymorphism.

我和以前的海报相反,说.多态不是标记这种情况的正确方法.多态性不是描述正在发生的事情的正确方法.

I go contrary to the previous posters an say no. Polymorphic is not the proper way to label this situation. Polymorphism is not the proper way to describe what is happening.

我也真的认为您应该与您的助教或您的教授仔细检查.问题经常发生,包括从拼写到完全脱心的经历等错误.

I also really think you should double check this with your TA or your professor. It happens regularly that questions include mistakes ranging from spelling to completely out of mind experiences.

yshavit 所述,重载表示编译时绑定.捕获块必须在运行时解决.我不知所措,但是找到了一个比重载更好的术语.

As yshavit noted, overloading indicates a compile time binding. The catch-block is by necessity resolved at runtime. I'm at a loss finding a better term than overloading though.

这篇关于Java:捕获块可以是多态的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆