【问题标题】:Cast object in ArrayList extension doesn't see it's methodsArrayList 扩展中的 Cast 对象看不到它的方法
【发布时间】:2016-10-06 13:22:57
【问题描述】:

所以我有一个有趣的问题...... 我想访问从 List 中检索到的对象的方法,该对象由 ArrayList 扩展。

看起来像这样:

import java.util.ArrayList;
import java.util.Collection;

public class PropertyList<Property> extends ArrayList {

private static final long serialVersionUID = -7854888805136619636L;

    PropertyList(){
        super();
    }

    PropertyList(Collection<Property> c){
        super(c);
    }

    boolean containsProperty(PropertyList<Property> pl){
        Property asdf = (Property) this.get(4);
        System.out.println(asdf.<can't access "Property" methods>);  //mark
        return false;
    }

}

知道为什么我不能访问标记行中的方法吗? 澄清一下 - Property 对象中的方法是公共的。

编辑: 属性说明:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;


@XmlAccessorType(XmlAccessType.FIELD)
class Property {

@XmlValue
private String property = null;

@XmlAttribute
String state;
@XmlAttribute
String name;
@XmlAttribute
String type;
@XmlAttribute
String value;

Property(){}

Property(String state, String name, String type, String value){
    this.state = state;
    this.name = name;
    this.type = type;
    this.value = value;
}

public String getName(){
    return name;
}

@Override 
public String toString(){
    return state + " " + name + " " + type + " " + value;
}

}

【问题讨论】:

  • 你不应该extends ArrayList&lt;Property&gt; 吗?无论如何,您不能也显示Property 的代码吗?
  • @Shark 确实如此。就目前而言,这没有任何意义。然而,对Property 的显式转换让我也想看看定义:)
  • 无论如何,你的类声明不应该是这样的:public class PropertyList&lt;T&gt; extends ArrayList&lt;T&gt;,你可以轻松地将T硬编码为Property
  • 您没有投射到类属性。您正在转换为 PropertyList 类的泛型类型,您选择将其命名为 Property 而不是 T 或 E 或任何其他传统的单个字母,因此会影响 Property 类。您几乎不应该扩展集合类。在这种情况下当然不是。
  • 好的,然后尝试按照我所说的(使用泛型类型)声明@JBNizet 指出的正确,它被遮蔽了。 (我猜 Property 在这种情况下就像一个泛型类型,类似于 TEGenericType 可能是任何东西)而不是你具体的 Property 类。

标签: java oop arraylist


【解决方案1】:

您应该重构代码以显式引用您的Property 类,而不是将其用作类似于ET 或任何其他GenericType 的泛型类型。您的问题是 Property 被推断为 GenericType 并且没有引用您的具体 Property 类,而是对其进行了隐藏 - 使其与您使用的相同

public class PropertyList<T> extends ArrayList<T>

所以,大致是这样的:

import java.util.ArrayList;
import java.util.Collection;

public class PropertyList extends ArrayList<Property> {

private static final long serialVersionUID = -7854888805136619636L;

    PropertyList(){
        super();
    }

    PropertyList(Collection<Property> c){
        super(c);
    }

    boolean containsProperty(PropertyList<Property> pl){
        Property asdf = (Property) this.get(4);
        System.out.println(asdf.<can't access "Property" methods>);  //mark
        return false;
    }

}

您会知道它“正常”工作,因为您的 IDE 会阻止您导入 Property 类。

如果您不想将其限制在您的 Property 类中,请使用

PropertyList<T> extends ArrayList<T>

但是您将获得与现在完全相同的行为 - 这意味着没有方法查找/可用性,因为 T 可以是任何东西,并且(我认为)默认/推断为 Object。

第一种方法应该会在标记行中显示getName() 方法。

真正的问题解决者是这样的:

PropertyList<T extends Property> extends ArrayList<T>

它实际上使用了泛型类型 T,它扩展了您的 Property 类,而不是像在您的初始解决方案/尝试中那样隐藏它。

【讨论】:

  • 好答案。如果可以的话,对于后一部分,Generic T 可以像这样限制为 Property:PropertyList&lt;T extends Property&gt; extends ArrayList&lt;T&gt; 以保留任何 Property 子类型的行为。它有时很有帮助!
  • 这很容易...谢谢!不知道您实际上可以使用这样的泛型进行扩展
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-28
  • 1970-01-01
  • 2020-06-05
  • 2015-04-29
  • 2015-05-08
  • 2011-05-31
  • 1970-01-01
相关资源
最近更新 更多