Creating instance list of different objects

前端 未结 7 1484
自闭症患者
自闭症患者 2021-02-04 08:17

I\'m tring to create an arraylist of different class instances. How can I create a list without defining a class type? ()

List

        
相关标签:
7条回答
  • 2021-02-04 09:21
    List<Object> objects = new ArrayList<Object>();
    

    objects list will accept any of the Object

    You could design like as follows

    public class BaseEmployee{/* stuffs */}
    
    public class RegularEmployee extends BaseEmployee{/* stuffs */}
    
    public class Contractors extends BaseEmployee{/* stuffs */}
    

    and in list

    List<? extends BaseEmployee> employeeList = new ArrayList<? extends BaseEmployee>();
    
    0 讨论(0)
提交回复
热议问题