English 中文(简体)
Java 术语
原标题:Java Terminology
  • 时间:2011-11-21 14:49:15
  •  标签:
  • java

我对贾瓦来说是新鲜事,并想知道更多东西。 我现在要回答一个当前的问题,但我也想知道这一方法是什么,以便我能够做一些进一步的解读。

我现在有这样的情况:

public class BasicActivityExtension {
    public Boolean basicExtensionMethod() { }
    ...
}

public class MyExtension extends BasicActivityExtension {
    public Boolean myExtensionMethod() { }
    ...
}

之后,我采用了一种服务方法(主轴是,我准备研究如何获得“ &”;在此停留):

public Class < ? extends BasicActivityExtension> getExtensionByActivity(
                                                     BasicActivity activity,
                                                     ExtensionTypes type)
                                                 throws Exception {
    ...
}

www.un.org/Depts/DGACM/index_spanish.htm 我的问题是,为什么这样做会造成错误,这种技术的名称是什么(当正确做时!)。

MyExtension members = (MyExtension) activityService.getExtensionByActivity(activity,
                                        ExtensionTypes.member);

The error is Cannot cast from Class to MyExtension;

另外,如果我有反对等级; 基本活动 推广和强化;我如何在不照顾到哪类基本活动的情况下,只提一般方法?

最佳回答

你似乎想要

<T extends BasicActivityExtension>
public T getExtensionByActivity(BasicActivity activity, ExtensionTypes type) {
....
}

当你具体指明<代码>Class时,它必须是一类物体,而不是一类物体。

BTW:我将避免盲目扔下<条码>。 处理经核实的例外情况的方法更好。

问题回答

您所做的工作称为casting。

之所以出现这一问题,是因为你正在从您的方法getExtensionByActactive上退回一个类型的物体。 但是,你正试图将这种内容投到一个类型的<代码>MyExtension上。 您将通过以下方式解决这一问题:从改为





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签