Java 不同类类型的重用增强循环

Java 不同类类型的重用增强循环,java,generics,for-loop,Java,Generics,For Loop,我希望这不是重复,因为我找不到任何与我所寻找的完全相符的东西。我有一段代码,在填充大型地图时会重复这些代码 不同代码块之间的唯一区别是循环通过的类 例如: for (SomeClass class : SomeClassList) { // Repeated code here. } for (AnotherClass aClass : AnotherClassList) { // Repeated code here. } for (AndA

我希望这不是重复,因为我找不到任何与我所寻找的完全相符的东西。

我有一段代码,在填充大型地图时会重复这些代码
不同代码块之间的唯一区别是循环通过的类

例如:

for (SomeClass class : SomeClassList) {
         // Repeated code here.
    }
for (AnotherClass aClass : AnotherClassList) {
         // Repeated code here.
    }
for (AndAnotherClass fClass : AndAnotherClassList) {
         // Repeated code here.
    } 
etc...
所有这些类都共享两个方法,
getXYZ()
setXYZ()
。我曾尝试使用泛型,但多次失败。我想创建一个方法,在这个方法中,我可以传入不同类的不同列表,并在不同的列表上执行相同的代码

类似这样的内容:

public void anyList(List<?> list){
   for(classOfList item: list){
      //code here
   }
}
public void anyList(列表){
for(列表项的类别:列表){
//代码在这里
}
}

如果它们是您的类,请将
getXYZ
setXYZ
放在它们都实现的接口中(直接或从基类等),然后在增强的
for
循环中将该接口用作您的类型

interface HasXYZ {
    void setXYZ(XYZType xyz);
    XYZType getXYZ();
}

class SomeClass implements HasXYZ {
    // ...
}

class AnotherClass implements HasXYZ {
    // ...
}

class AndAnotherClass implements HasXYZ {
    // ...
}
然后您的
任意列表

public void anyList(List<HasXYZ> list){
    for (HasXYZ item : list){
        // code here
    }
}

如果它们是您的类,请将
getXYZ
setXYZ
放在它们都实现的接口中(直接或从基类等),然后在增强的
for
循环中将该接口用作您的类型

interface HasXYZ {
    void setXYZ(XYZType xyz);
    XYZType getXYZ();
}

class SomeClass implements HasXYZ {
    // ...
}

class AnotherClass implements HasXYZ {
    // ...
}

class AndAnotherClass implements HasXYZ {
    // ...
}
然后您的
任意列表

public void anyList(List<HasXYZ> list){
    for (HasXYZ item : list){
        // code here
    }
}
创建一个接口(SomeInterface),某个类、另一个类和另一个类等都将实现该接口。这个接口上应该有
getXYZ()
setXYZ()

然后你可以:

for (SomeInterface class : SomeClassList) {
         class.getXYZ();
}
创建一个接口(SomeInterface),某个类、另一个类和另一个类等都将实现该接口。这个接口上应该有
getXYZ()
setXYZ()

然后你可以:

for (SomeInterface class : SomeClassList) {
         class.getXYZ();
}

这有几个部分

首先:创建接口(或基类)

Second:您可以调用接口(或基类)列表中的someMethod

public void callsomethod(列出事物){
for(SomeInterface SomeInterface:things){
someInterface.someMethod();
}
}
列出一些接口列表=//列出一些
callSomeMethod(someInterfaceList)//这很有效!!!
List someClassList=//制作一些列表
callSomeMethod(someClassList)//这是一个错误:(
Third:如果需要使用SomeClass或SomeOtherClass的列表,则使用泛型

// I prefer the explicit type
public <T extends SomeInterface> void callSomeMethod(List<T> things) {
    for (T t : things) {
        t.someMethod();
    }
}

// This works as well
public void callSomeMethod(List<? extends SomeInterface> things) {
    for (SomeInterface t : things) {
        t.someMethod();
    }
}
//我更喜欢显式类型
public void callsomethod(列出内容){
对于(T:事物){
t、 somethod();
}
}
//这同样有效

public void callsomethod(List这有几个部分

首先:创建接口(或基类)

Second:您可以调用接口(或基类)列表中的someMethod

public void callsomethod(列出事物){
for(SomeInterface SomeInterface:things){
someInterface.someMethod();
}
}
列出一些接口列表=//列出一些
callSomeMethod(someInterfaceList);//这很有效!!!
List someClassList=//制作一些列表
callSomeMethod(someClassList);//这是一个错误:(
Third:如果需要使用SomeClass或SomeOtherClass的列表,则使用泛型

// I prefer the explicit type
public <T extends SomeInterface> void callSomeMethod(List<T> things) {
    for (T t : things) {
        t.someMethod();
    }
}

// This works as well
public void callSomeMethod(List<? extends SomeInterface> things) {
    for (SomeInterface t : things) {
        t.someMethod();
    }
}
//我更喜欢显式类型
public void callsomethod(列出内容){
对于(T:事物){
t、 somethod();
}
}
//这同样有效

public void callsomethod(list您真的需要合并列表吗?您可以为每个列表调用一个方法。@AndyTurner:是的,肯定更好。op也提到了这样做,那么我在哪里?:-)你真的需要合并列表吗?你可以为每个列表调用一个方法。@AndyTurner:是的,肯定更好。op也提到了这样做,那么我在哪里?:-)