[英]JAVA cast Object with “unknown” type in type of another Object
Im new here and i want to start with my first not so easy to describe Questions. 我在这里是新手,我想从我的第一个不太容易描述的问题开始。 I think a piece of code can explain best.
我认为一段代码可以最好地解释。
public void erlaube(Geraet pGeraet){
for(Object g : mGeraetetypen){
if(pGeraet.getClass().equals(g.getClass())){
System.out.println("TRUE");
mErlaubt.add((g.getClass())pGeraet); // MY PROBLEM
}
//System.out.println(pGeraet.getClass());
//System.out.println(g.getClass());
}
}
A "Geraet" (Device) is an abstract Class and it can be a Sensor or an Actor f.ex. “ Geraet”(设备)是抽象类,它可以是Sensor或Actorf.ex。 the class Temperaturesensor.
类温度传感器。
Geraetetypen (Devicetypes) is an ArrayList and contains all available Sensors and Actors. Geraetetypen(设备类型)是一个ArrayList,包含所有可用的Sensor和Actor。
The for- and if-block checks if the parameter Object is a type of an Object contained in the Devicetypes list f.ex. for和if块检查参数Object是否是Devicetypes列表f.ex中包含的Object的类型。 Temperaturesensor.
温度感应器。 if it is, i want to add it as this datatype (Temperaturesensor) to the "mErlaubt" (mAllowed) ArrayList.
如果是的话,我想将其作为此数据类型(温度传感器)添加到“ mErlaubt”(mAllowed)ArrayList中。
So it should do the same as This (cast): 所以它应该和This(cast)一样:
mErlaubt.add( (Temperatursensor) pGeraet);
But i dont want to cast explicit with (Temperatursensor). 但是我不想用(Temperatursensor)进行显式转换。 I want to cast dynamic with the datatype i found from the Datatypelist that compares.
我想使用从比较的数据类型列表中找到的数据类型动态转换。
Maybe something like the line with the comment //MY PROBLEM, but this doesnt work. 也许类似带有注释的行//我的问题,但这行不通。
I know this is hard and my description is not verry well, but i hope you understand what i try to do. 我知道这很难,我的描述也不太好,但是我希望你能理解我的尝试。 Please help.
请帮忙。 If you dont understand something or have a Question please ask.. Thanks!
如果您听不懂或有任何疑问,请询问..谢谢!
If your mErlaubt is an ArrayList of Geraet superclass, then you just have to use mErlaubt.add(pGeraet);
如果您的mErlaubt是Geraet超类的ArrayList,则只需使用
mErlaubt.add(pGeraet);
Your mErlaubt must then be defined like : List<Geraet> mErlaubt = new ArrayList<Geraet>();
然后,必须像下面这样定义您的mErlaubt:
List<Geraet> mErlaubt = new ArrayList<Geraet>();
You want to retrieve the class from a specific item of a list, and cast another object to this class? 您要从列表的特定项目中检索该类,然后将另一个对象转换为该类吗? Apart from
someClass.cast(someObject)
, there won't be any solutions. 除了
someClass.cast(someObject)
,没有任何解决方案。 But as far as i can see, this whole code doesn't make any sense. 但是据我所知,整个代码没有任何意义。 You'll add a single object multiple times.
您将多次添加单个对象。 And apart from that i'd say that's rather a job for
Generics
than casting
. 除此之外,我想说的是
Generics
不是casting
。
I think that this change should work as pGeraet
is of Class Geraet
so casting a variable to the super class should work. 我认为此更改应该起作用,因为
pGeraet
是Class Geraet
因此将变量强制转换为超类应该起作用。
for(Object g : mGeraetetypen){
if(pGeraet.getClass().equals(g.getClass())){
System.out.println("TRUE");
mErlaubt.add((Geraet)pGeraet);
}
Thank you all! 谢谢你们! I want to share my verry simple solution to all.
我想向所有人分享我非常简单的解决方案。 The class is a Deviceplace for Sensors/Actors on a houseautomation GUI.
该类是在houseautomation GUI上用于传感器/ Actor的Deviceplace。 Each Deviceplace allows only a hand full of Sensor/Actors (f.ex. no Heating on a outside place)
每个设备位置只允许满手的传感器/执行器(例如,在室外不加热)
public class Deviceplace {
ArrayList<Geraet> mErlaubt; //allowed devices
public Deviceplace(){
mErlaubt = new ArrayList<>();
}
public void erlaube(Geraet pGeraet){ //add allowed device
mErlaubt.add(pGeraet);
}
public boolean isErlaubt(Geraet pGeraet){ // check if device is allowed on this place
for(Geraet g : mErlaubt){
if(g.getClass().isInstance(pGeraet)){
return true;
}
}
return false;
}
public static void main(String[] args) {
Deviceplace place = new Deviceplace();
place.erlaube(new Temperatursensor()); // ad Temperaturesensor as allowed device
// now check if a device is allowed on this deviceplace
System.out.println(place.isErlaubt(new Windsensor())); // RETURNS false (Windsensor not allowed)
System.out.println(place.isErlaubt(new Temperatursensor())); // RETURNS true (Temperaturesensor allowed)
}
} }
Referring to your code sample , I'd suggest to store the allowed classes instead of instances of the allowed classes. 关于您的代码示例 ,我建议存储允许的类而不是允许的类的实例 。 You could use this modified code:
您可以使用以下修改的代码:
public class Deviceplace {
Set<Class<? extends Geraet>> mErlaubt;
public Deviceplace(){
mErlaubt = new HashSet<>();
}
public void erlaube(Class<? extends Geraet> clazz) {
mErlaubt.add(clazz);
}
// Option 1: no subtypes of the allowed types
public boolean isErlaubt(Geraet pGeraet) {
return mErlaubt.contains(pGeraet.getClass());
}
public static void main(String[] args) {
Deviceplace place = new Deviceplace();
place.erlaube(Temperatursensor.class);
System.out.println(place.isErlaubt(new Windsensor()));
System.out.println(place.isErlaubt(new Temperatursensor()));
}
}
An alternative implementation of isErlaubt
that also includes subtypes would be: 也包含子类型的
isErlaubt
的替代实现是:
// Option 2: including subtypes of the allowed types
public boolean isErlaubt(Geraet pGeraet) {
for(Class<? extends Geraet> clazz : mErlaubt){
if (clazz.isAssignableFrom(pGeraet.getClass())) {
return true;
}
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.