How to assign super to a variable?

前端 未结 4 1943
长发绾君心
长发绾君心 2021-01-22 07:52

I\'d like to do the following:

public class Sub extends Super {
  public Sub(Super underlying) {
    if (underlying == null) {
      underlying = super; // this          


        
4条回答
  •  长发绾君心
    2021-01-22 08:51

    public class Sub extends Super {
      public Sub(Super underlying) {
        this.underlying = (underlying == null)
            ? new Super()
            : underlying;
      }
    
      @Override
      public void method() {
        underlying.method();
      }
    }
    

    Since another object is being created, it's not exactly the same, but it behaves as expected.

提交回复
热议问题