简体   繁体   English

一种在java中实现部分类的方法

[英]A way to implement partial classes in java

I have an interface that I want to implement in separate classes after doing a quick google search apparently, Java doesn't have partial classes. 我有一个界面,我想在快速谷歌搜索后在单独的类中实现,显然,Java没有部分类。 Is there a way that I can do this or am I stuck throwing all of my code into one class? 有没有办法可以做到这一点,还是我把所有代码都扔进一个类?

Basically, I am trying to write a service. 基本上,我正在尝试编写服务。 Some of the service methods really belong in their own class and seem kind of illogical in the same class. 一些服务方法确实属于他们自己的类,并且在同一个类中看起来有点不合逻辑。 Here is an example of what I am trying to do. 这是我想要做的一个例子。

package com.upmc.esdm.messaging.epcd.service;
import java.util.List;

import javax.ejb.Remote;

import com.upmc.esdm.messaging.epcd13jpa.entities.EmailDomainTrust;


@Remote
public interface MessagingInterfaceRemote {
public List<EmailDomainTrust> getEmailDomains();

    public int upDateQualityTable();

    public string isLogial();

    public byte[] ToWritePartialClassesInJava();
}

I would normally have partial classes in C# and I would put similar methods that return similar values into one partial class (or maybe classes that update a record in one class). 我通常会在C#中使用部分类,我会将类似的值返回到一个部分类(或者更新一个类中的记录的类)。 How would I implement this? 我该如何实现? Or should I just put all the method implementations into one class? 或者我应该将所有方法实现放在一个类中?

Thanks. 谢谢。

There is nothing like partial classes in Java. Java中没有类似于部分类的东西。 You can achieve many of the same benefits using aggregation, delegation, and abstract base classes. 您可以使用聚合,委派和抽象基类实现许多相同的好处。

(I have caved to peer pressure and eliminated the “thankfully” remark that has generated so much heat in the comments. Evidently that little aside seems to have earned me four downvotes, despite being irrelevant to the answer.) (我已经屈服于同伴的压力,消除了在评论中产生了如此多热度的“谢天谢地”的评论。显然,尽管与答案无关,但小小的旁边似乎已经为我赢了四个赞成票。)

Aspectj can be the answer to C#/.net partial class feature! Aspectj可以是C#/ .net部分类功能的答案! Spring Roo is one of the typical development framework using aspectj to divide a class functionalities into several different files. Spring Roo是使用aspectj将类功能划分为几个不同文件的典型开发框架之一。

Before going that route, you might want to consider the Builder design pattern . 在走这条路线之前,您可能需要考虑Builder设计模式 That allows your service to aggregate implementations that are implemented in separate classes. 这允许您的服务聚合在单独的类中实现的实现。 No need for a pre-compiler, and very clean from an OO perspective. 从OO的角度来看,不需要预编译器,而且非常干净。

Each component class has a specific, narrow responsibility, with the service class enlisting the services of it's component classes to implement just the responsibility of the service. 每个组件类都有一个特定的,狭窄的责任,服务类使用它的组件类的服务来实现服务的责任。

If you really want to ( not recommended ), you can use a pre-processor to assemble parts of a class prior to compilation. 如果你真的想( 不推荐 ),你可以在编译之前使用预处理器来组装类的一部分。

The only scenario where a pre-processor might make sense is if some of your implementation is code-generated and other parts are hand-coded. 预处理器可能有意义的唯一情况是,您的某些实现是代码生成的,而其他部分是手工编码的。 A straightforward approach might be to #include the externally defined class fragments in your main .java file and run a C pre-processor over the file before compilation. 一个简单的方法可能是#include主.java文件中的外部定义的类片段,并在编译之前在文件上运行C预处理器。

You can declare methods trough multiple interfaces and then let your concrete classes implement multiple interfaces. 您可以通过多个接口声明方法,然后让您的具体类实现多个接口。

More over, using java.lang.Proxy you can assemble your service from multiple interfaces and delegate actual method calls to a separate implementation. 此外,使用java.lang.Proxy,您可以从多个接口组装服务,并将实际方法调用委托给单独的实现。

I hate to bring up an old thread, but thought I would bring a new perspective in respect to the given answers. 我讨厌提出一个旧线程,但我想我会给出一个关于给定答案的新观点。 Aspect Oriented programming may better achieve what you're trying to implement cross-cutting concerns. 面向方面的编程可以更好地实现您尝试实现跨领域关注的问题。

You're looking for a robust associative solution where you can parse out the implementing details to different controlling methods. 您正在寻找一种强大的关联解决方案,您可以将实施细节解析为不同的控制方法。 The Spring framework is very prominent in this space and often is associated with micro-services these days. Spring框架在这个领域非常突出,并且现在经常与微服务相关联。

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

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