继承内部类

来源:互联网 发布:阿里云自建数据库 编辑:程序博客网 时间:2024/05/17 06:44

最近刚好碰到一个场景,需要继承某个类的内部类。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;

   

    publicExtMyQuery(){

       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 ExtConstraintextends MyQuery.Constraint {

       public StringcolType;

      

       public ExtConstraint(MyQuery mq){

           mq.super();

       }

 

       public String getColType() {

           returncolType;

       }

 

       publicvoid setColType(String colType) {

           this.colType = colType;

       }

    }

 

}

 

 

Test:

publicclassTest{

 

    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
原创粉丝点击