Java 基于参数类型的调用方法(当参数为常规或特定时)
如果我有两个方法,如何使用特定类型而不是常规类型调用方法 我准备了两个例子:简单和扩展Java 基于参数类型的调用方法(当参数为常规或特定时),java,oop,design-patterns,polymorphism,Java,Oop,Design Patterns,Polymorphism,如果我有两个方法,如何使用特定类型而不是常规类型调用方法 我准备了两个例子:简单和扩展 简单示例: public class Testing { static void process(Object object) { System.out.println("process Object"); } static void process(Integer integer) { System.out.println(&
简单示例:
public class Testing {
static void process(Object object) {
System.out.println("process Object");
}
static void process(Integer integer) {
System.out.println("process Integer");
}
public static void main(String[] args) {
Object objectString = new String("a");
Object objectInteger = new Integer(1);
process(objectString); // "process Object"
process(objectInteger); // it prints "process Object" instead of "process Integer"
}
}
public class Testing {
interface MyInterface {
}
static class First implements MyInterface {
String first = "first";
}
static class Second implements MyInterface {
String second = "second";
}
static class SpecificSecond extends Second {
String specificSecond = "specificSecond";
}
public static void process(MyInterface myInterface) {
if (myInterface instanceof First) {
System.out.println("General case: " + ((First) myInterface).first);
} else if (myInterface instanceof Second) {
System.out.println("General case: " + ((Second) myInterface).second);
} else {
System.out.println("Should not call");
}
}
public static void process(SpecificSecond specificSecond) {
System.out.println("Specific case for SpecificSecond: " + specificSecond.specificSecond);
}
public static void main(String[] args) {
MyInterface first = new First();
MyInterface second = new Second();
MyInterface specificSecond = new SpecificSecond();
Testing.process(first); // "General case: first"
Testing.process(second); // "General case: second"
Testing.process(specificSecond); // it prints "General case: second" instead of "Specific case for SpecificSecond: specificSecond"
}
}
我知道我可以创建具有特定类型的引用:
Integer objectInteger = new Integer(1);
将调用suit方法
但是我想使用常规类型(最好使用List=new ArrayList()
而不是ArrayList List=new ArrayList()
)
扩展示例:
public class Testing {
static void process(Object object) {
System.out.println("process Object");
}
static void process(Integer integer) {
System.out.println("process Integer");
}
public static void main(String[] args) {
Object objectString = new String("a");
Object objectInteger = new Integer(1);
process(objectString); // "process Object"
process(objectInteger); // it prints "process Object" instead of "process Integer"
}
}
public class Testing {
interface MyInterface {
}
static class First implements MyInterface {
String first = "first";
}
static class Second implements MyInterface {
String second = "second";
}
static class SpecificSecond extends Second {
String specificSecond = "specificSecond";
}
public static void process(MyInterface myInterface) {
if (myInterface instanceof First) {
System.out.println("General case: " + ((First) myInterface).first);
} else if (myInterface instanceof Second) {
System.out.println("General case: " + ((Second) myInterface).second);
} else {
System.out.println("Should not call");
}
}
public static void process(SpecificSecond specificSecond) {
System.out.println("Specific case for SpecificSecond: " + specificSecond.specificSecond);
}
public static void main(String[] args) {
MyInterface first = new First();
MyInterface second = new Second();
MyInterface specificSecond = new SpecificSecond();
Testing.process(first); // "General case: first"
Testing.process(second); // "General case: second"
Testing.process(specificSecond); // it prints "General case: second" instead of "Specific case for SpecificSecond: specificSecond"
}
}
我知道我能做到:
SpecificSecond specificSecond = new SpecificSecond();
但是如果不使用我的接口,我就不能使用其他泛型方法和类
如何更改方法流程(但不更改合同)以调用适当的方法
我找到了解决方法(创建新的代理方法以选择合适的方法并更改常规方法的名称):
但是有了这个,我无法在我的IDE中找到特定process(specificsond)
方法的所有用法(因为它们都通过代理方法)
是否有任何其他解决方法来强制调用进程(SpecificSecond)
方法
如何更好地设计它?问题不在于特定类型与常规类型,而在于静态绑定与动态绑定。您的示例演示了重载。Java使用最特定的类型选择重载方法,但基于静态信息(即指定为参数的表达式类型)。由于此变量的类型为对象
,因此选择了对象
重载
为了实现您的目标,您必须将代码设计为使用动态绑定。在这种情况下,将根据作为参数传递的对象的最特定类型选择方法实现。要实现这一点,您需要定义接口或实例方法,以及它们。在OOP中,处理逻辑将存在于每个MyInterface
实现中,并通过多态性调用。静态处理方法是过程编程,而不是OOP。