java部分类 [英] java partial classes

查看:112
本文介绍了java部分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小序言。我是1.4 jdk的优秀java开发人员。在它之后,我已经切换到另一个平台,但在这里我遇到问题所以问题强烈关于jdk 1.6(或更高:))。我有3个耦合类,与本机方法有关的耦合性质。 Bellow是这个3级的例子

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

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

所以有接口A,用原生实现AOperations的方式,AImpl只是将方法调用委托给本机方法。
这些关系是自动生成的。一切都好,但我有问题。有时像A这样的接口需要暴露迭代器功能。我可以影响接口,但不能改变实现(AImpl)。

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).

在C#中说我可以通过简单的部分来解决问题:
(C#sample)

Saying in C# I could be able resolve problem by simple partial: (C# sample)

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

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

所以,有java类似的部分或类似的东西。

So, has java analogue of partial or something like.

EDITED
根据@pgras的评论我需要一些澄清。 AImpl不在真空中,有一些工厂(本机实现)返回AImpl的实例,这就是为什么从AImpl创建继承不适用。

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.

EDITED 2
可能与它无关,但JUnit 4是如何完成的:

EDITED 2: May be it doesn't relate, but how it is done by 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不支持部分或开放类。其他JVM语言可以,但不是Java。在您的示例中,最简单的事情可能是使用委托。您可以让AImpl使用另一个实现这些扩展方法接口的对象。生成的AImpl将生成方法,例如迭代器方法,它可以委托给您传入的用户创建的对象。

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部分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆