Calling constructor of a generic type

后端 未结 6 1705
借酒劲吻你
借酒劲吻你 2020-12-10 12:48

If I have an abstract class like this:

public abstract class Item
{
    private Integer value;
    public Item()
    {
        value=new Integer(0);
    }
           


        
6条回答
  •  佛祖请我去吃肉
    2020-12-10 13:05

    It's impossible to use T to call a constructor because if it would be possible than after a compilation you would get the code like this:

    public class Box{
        Object item;
        public Box(){
            item = new Object();
        }
    }
    

    So if you would use this code and pass some object than you expect that there is the constructor of some specific type is called, but instead you get the Object constructor.

提交回复
热议问题