Java 将通过继承创建的对象添加到链表并实现其方法
我试图将不同“类型”的对象存储在链接列表中。我有几个独特的“类型”类,它们继承自Java 将通过继承创建的对象添加到链表并实现其方法,java,Java,我试图将不同“类型”的对象存储在链接列表中。我有几个独特的“类型”类,它们继承自ProtoType(这里只显示一个,总共有5种类型) 当我创建一个新对象时,我可以使用“type1.someMethods”访问其中的所有方法。我不知道的是,如何遍历列表并根据它们在列表中的位置获得每个不同“类型”的方法。我想我可以使用“typeList.get(int index).someMethods()”。我只能使用与LinkedList关联的方法 父类 public class ProtoType { p
ProtoType
(这里只显示一个,总共有5种类型)
当我创建一个新对象时,我可以使用“type1.someMethods”访问其中的所有方法。我不知道的是,如何遍历列表并根据它们在列表中的位置获得每个不同“类型”的方法。我想我可以使用“typeList.get(int index).someMethods()”。我只能使用与LinkedList关联的方法
父类
public class ProtoType {
private int ID;
private int x;
private String type;
private int randomNumber;
public ProtoType(int ID, int x, String type ) {
this.ID = ID;
this.x = x;
this.type = type;
this.randomNumber = randomNumber;
}
public int getID() {
return ID;
}
public int getX() {
return x;
}
public void randomNumberGenerator(int max, int min) {
Random r = new Random();
randomNumber = r.nextInt(max - min + 1) + 1;
}
public int getRandomNum() {
return randomNumber;
}
}
儿童班
public class Type1 extends ProtoType {
public Type1(int ID, int x, String type) {
super(ID, x, type);
}
public void preformAction(){
System.out.println(getRandomNum());
switch (getRandomNum()){
case 1:
case 2:
//Some action
break;
case 3:
//some action
break;
case 4:
//some action
break;
}
}
}
主类
import java.util.LinkedList;
public class TestMAin {
public static void main(String[] args) {
LinkedList typeList = new LinkedList();
Type1 t1 = new Type1(1, 12, "type1");
typeList.add(0, t1);
Type1 t2 = new Type1(2, 13, "type1");
typeList.add(1, t2);
}
//////////////
// the issue
//iterate thru the array get the type
//implement the methods of that type
/////////////
}
根据一件事,我有两个建议。您的继承类是否使用方法重写 例如:
public class ParentClass {
public void method() {}
}
public class ChildA extends ParentClass {
@Override
public void method() {}
}
public class ChildB extends ParentClass {
@Override
public void method() {}
}
public class Main {
public static void main(String[] args) {
ArrayList<ParentClass> list = new ArrayList<>();
list.add(new ChildA());
list.add(new ChildB());
for (int i = 0; i < list.size(); i++) {
list.get(i).method(); //Will class the appropriate sub-classes implementation of method().
}
}
}
公共类父类{
public void方法(){}
}
公共类ChildA扩展了ParentClass{
@凌驾
public void方法(){}
}
公共类ChildB扩展了ParentClass{
@凌驾
public void方法(){}
}
公共班机{
公共静态void main(字符串[]args){
ArrayList=新建ArrayList();
添加(new ChildA());
添加(新的ChildB());
对于(int i=0;i
如果您不希望使用重写的方法,那么instanceof操作符可能就是您要查找的对象
public class Main {
public static void main(String[] args) {
ArrayList<ParentClass> list = new ArrayList<>();
list.add(new ChildA());
list.add(new ChildB());
for (int i = 0; i < list.size(); i++) {
ParentClass obj = list.get(i);
if (obj instanceof ChildA) {
obj.childAMethod();
}
else if (obj instanceof ChildB) {
obj.childBMethod();
}
}
}
}
公共类主{
公共静态void main(字符串[]args){
ArrayList=新建ArrayList();
添加(new ChildA());
添加(新的ChildB());
对于(int i=0;i
如果您发现自己依赖instanceof运算符,您可能需要查看您的程序结构,因为它不被认为是OOP设计的最佳选择。您想知道它实现了哪些方法?为什么不检查它是哪个类的实例?这是一个封装在功能请求中的设计错误。您实际上是什么尝试做什么?当然有一个比拥有一个非类型化的对象列表更好的方法。最后,我需要在列表中循环,并基于对象,我需要实现某些方法。因此,如果我在索引12,我会得到对象类型,并基于类型实现某些操作。随着我的移动,一些实例将被删除,而其他实例将被删除我需要一种方法来存储、检索和删除不同的实例,然后实现它们的方法。