开发者

How to pass generic parameters in Java

开发者 https://www.devze.com 2023-03-26 07:38 出处:网络
In Java I have two classes: Class A { public String ID; public Object Name; } Class B { public String ID; public Object 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 g开发者_高级运维et 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?


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.


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
}


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;
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消