java partial classes

后端 未结 3 1738
太阳男子
太阳男子 2020-12-11 04:36

Small preamble. I was good java developer on 1.4 jdk. After it I have switched to another platforms, but here I come with problem so question is strongly about jdk 1.6 (or h

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 05:20

    How about that: 
    Compute.java  =    your class
    Compute$.java  =   base class for partial classes. Reference a Compute object
    Compute$Add.java = your partial class. Subclass Compute$.
    Compute$Sub.java = your partial class. Subclass Compute$.
    

    file Compute.java

    public class Compute {
        protected int a, b;
        Compute$Add add;
        Compute$Sub sub;
    
        public Compute() {
            add = new Compute$Add(this);
            sub = new Compute$Sub(this);
        }
    
        public int[] doMaths() {
            int radd = add.add();
            int rsub = sub.sub();
            return new int[] { radd, rsub };
        }
    }
    

    file Compute$.java

    public abstract class Compute$ {
        protected Compute $that;
        public Compute$(Compute c){
            $that=c;
        }
    }
    

    file Compute$Add.java

    public class Compute$Add extends Compute$ {
        public Compute$Add(Compute c) {
            super(c);
            // TODO Auto-generated constructor stub
        }
    
        public int add(){
            return $that.a+$that.b;
        }
    }
    

    file Compute$Sub.java

    public class Compute$Sub extends Compute$ {
        public Compute$Sub(Compute c) {
            super(c);
        }
    
        public int sub() {
            return $that.a - $that.b;
        }
    }
    

提交回复
热议问题