java partial classes

后端 未结 3 1731
太阳男子
太阳男子 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:07

    You could extend A (say interface B extends A) and extend AImpl and implement B (class BImpl extends AImpl implements B)...

    0 讨论(0)
  • 2020-12-11 05:18

    Java does not have support for partials or open classes. Other JVM languages do, but not Java. In your example, the simplest thing may unfortunately be to use delegation. You can have your AImpl take another object that fulfills an interface to these extension methods. The generated AImpl would then have generated methods such as iterator methods that it could delegate to the user created object you pass in.

    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题