How to get the class of type variable in Java Generics

前端 未结 7 1447
北恋
北恋 2021-02-01 17:24

I\'ve seen similar questions but they didnt help very much.

For instance I\'ve got this Generic Class:

public class ContainerTest
{

    public          


        
7条回答
  •  故里飘歌
    2021-02-01 18:12

    The only way is to store the class in an instance variable and require it as an argument of the constructor:

    public class ContainerTest
    {
        private Class tClass;
        public ContainerTest(Class tClass) {
            this.tCLass = tClass;
        }
    
        public void doSomething()
        {
            //access tClass here
        }
    }
    

提交回复
热议问题