简体   繁体   English

从超类的对象数组访问子类的功能

[英]Access to a function of subclass from an array of object of superclass

I have class Carte and Croyant , DeuxEx are subclass of Carte . 我有CarteCroyant类, DeuxExCarte子类。 In Croyant and DeuxEx , there are attributes valueCroyant or valueDeuxEx which just exists in each class. CroyantDeuxEx ,每个类中都存在属性valueCroyantvalueDeuxEx I create an array of Object of class Carte . 我创建了Carte类的Object数组。 How can I access to method getValueCroyant() or getValueDeuxEx() from object of this array. 如何从此数组的对象访问方法getValueCroyant()getValueDeuxEx()

Class Carte {
    private int id;
    public Carte(int id){
        this.id=id;
    }
}
Class Croyant extends Carte{
    private int valueCroyant;
    public Croyant(int id){
        super(id);
    }
    public int getValueCroyant(){
        return this.valueCroyant;
    }
}
Class DeuxEx extends Carte{
    private String valueDeuxEx;
    public DeuxEx(int id){
      super(id); 
    }
    public String getValueDeuxEx(){
       return this.ValueDeuxEx;
    }
}
public static void main(String[] agrs){
    ArrayList<Carte> array_carte = new ArrayList();
    Croyant cr1 = new Croyant(1);
    Croyant cr2 = new Croyant(2);
    DeuxEx de1= new DeuxEx(3);
    DeuxEx de2 = new DeuxEx(4);
    array_carte.add(cr1);
    array_carte.add(cr2);
    array_carte.add(de1);
    array_carte.add(de2);
    for(Carte c:array_carte){
       if(c instanceof Croyant){
           System.out.println(c.getValueCroyant()); 
        }else{
           System.out.println(c.getValueDeuxEx()); 
        }
    }
}

I want to do a for loop like this, but it doesn't work. 我想像这样做一个for循环,但是不起作用。 Can somebody help me please! 有人可以帮我吗!

c.getValueCroyant() won't compile because its type is Carte which is the superclass. c.getValueCroyant()无法编译,因为它的类型是Carte,这是超类。 Even if it is not elegant, to call this method, using instanceOf is fine but you have also to cast to Croyant 即使不是很优雅,也可以使用instanceOf调用此方法,但还必须转换为Croyant

What you're trying to do hints strongly at a problem with your design. 您尝试做的事情强烈暗示了您的设计存在问题。 The suggestion in another answer of testing with instanceof and casting would work, but it's very anti-OO. 使用instanceof和casting测试的另一个答案中的建议可能会起作用,但这是非常反OO的。 Basically, that kind of solution circumvents polymorphism. 基本上,这种解决方案绕过了多态性。

I see that valueCroyant is an int while valueDeuxEx is a String . 我看到valueCroyant是一个intvalueDeuxExString The best solution would be to rethink your design so these values are either the same type or implement some common interface. 最好的解决方案是重新考虑您的设计,以便这些值是相同的类型或实现某些通用接口。 Then move the value and the getter to the common superclass Carte . 然后将值和吸气剂移至通用超类Carte If it's impossible to represent both values using the same type, then give Carte an abstract method that is meant to do something with the value, and implement it differently in each subclass. 如果不可能使用相同的类型表示两个值,则为Carte一个抽象方法,该方法将对值进行处理,并在每个子类中以不同的方式实现它。

I think you should cast. 我认为你应该投。 The code snippet shown by you won't compile. 您显示的代码段不会编译。

Something like this: 像这样:

for(Carte c:array_carte){
       if(c instanceof Croyant){
           System.out.println(((Croyant)c).getValueCroyant()); 
        }else if(c instanceof DeuxEx){
           System.out.println(((DeuxEx)c).getValueDeuxEx()); 
        }
    }

Check my answer here . 在这里检查我的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM