Java中如何传递泛型参数

发布于 2024-11-28 00:09:47 字数 467 浏览 0 评论 0原文

在 Java 中,我有两个类:

Class A
{
  public String ID;
  public Object Name;
}

Class B
{
  public String ID;
  public Object Name;
}

我想要一个方法,可以将 A 类或 B 类对象传递给它:

public void SomeMethod(???  arg)
{
  String id = arg.ID;
  Object name= arg.Name;
}

是否可以将 A 类或 B 类对象传递给此方法?如果是的话,方法的签名怎么写?

我能想到的唯一解决方案是创建一个 A 类和 B 类都实现的接口,其中包含 get 和 set 方法来设置字段 ID 和 Name。那么该方法的签名将是一个类型为接口的参数。我希望也许有一种更简单的方法,可能使用泛型?

In Java I have two classes:

Class A
{
  public String ID;
  public Object Name;
}

Class B
{
  public String ID;
  public Object Name;
}

I want to have a method where I can pass it either a Class A or B object:

public void SomeMethod(???  arg)
{
  String id = arg.ID;
  Object name= arg.Name;
}

Is it possible to pass an object of either class A or B to this method? If so, how is the method's signature written?

The only solution I can think of is to create an interface that both Class A and B implements containing get and set methods to set the fields ID and Name. Then the method's signature would be a parameter whose type is the interface. I was hoping that maybe there is a simpler way, possibly with generics?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

下雨或天晴 2024-12-05 00:09:47

您需要使用具有适当方法签名的接口(或抽象类)是正确的。对于java来说,这两个类是不同的,除了Object之外没有任何共同点。您需要创建一个类层次结构来反映它们之间的共性。

You are correct with needing to use an interface (or an abstract class) with the appropriate method signatures. To java the two class are different with nothing (beside Object) in common. You need to create a class hierarchy refelecting the commonality between them.

三人与歌 2024-12-05 00:09:47

使用方法重载

public void SomeMethod(A arg)
{
  String id = arg.ID;
  Object name= arg.Name;
}

public void SomeMethod(B arg)
{
  String id = arg.ID;
  Object name= arg.Name;
}

您可以创建一个接口并让 AB 实现它。这实际上取决于您的应用程序。对于小型程序,我会坚持使用方法重载,因为它只会在程序中引入不必要的抽象。

对于优先考虑可扩展性的大型应用程序,您可能需要考虑使用接口。假设稍后您要编写也具有 SomeMethod() 的类 CD。使用接口使您不必一遍又一遍地检查整个代码并重载适当的方法。

如果您确定 AB 就是故事的结局,那么就没有必要制作界面。

编辑:如果有很多代码需要重复,那么创建一个辅助方法:

public void SomeMethod(A arg)
{
  HelpMePlease( arg.ID, arg.Name );
}

public void SomeMethod(B arg)
{
  HelpMePlease( arg.ID, arg.Name );
}

private void HelpMePlease( String id, Object name ) {
  // 1000 lines of code here
}

Use method overloading.

public void SomeMethod(A arg)
{
  String id = arg.ID;
  Object name= arg.Name;
}

public void SomeMethod(B arg)
{
  String id = arg.ID;
  Object name= arg.Name;
}

You could make an interface and have A and B implement it. It really depends on your application. For small programs, I would just stick with method overloading since it just introduces unnecessary abstraction into your program.

For larger applications where extensibility is a priority, you may want to consider using an interface. Suppose later on you want to write classes C and D which also have SomeMethod(). Using an interface makes it so that you don't have to go through your entire code and overload appropriate methods over and over again.

If you know for sure that A and B are the end of the story, then there's no need to make an interface.

EDIT: If there's a lot of code to be duplicated, then make a helper method:

public void SomeMethod(A arg)
{
  HelpMePlease( arg.ID, arg.Name );
}

public void SomeMethod(B arg)
{
  HelpMePlease( arg.ID, arg.Name );
}

private void HelpMePlease( String id, Object name ) {
  // 1000 lines of code here
}
孤独患者 2024-12-05 00:09:47

您不需要泛型类型。简单的继承就可以完成这项工作

abstract class Base {
  public String ID;
  public Object Name;
}

class A extends Base {
}

class B extends Base {
}

public void SomeMethod(Base arg)
{
  String id = arg.ID;
  Object name= arg.Name;
}

You don't need generic types. Simple inheritance will do the job

abstract class Base {
  public String ID;
  public Object Name;
}

class A extends Base {
}

class B extends Base {
}

public void SomeMethod(Base arg)
{
  String id = arg.ID;
  Object name= arg.Name;
}
兲鉂ぱ嘚淚 2024-12-05 00:09:47

泛型旨在提高编译期间的类型安全性。

您所问的问题似乎类似于 C++ 概念或各种其他语言的鸭子类型

在 Java 中,如果需要对两种不同的类型执行某些操作序列,则需要引入接口或诉诸脚本/反射。

Generics are intended to improve type safety during compilation.

What you are asking about seems to be something akin to C++ concepts or various other languages' duck typing.

In Java, if some sequence of operations need to be performed on two disparate types, you need to introduce an interface or resort to scripting/reflection.

z祗昰~ 2024-12-05 00:09:47

定义两个接口 hasID 和 hasName,然后:

public class MyClass<A extends hasID & hasName>{
    public void SomeMethod(A object) {
        String id = object.getID();
        Object name= object.getName();
    }
}

其中 getID 和 getName 定义在各自的接口上。

Define two interfaces, hasID and hasName, and then:

public class MyClass<A extends hasID & hasName>{
    public void SomeMethod(A object) {
        String id = object.getID();
        Object name= object.getName();
    }
}

Where getID and getName are defined on their respctive interfaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文