继承内部类

最近刚好碰到一个场景,需要继承某个类的内部类。Thinking in Java是这样描述的:

Because theinner-class constructor must attach to a reference of the enclosing classobject,
things are slightly complicated when you inherit from an inner class. Theproblem is that the
"secret" reference to the enclosing class object must beinitialized, and yet in the derived class
there’s no longer a default object to attach to. You must use a special syntaxto make the
association explicit:
//: innerclasses/InheritInner.java
// Inheriting an inner class.
class WithInner {
class Inner {}
}
public class InheritInner extends WithInner.Inner {
//! InheritInner() {} // Won’t compile
InheritInner(WithInner wi) {
wi.super();
}
public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
} ///:~
You can see that InheritInner is extending only the innerclass, not the outer one. But when
it comes time to create a constructor, the default one is no good, and youcan’t just pass a
reference to an enclosing object. In addition, you must use the syntax
enclosingClassReference.super();
inside the constructor. This provides the necessary reference, andthe program will then
compile.

 

测试代码:一个内部类继承另外一个类的内部类。

MyQuery:

importjava.io.Serializable;

importjava.lang.reflect.Array;

importjava.util.ArrayList;

importjava.util.List;

 

public classMyQuery implements Serializable{

    public List<Constraint> constraints;

    public String sortOrder = "DESC";

   

    public MyQuery(){

       constraints = newArrayList<Constraint>();

    }

   

    public List<Constraint>getConstraints(){

       return this.constraints;

    }

   

    public void addConstraint(Stringcolnm,String op, String value) {

       Constraint c = new Constraint();

       c.setColumnName(colnm);

       c.setOperator(op);

       c.setValue(value);

       this.getConstraints().add(c);

      

    }

    public void addConstraint(Constraint con) {

       constraints.add(con);

    }

   

    public class Constraint implementsSerializable {

       private String columnName;

        private String operator;

        private String value;

        final MyQuery qr;

 

        public Constraint() {

        qr= MyQuery.this;

         System.out.println("*** Constraintconstructor,query.sortOrder:" + qr.sortOrder);

        }

       

       public String getColumnName()

        {

            return columnName;

        }

 

        public void setColumnName(StringcolumnName)

        {

            this.columnName = columnName;

        }

 

        public String getOperator()

        {

            return operator;

        }

 

        public void setOperator(Stringoperator)

        {

            this.operator = operator;

        }

 

        public String getValue()

        {

            return value;

        }

       

        public void setValue(String val)

        {

            this.value = val;

        }

       

        public String toString()

        {

            StringBuffer sb = new StringBuffer(512);

            sb.append("Constraint");

            sb.append("[");

           sb.append("columnName=").append(columnName).append(",");

           sb.append("operator=").append(operator).append(",");

            sb.append("value=");

            valueToString(sb, value);

            sb.append(", ");

            sb.append("]");

            return sb.toString();

        }

       

        protected StringBuffervalueToString(StringBuffer sb, Object value)

        {

            if(value == null)

                sb.append("null");

            else

            if(value.getClass().isArray())

            {

                int length =Array.getLength(value);

                sb.append("{");

                for(int i = 0; i < length;i++)

                {

                    Object itemValue =Array.get(value, i);

                    if(i > 0)

                       sb.append(",");

                    sb.append(itemValue);

                }

 

                sb.append("}");

            } else

            {

                sb.append(value);

            }

            return sb;

        }

 

 

    }

}

 

ExtMyQuery:

import java.util.ArrayList;

import java.util.List;

 

publicclass ExtMyQuery {

    public List<ExtConstraint> constraints;

   

    public ExtMyQuery(){

       constraints = new ArrayList<ExtConstraint>();

    }

   

    public List<ExtConstraint> getConstraints(){

       returnthis.constraints;

    }

   

    publicvoid addConstraint(MyQuery mq, String colnm, String op,String value,String coltype) {

       ExtConstraint c = new ExtConstraint(mq);

       c.setColumnName(colnm);

       c.setOperator(op);

       c.setValue(value);

       c.setColType(coltype);

       this.getConstraints().add(c);

      

    }

    publicvoid addConstraint(ExtConstraint con) {

       constraints.add(con);

    }

   

    class ExtConstraint extends MyQuery.Constraint {

       public String colType;

      

       public ExtConstraint(MyQuery mq){

           mq.super();

       }

 

       public String getColType() {

           returncolType;

       }

 

       publicvoid setColType(String colType) {

           this.colType = colType;

       }

    }

 

}

 

 

Test:

publicclass Test{

 

    publicstaticvoid main(String[] args){

       MyQuery mq = new MyQuery();

       ExtMyQuery emq = new ExtMyQuery();

       emq.addConstraint(mq, "taskId","Equals", "23456", "Integer");

       List<ExtMyQuery.ExtConstraint> cons =emq.getConstraints();

      

       for (ExtMyQuery.ExtConstraint con : cons){

           System.out.println("colname:" + con.getColumnName() + ",operator:" +con.getOperator() + ",value:" + con.getValue() + ",coltype:" +con.getColType());

       }

      

    }

   

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值