returning a Void object

前端 未结 5 1270
故里飘歌
故里飘歌 2020-12-09 00:17

What is the correct way to return a Void type, when it isn\'t a primitive? Eg. I currently use null as below.

interface B{ E method();          


        
5条回答
  •  情话喂你
    2020-12-09 00:55

    There is no generic type which will tell the compiler that a method returns nothing.

    I believe the convention is to use Object when inheriting as a type parameter

    OR

    Propagate the type parameter up and then let users of your class instantiate using Object and assigning the object to a variable typed using a type-wildcard ?:

    interface B{ E method(); }
    
    class A implements B{
    
        public T method(){
            // do something
            return null;
        }
    }
    
    A a = new A();
    
        

    提交回复
    热议问题