JAVA使用"未知"输入另一个Object的类型

时间:2015-04-15 13:10:26

标签: java dynamic casting extends

我是新来的,我想从我的第一个不太容易描述的问题开​​始。我认为一段代码可以解释得最好。

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());
    }
}

“Geraet”(设备)是一个抽象类,它可以是传感器或演员f.ex. Temperaturesensor类。

Geraetetypen(Devicetypes)是一个ArrayList,包含所有可用的传感器和Actors。

for-和if-block检查参数Object是否是包含在Devicetypes列表f.ex中的Object类型。温度感应器。 如果是,我想将它作为此数据类型(Temperaturesensor)添加到“mErlaubt”(mAllowed)ArrayList。

所以它应该和This(演员)一样:

mErlaubt.add( (Temperatursensor) pGeraet);

但我不想与(Temperatursensor)明确表达。我想使用从比较的数据类型列表中找到的数据类型来强制动态。

也许类似于评论的界面//我的问题,但这不起作用。

我知道这很难,我的描述也不是很好,但我希望你能理解我的尝试。请帮忙。如果你不明白或有问题,请问......谢谢!

5 个答案:

答案 0 :(得分:0)

如果您的mErlaubt是Geraet超类的ArrayList,那么您只需使用mErlaubt.add(pGeraet);

然后必须将您的mErlaubt定义为:List<Geraet> mErlaubt = new ArrayList<Geraet>();

答案 1 :(得分:0)

您想要从列表的特定项中检索类,并将另一个对象强制转换为此类?除了someClass.cast(someObject)之外,没有任何解决方案。但据我所知,这整个代码没有任何意义。您将多次添加单个对象。除此之外我还说Generics而不是casting的工作。

答案 2 :(得分:0)

我认为此更改应该在pGeraet属于类Geraet时起作用,因此将变量强制转换为超类应该有效。

   for(Object g : mGeraetetypen){                                          
        if(pGeraet.getClass().equals(g.getClass())){                        
            System.out.println("TRUE");
            mErlaubt.add((Geraet)pGeraet); 
        }

答案 3 :(得分:0)

谢谢大家!我想与所有人分享我的简单解决方案。该类是家庭自动化GUI上的传感器/演员的设备。每个Deviceplace只允许一只手充满传感器/演员(例如,外部没有加热)

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)
}

}

答案 4 :(得分:0)

参考您的code sample,我建议存储允许的类的允许而不是实例。您可以使用此修改后的代码:

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()));
  }

}

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;
  }
相关问题