java中类字段_Java中的类类型为字段(Class type as field in Java)

Java中的类类型为字段(Class type as field in Java)

我的问题可能看起来很奇怪,我不知道是否可能,但它是:

我有几个类(UseCases)都实现了一个接口=> IUseCase 。

UseCase1 implementing IUseCase

UseCase2 implementing IUseCase

etc.

我还有另一个类“ UseCaseContext ”,它应该带有一些关于几个用例的信息。

我的目标是每个UseCase有一个 UseCaseContext实例。 为此,我需要UseCaseContext中的一个字段 ,告诉我这个上下文是否与UseCase1或UseCase2等相关。

因此,不是将UseCase的实例存储在字段中,还有另一种方法来确定UseCaseContext与哪个UseCase相关?

我知道这可能会令人困惑,所以请告诉我,如果你需要我解释,否则:)

谢谢 !

My question might seem weird, and I don't know if it possible, but there it is :

I have several classes (UseCases) all implementing an interface => IUseCase.

UseCase1 implementing IUseCase

UseCase2 implementing IUseCase

etc.

I also have another class "UseCaseContext" that is supposed to carry some information about several usecases.

My goal would be to have one instance of UseCaseContext per UseCase. To do that, I would need a field in the UseCaseContext that tells me if this context is either related to UseCase1 or UseCase2 or etc.

So, rather than storing an instance of the UseCase in a field, is there another way to determine if to which UseCase is my UseCaseContext related to ?

I know this is might be confusing, so please, tell me if you need me to explain it otherwise :)

Thank you !

原文:https://stackoverflow.com/questions/14937308

2020-01-11 02:31

满意答案

尝试这个:

public class UseCaseContext {

private Class useCaseClass;

public UseCaseContext(Class useCaseClass) {

this.useCaseClass = useCaseClass;

}

}

然后你可以简单地调用: new UseCaseContext(instance.getClass()) 。

Try this:

public class UseCaseContext {

private Class useCaseClass;

public UseCaseContext(Class useCaseClass) {

this.useCaseClass = useCaseClass;

}

}

Then you can simply call: new UseCaseContext(instance.getClass()).

2013-02-18

相关问答

这似乎是一个javac6错误,它在javac7中是修复的。 解决方法是通过升级: ((Area>)area).parent = this;

这似乎真的很奇怪 - 为什么我们需要一个演员来访问超级类的成员? 根本的问题是,私有成员被特定地排除在继承之外,因此A没有parent成员。 相同的问题可以通过非通用示例来证明。 在“ 区域 ”中, 父项具有私有访问权限的错误信息不太准确,但大多数情况下可能都是这样。 但是在这种情况下,这是误导的,更好的信息是“ A不会继承”区域 “中的私人成员 ” 为...

现在,您将获得Class#toString()提供的默认字符串表示形式: 将对象转换为字符串。 字符串表示形式是字符串“class”或“interface”,后跟空格,然后是getName返回的格式的类的完全限定名称。 如果此Class对象表示基本类型,则此方法返回基本类型的名称。 如果此Class对象表示void,则此方法返回“void”。 因此,您希望使用其他方法来获取类的名称。 更改 f.getType()

至 ((Class) f.getType()).getName()

// or

(...

你正在想象你想要的x 。 人们常常因为需要重命名类参数以避免创建字段而烦恼。 请参阅此症状和阴影问题 。 在这种情况下,它会导致互操作问题。 scala> :pa

// Entering paste mode (ctrl-D to finish)

class Vector2(x0: Float, y0: Float) extends gdx.math.Vector2(x0, y0) {

def copy(x: Float = this.x, y: Float = this.y): Vecto...

尝试这个: public class UseCaseContext {

private Class useCaseClass;

public UseCaseContext(Class useCaseClass) {

this.useCaseClass = useCaseClass;

}

}

然后你可以简单地调用: new UseCaseContext(instance.getClass()) 。 Try this: public class...

是的,因为PriorityQueue实现了Queue 。 如果你想将创建委托给子类,让他们通过适当的Queue来使用基础构造函数: public class Base {

protected final Queue customers;

protected Base(Queue c) {

this.customers = c;

}

}

public class Sub1 extends Base {

Su...

你的课程必须是一个通用类型 : 例如: static class APICallback {

boolean status;

T data;

}

Your class would have to be a generic type: E.g: static class APICallback {

boolean status;

T data;

}

如果目标字段是基本类型,那么您可以通过反射获取其默认值,反过来将反映在包装器对象中。 从这个对象,您可以获得相应的包装类型。 因此,您可以替换以下代码行: Class> fType = f.getType();

有: Class> fType = f.getType();

if(fType.isPrimitive()){

fType = f.get(target).getClass();

}

If the target field is a primitive type, then...

Field#getType()方法返回一个Class>对象,表示该字段类型的Class实例。 在这种情况下,您将获得Class 。 当然不能是MyType>的实例。 这是Class一个实例。 如果要查看该字段是否为MyType>类型或其子类型,则需要从该Class对象获取实例: if (field.getType().newInstance() instanceof MyType>) {

System.out.println("Field is of typ...

我以非常简单的方式解决了。 我使用了setField的签名和OType参数,如下所示: this.data.field(fieldName, fieldDataItem, OType.EMBEDDEDLIST);

I resolved in very simple way. I used the signature of setField with OType parameter, like this: this.data.field(fieldName, fieldDataItem, OTyp...

Type genericSuperclass = x.getClass().getGenericSuperclass();

if (genericSuperclass instanceof ParameterizedType) {

Type[] types =

((ParameterizedType) genericSuperclass).getActualTypeArguments();

}

然后,如果type instanceof Class ,则可以使用Class....

相关文章

Field: Field就是一个字段,定义一个Field很简单: 基本上属性也和FieldType类似

...

在使用jsp生成web图片时遇到这个问题,这是源代码中的一条语句,源代码可以执行,可是一将源码放入ec

...

import java.lang.reflect.ParameterizedType;import j

...

在solr建立索引的时候,如果你提交的doc中没有 id 这个Field,结果Solr在建立索引时候出

...

Java类用于描述一类事物的共性,该类事物有什么属性,没有什么属性,至于这个属性的值是什么,则是由这个

...

为了和以前的程序兼容,在solr建立索引的时候,将id设为gid,结果在建立索引时候出现如下错误: o

...

在solr中 添加新的索引词语时,报如标题所示错误,指定是插入的字段没有在solr索引字段里 可以修改

...

在编写后台登陆模块时,将许多默认的设置放在一个名为default的package 里。然后再定义其他

...

Java变量类型 在Java语言中,所有的变量在使用前必须声明。声明变量的基本格式如下: typ

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值