java java部分类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2501736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java partial classes
提问by Dewfy
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 higher :) ). I have 3 coupled class, the nature of coupling concerned with native methods. Bellow is example of this 3 class
小序言。我是 1.4 jdk 上的优秀 Java 开发人员。之后我切换到另一个平台,但在这里我遇到了问题,所以问题是关于 jdk 1.6(或更高版本:))。我有 3 个耦合类,耦合的性质与本地方法有关。波纹管是这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. These relations are auto-generated. Everything ok, but I have stand before problem. Sometime interface like A need expose iterator capability. I can affect interface, but cannot change implementation (AImpl).
于是就有了接口A,它是由AOperations以原生方式实现的,而AImpl只是将方法调用委托给了原生方法。这些关系是自动生成的。一切正常,但我遇到了问题。有时像 A 这样的接口需要公开迭代器功能。我可以影响接口,但不能改变实现(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. 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.
编辑:根据@pgras 的评论,我需要澄清一下。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??
回答by Russell Leggett
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.
Java 不支持部分或开放类。其他 JVM 语言可以,但 Java 不行。在您的示例中,不幸的是,最简单的事情可能是使用委托。你可以让你的 AImpl 使用另一个对象来实现这些扩展方法的接口。生成的 AImpl 然后会生成方法,例如迭代器方法,它可以委托给您传入的用户创建的对象。
回答by Max Mba
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;
}
}
回答by pgras
You could extend A (say interface B extends A) and extend AImpl and implement B (class BImpl extends AImpl implements B)...
您可以扩展 A(比如接口 B 扩展 A)并扩展 AImpl 并实现 B(类 BImpl 扩展 AImpl 实现 B)...

