Creating instance list of different objects

前端 未结 7 1614
自闭症患者
自闭症患者 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 objects = new ArrayList();
    
    
    

    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 employeeList = new ArrayList();
    

    提交回复
    热议问题