繁体   English   中英

无法从Java中的ArrayList访问对象的方法

[英]Can't access an object's methods from an ArrayList in Java

Java告诉我在尝试运行main方法时找不到getName()。 我不确定为什么,我环顾了一下stackexchange都没有用。 似乎是一个简单的解决方案,但我似乎无法弄清楚。

import java.util.*;

public class ArrayTester
{
    public static ArrayList<Object> array;

    public static void main(String args[])
{
        array = new ArrayList<Object>();
        array.add(new TestObject("Some Name"));
        System.out.println("Object Name: " + array.get(0).getName());
    }
}

宾语:

public class TestObject
{
    private String name;

    public TestObject(String cname){
        name = cname;
    }

    public String getName(){
        return name;
    }
}

感谢您的帮助。 如果在我之前没有看到相同的问题,我深表歉意。

Java告诉我在尝试运行main方法时找不到getName()

ObjectTestObject类中使用的方法TestObject类。 由于array.get(0)返回Object类作为对象。 没有这样的方法意味着Object类中存在getName()

更改

System.out.println("Object Name: " + array.get(0).getName());

System.out.println("Object Name: " + ((TestObject)array.get(0)).getName());

您创建了一个名为TestObject的类,因此应使用TestObject而不是Object创建数组:

public class ArrayTester
{
    public static ArrayList<TestObject> array;

    public static void main(String args[])
{
        array = new ArrayList<TestObject>();
        array.add(new TestObject("Some Name"));
        System.out.println("Object Name: " + array.get(0).getName());
    }
}

或至少投放:

System.out.println("Object Name: " + ((TestObject) array.get(0).getName());

当您想在ArrayList中存储不同类型的对象时,您应该找出它们的共同点。 例如,所有这些都有一个名称。 然后,您可以创建一个称为TestInterface的接口:

public interface TestInterface {

     String getName();
}

该接口定义了getName()方法以及实现该方法的所有类,也包含getName()。

TestClass应该实现TestInterface:

public class TestObject implements TestInterface {

     private String name;

     public TestObject(String cname) {
         name = cname;
     }

     public String getName() {
         return name;
     }
 }

您还可以编写一个OtherClass:

public class OtherClass implements TestInterface {

     public String getName() {
         return "OtherClass getName method";
     }
}

您的主要:

public class ArrayTester {
     public static ArrayList<TestInterface> array;

     public static void main(String args[]) {
          array = new ArrayList<TestInterface>();
          array.add(new TestObject("Some Name"));
          array.add(new OtherClass());
          for (TestInterface t: array) {
               System.out.println("Object Name: " + t.getName());
          }
     }
}

您可以将实现TestInterface的所有类的实例传递给ArrayList,并在对其进行迭代时,Java调用getName()方法的具体实现。

输出:

Some Name
OtherCalss getName method

Cast也可以工作,但类型安全。 在对象类型的ArrayList中,您可以传递各种对象。 将字符串传递给它时也可以。 它可以编译,但是当您尝试将String强制转换为TestObject时,将出现ClassCastException。

您可以使用instanceof函数并在适当的转换之前在if块中进行比较:

public static void main(String args[])
{
    String name;
    array = new ArrayList<Object>();
    array.add(new TestObject("Some Name"));
    for (Object o : array) {
        if(o instanceof TestObject){
            System.out.println("Object Name: " + ((TestObject) o).getName());
        } 
    }
}

和o是其他类的实例,您可以在if块中进行简单比较,并在转换后调用相应的函数,例如:

public static void main(String args[])
{
    String name;
    array = new ArrayList<Object>();
    array.add(new TestObject("Some Name"));
    for (Object o : array) {
        if(o instanceof TestObject){
            System.out.println("Object Name: " + ((TestObject) o).getName());
        } 
        else if(o instanceof SomeObject){
            System.out.println("Object Name: " + ((SomeObject) o).getSomeObjectFunction());
        }
    }
}

我希望这能解决您的问题。

暂无
暂无

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

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