简体   繁体   English

Java中的静态方法和接口

[英]Static methods and interfaces in Java

I Have a UserEntity class which implements the IUserEntity interface. 我有一个实现IUserEntity接口的UserEntity类。

In the UserEntity class I have a static map: UserEntity类中,我有一个静态映射:

private static Map<IUserEntity.IIdentifiable, IUserEntity> staticUserEntityMap = new HashMap<>();

In the IUserEntity interface I would like to write a method like that: IUserEntity接口中,我想编写一个这样的方法:

public Collection<IUserEntity>      getUsers();

And in the class : 在课堂上:

public static Collection<IUserEntity> getUsers(){
    return staticUserEntityMap.values();
}

But I can't declare static methods in the interface and I can't change the method signature in the UserEntity class. 但是我无法在接口中声明静态方法,也无法在UserEntity类中更改方法签名。

How can I do this ? 我怎样才能做到这一点 ?

In Java 8 you can have default implementations in interface but I believe that will not solve your problem. 在Java 8中,您可以在界面中使用默认的实现,但是我相信这不会解决您的问题。 Instead of changing the method signatures you can create a separate static method and call it with the class name within getUsers implementation. 无需更改方法签名,您可以创建一个单独的静态方法,并在getUsers实现中使用类名对其进行调用。 eg 例如

Create new method: 创建新方法:

public static Collection<IUserEntity> getUsersStatic() {
   return staticUserEntityMap.values();
}

Call this method from getUsers : getUsers调用此方法:

public Collection<IUserEntity>      getUsers() {
  return UserEntity.getUsersStatic();
}

I'd refer you to this question , and the docs : 我将向您介绍这个问题以及docs

In addition to default methods, you can define static methods in interfaces. 除了默认方法外,您还可以在接口中定义静态方法。 (A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.) This makes it easier for you to organize helper methods in your libraries; (静态方法是与其定义的类相关联的方法,而不是与任何对象相关联的方法。该类的每个实例都共享其静态方法。)这使您更轻松地在库中组织帮助程序方法; you can keep static methods specific to an interface in the same interface rather than in a separate class. 您可以将特定于接口的静态方法保留在同一接口中,而不是在单独的类中。

You could, however, implement the field in the interface as well, as that would allow you to implement your simple static method. 但是,您也可以在接口中实现该字段,因为这将允许您实现简单的静态方法。 Downside would be that the Map would become public. 不利之处在于该地图将公开。 It'd look like this: 看起来像这样:

public interface  IUserEntity {
// ...

 static Map<IUserEntity.IIdentifiable, IUserEntity> staticUserEntityMap = new HashMap<>();

 static Collection<IUserEntity> getUsers(){
   return staticUserEntityMap.values();
 }
}

you can create abstract SkeletonUserEntity(or AbstractUserEntity) class where you would define this getUser method and all another general methods. 您可以创建抽象SkeletonUserEntity(或AbstractUserEntity)类,在其中定义此getUser方法和所有其他常规方法。 All classes have to implemeent IUserEntity you extends from SkeletonUserEntity 所有类都必须实现从SkeletonUserEntity扩展的IUserEntity

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

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