简体   繁体   English

Java局部类

[英]java partial classes

Small preamble. 小序言。 I was good java developer on 1.4 jdk. 我是1.4 jdk上的优秀Java开发人员。 After it I have switched to another platforms, but here I come with problem so question is strongly about jdk 1.6 (or higher :) ). 之后,我切换到了另一个平台,但是在这里我遇到了问题,因此问题主要是关于jdk 1.6(或更高版本:))。 I have 3 coupled class, the nature of coupling concerned with native methods. 我有3个耦合类,耦合的本质与本机方法有关。 Bellow is example of this 3 class 波纹管是这3类的例子

public interface A
{
     public void method();
}
final class AOperations
{
     static native method(. . .);
}
public class AImpl implements A
{
    @Override
    public void method(){ 
        AOperations.method( . . . );
    }
}

So there is interface A, that is implemented in native way by AOperations, and AImpl just delegates method call to native methods. 因此存在接口A,它由AOperations以本机方式实现,而AImpl仅将方法调用委托给本机方法。 These relations are auto-generated. 这些关系是自动生成的。 Everything ok, but I have stand before problem. 一切都很好,但我站在问题面前。 Sometime interface like A need expose iterator capability. 有时像A这样的接口需要公开迭代器功能。 I can affect interface, but cannot change implementation (AImpl). 我可以影响界面,但是不能更改实现(AImpl)。

Saying in C# I could be able resolve problem by simple partial: (C# sample) 在C#中说,我可以通过简单的部分解决问题:(C#示例)

partial class AImpl{
 ... //here comes auto generated code
} 

partial class AImpl{
 ... //here comes MY implementation of 
 ... //Iterator 
} 

So, has java analogue of partial or something like. 因此,有部分或类似的Java模拟。

EDITED : According to comment by @pgras I need some clarification. 编辑 :根据@pgras的评论,我需要澄清一下。 AImpl is not in vacuum, there is some factory (native implemented) that returns instance of AImpl, that is why creation of inheritance from AImpl, is not applicable. AImpl并非处于真空状态,有一些工厂(本机实现)返回AImpl的实例,这就是为什么从AImpl继承的创建不适用的原因。

EDITED 2 : May be it doesn't relate, but how it is done by JUnit 4: 编辑2 :也许它不相关,但是JUnit 4是如何完成的:

public class SomeTest {
 ...
 //there is no direct inheritance from Assert, but I can use follow:
 assertTrue(1==1); //HOW DOES it works??

Java does not have support for partials or open classes. Java不支持局部或开放类。 Other JVM languages do, but not Java. 其他JVM语言也可以,但是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. 您可以让AImpl接受另一个对象,这些对象实现这些扩展方法的接口。 The generated AImpl would then have generated methods such as iterator methods that it could delegate to the user created object you pass in. 然后,生成的AImpl将具有生成的方法(例如迭代器方法),可以将其委托给您传入的用户创建的对象。

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 文件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 文件Compute $ .java

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

file Compute$Add.java 文件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 文件Compute $ Sub.java

public class Compute$Sub extends Compute$ {
    public Compute$Sub(Compute c) {
        super(c);
    }

    public int sub() {
        return $that.a - $that.b;
    }
}

您可以扩展A(例如,接口B扩展A)并扩展AImpl和实现B(类BImpl扩展AImpl实现B)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM