简体   繁体   English

如何创建一些对象来存储在一个arrayList中,这是一个子类的属性,来自Java中的父类?

[英]How do I create some objects to store in an arrayList, which is a attribute of sub class, from a parent class in Java?

So I have 3 classes, Lair, LairLocation & Minion. 所以我有3个班,Lair,LairLocation&Minion。 I created an ArrayList, which is supposed to store Minion objects, and this is an attribute of LairLocation. 我创建了一个ArrayList,它应该存储Minion对象,这是LairLocation的一个属性。 I'm supposed to create some objects to store in ArrayList, from the parent class Lair. 我应该从父类Lair创建一些存储在ArrayList中的对象。 Both LairLocation and Minion and sub-classes of Lair. LairLocation和Minion以及Lair的子类。

Whenever I try to create objects to store in ArrayList form my parent, I keep getting an error saying 'minion cannot be resolved' and telling me to create a local variable etc. Please help 每当我尝试创建存储在我父母的ArrayList中的对象时,我就会收到一条错误,说'minion无法解决'并告诉我创建一个局部变量等。请帮忙

LairLocation 

'''
public class LairLocations extends Lair
{
        public static ArrayList<Minion> minions = new ArrayList<Minion>();
}
'''

Lair

'''
public class Lair
{
        public void createMinions() 
    {
        minions.add("12", "Mine", "Me");//This is giving me the error 
    }
}
'''

Minion
'''
public class Minion extends Lair
{

    private String id;
    private String fName;
    private String lName;

    public Minion(String Id, String fName, String lName) 
    {
        this.id = id;
        this.fName = fName;
        this.lName = lName;
    }
}
'''

You're trying to access a field of a subclass from its parent class. 您正在尝试从其父类访问子类的字段。 This does not work this way. 这不起作用。

You can only access fields of a parent class (if they're public or protected). 您只能访问父类的字段(如果它们是公共的或受保护的)。

So, you either have to move your minions field to the parent class Lair (and make it non-static, by the way), or access this object via LairLocation class: LairLocation.minions() (if the field is supposed to be static). 所以,你要么必须将你的minions字段移动到父类Lair (并且顺便使它成为非静态的),或者通过LairLocation类访问该对象: LairLocation.minions() (如果该字段应该是静态的) )。

Also, this line of code is incorrect: minions.add("12", "Mine", "Me"); 此外,这行代码不正确: minions.add("12", "Mine", "Me");

The add() method accepts only one element. add()方法只接受一个元素。

It should probably be: minions.add(new Minion("12", "Mine", "Me")); 应该是: minions.add(new Minion("12", "Mine", "Me"));

你必须调用new Minion()来获得一个新的奴才。

minions.add( new Minion( "12", "Mine", "Me") );

暂无
暂无

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

相关问题 来自父 class ZD52387880E1EA22817A72D37592138 的 arraylist 中的子 Class 对象的方法调用 - Method Calls from Child Class objects in arraylist of parent class Java 如何将对象从 ArrayList 移动到另一个类中的 RecyclerView? - How do I move Objects from an ArrayList to a RecyclerView in another Class? 如何在 class 中创建私有 ArrayList? (爪哇) - How do I create a private ArrayList within a class? (Java) 如何从一个类的一个arrayList到另一个类的arraylist获取一定数量的对象? - How do I get a certain number of objects from one arrayList in one class to an arraylist in another class? 如何将来自另一个类的对象存储在另一个类的 ArrayList 中? - How can I store objects from another class in an ArrayList from a different class? 如何创建一个可以在java中存储多个数据类型对象的arraylist类 - How to create an arraylist class that can store multiple data type objects in java 如何创建一个可以存储多个对象的arraylist类? - How to create an arraylist class that can store multiple objects? 如何在Java的同一数组中存储多级类对象? - How do I store multipule class objects in the same array in Java? Java ArrayLists:将父类和子类添加到同一arraylist中,解决方案 - Java ArrayLists: Adding parent class and sub class to the same arraylist, solutions 如何修改其他班级的arraylist? Java的 - How do I modify an arraylist from a different class? Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM