How to assign super to a variable?

前端 未结 4 1938
长发绾君心
长发绾君心 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:35

    It looks like you want to implement the delegation pattern.

    Simple extend Super, and let your IDE override all methods with the creation of super calls.

    Then replace "super." with "underlying."

    Error prone, but that's it.

    public class Sub extends Super {
        Super underlying;
        public Sub(Super underlying) {
            this.underlying = underlying;
        }
    
        @Override
        public void f() {
            underlying.f();
        }
    

提交回复
热议问题