基于对象拆分为列表,不使用 instanceof

我直接进入正题。


我上了3节课。人,教授和学生。(角色,教授,校友)。教授和学生都从 Person 扩展。但是 Person 也可以被实例化,因为它不是抽象的。


我有 50 人,随机生成在一个列表中。它可以是任何类型的教授或学生。我想将它们分别分成不同的列表。


目前,我是这样做的:


for(Persona persona : personas) {

        if(persona instanceof Profesor) {

            profesores.add((Profesor) persona);

        }

        else if(persona instanceof Alumno) {

            alumnos.add((Alumno) persona);

        }

        else {

            nuevasPersonas.add(persona);

        }

    }

profesores 是教授名单


校友是学生名单


nuevasPersonas 是人员列表


哪个工作完美。但是有人告诉我不要使用instanceof,所以我不习惯。


关于如何在不使用 instanceof 的情况下将它们分成列表的任何想法?


谢谢。


UYOU
浏览 170回答 3
3回答

小怪兽爱吃肉

我会使用instanceof,你为什么不想习惯呢?无论如何,另一种方法可能是拥有一个变量类型(带有 getter,但没有 setter),亲自(=0),并在教授和学生中将其覆盖为 1 和 2。然后,您将测试变量而不是使用 instanceof。

慕容森

您可以使用每种对象类型重载“add”方法 - 类似于以下代码。main 方法只是将三个不同对象的实例添加到 ObjectSplitter 类中 - 它将它们分离到不同的集合中public class ObjectSplitter {public static void main(String ... args){&nbsp; &nbsp; ObjectSplitter d = new ObjectSplitter();&nbsp; &nbsp; d.addToCollection(new Object1());&nbsp; &nbsp; d.addToCollection(new Object1());&nbsp; &nbsp; d.addToCollection(new Object2());&nbsp; &nbsp; d.addToCollection(new Object1());&nbsp; &nbsp; d.addToCollection(new Object1());&nbsp; &nbsp; d.addToCollection(new Object1());&nbsp; &nbsp; d.addToCollection(new Object2());&nbsp; &nbsp; d.addToCollection(new Object3());&nbsp; &nbsp; d.addToCollection(new Object1());&nbsp; &nbsp; System.out.println("Num Ob1s : " + d.getOb1sSize());&nbsp; &nbsp; System.out.println("Num Ob2s : " + d.getOb2sSize());&nbsp; &nbsp; System.out.println("Num Ob3s : " + d.getOb3sSize());}private List<Object1> ob1s = new ArrayList<>();private List<Object2> ob2s = new ArrayList<>();private List<Object3> ob3s = new ArrayList<>();void addToCollection(Object1 o){&nbsp; &nbsp; ob1s.add(o);}void addToCollection(Object2 o){&nbsp; &nbsp; ob2s.add(o);}void addToCollection(Object3 o){&nbsp; &nbsp; ob3s.add(o);}int getOb1sSize(){&nbsp; &nbsp; return ob1s.size();}int getOb2sSize(){&nbsp; &nbsp; return ob2s.size();}int getOb3sSize(){&nbsp; &nbsp; return ob3s.size();}static class Object1 {}static class Object2 extends Object1 {}static class Object3 extends Object2 {}}

holdtom

您也可以使用 getClass().getSimpleName() 如下:public static void main (String[] args) throws java.lang.Exception{&nbsp; &nbsp; List<Test1> list = new ArrayList<>(2);&nbsp; &nbsp; Test1 o1 = new Test1();&nbsp; &nbsp; list.add(o1);&nbsp; &nbsp; Test2 o2 = new Test2();&nbsp; &nbsp; list.add(o2);&nbsp; &nbsp; for (Test1 test : list) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(test.getClass().getSimpleName());&nbsp; &nbsp; }}static class Test1{}static class Test2 extends Test1{}在 for 循环中,您可以使用如下代码的 if 条件来完成您的工作:for (Test1 test : list) {&nbsp; &nbsp; String className = test.getClass().getSimpleName();&nbsp; &nbsp; if(className.equals("Test1")) {&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Test1");&nbsp; &nbsp; } else if(className.equals("Test2")) {&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Test2");&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println();}根据覆盖方法的另一种解决方案是:public static void main (String[] args) throws java.lang.Exception{&nbsp; &nbsp; List<Test1> list = new ArrayList<>(2);&nbsp; &nbsp; Test1 o1 = new Test1();&nbsp; &nbsp; list.add(o1);&nbsp; &nbsp; Test2 o2 = new Test2();&nbsp; &nbsp; list.add(o2);&nbsp; &nbsp; for (Test1 test : list) {&nbsp; &nbsp; &nbsp; &nbsp; test.addToMyTypeList();&nbsp; &nbsp; }&nbsp; &nbsp; for(Test1 test : Test1.list) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(test.getClass().getSimpleName());&nbsp; &nbsp; }&nbsp; &nbsp; for(Test1 test : Test1.list) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(test.getClass().getSimpleName());&nbsp; &nbsp; }}static class Test1{&nbsp; &nbsp; public static List<Test1> list = new ArrayList<>();&nbsp; &nbsp; public void addToMyTypeList() {&nbsp; &nbsp; &nbsp; &nbsp; String className = test.getClass().getSimpleName();&nbsp; &nbsp; &nbsp; &nbsp; if(className.equals("Test1")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Test1.list.add(this);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}static class Test2 extends Test1{&nbsp; &nbsp; public static List<Test1> list = new ArrayList<>();&nbsp; &nbsp; @Override&nbsp; &nbsp; public void addToMyTypeList() {&nbsp; &nbsp; &nbsp; &nbsp; String className = test.getClass().getSimpleName();&nbsp; &nbsp; &nbsp; &nbsp; if(className.equals("Test2")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Test2.list.add(this);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java