Java 制作一个能够识别参数类型并相应调用其常用方法的方法
下面是三种方法的示例,它们的指导者几乎相同,不包括第一个Tile、Primitive和Cube 让我们假设对于这些对象,这些方法也存在:Java 制作一个能够识别参数类型并相应调用其常用方法的方法,java,methods,constructor,Java,Methods,Constructor,下面是三种方法的示例,它们的指导者几乎相同,不包括第一个Tile、Primitive和Cube 让我们假设对于这些对象,这些方法也存在: Tile.set(int a, int b, int c){ ... } Primitive.set(int a, int b, int c){ ... } Cube.set(int a, int b, int c){ ... } 下面是使用上述方法的3种不同方法: void first(Tile tile, int a, int b, int c){
Tile.set(int a, int b, int c){ ... }
Primitive.set(int a, int b, int c){ ... }
Cube.set(int a, int b, int c){ ... }
下面是使用上述方法的3种不同方法:
void first(Tile tile, int a, int b, int c){
tile.set(a,b,c);
}
void second(Primitive tile, int a, int b, int c){
tile.set(a,b,c);
}
void third(Cube tile, int a, int b, int c){
tile.set(a,b,c);
}
我的问题是,如果我们假设他们都有这个方法,有没有可能创建一个像怪物一样的方法来识别我的输入是tile、Primitive还是Cube。set
大概是这样的:
void monster(Anything tile, int a, int b, int c){
tile.set(a,b,c);
}
//给洛洛//
虽然我知道这是错误的,因为该方法有3个同名的讲师,所以它应该只从3个讲师中选择1个,不管是什么:
void monster(Tile tile, Primitive tile, Cube tile, int a, int b, int c){
tile.set(a,b,c);
}
是的,遗产。
使用set方法创建一个基类。
将set方法设为虚拟,并在Tile基元和多维数据集类中重写它
然后简单地说:
void monster(BaseClass tile, int a, int b, int c)
{
tile.set(a,b,c)
}
此函数将接受从基类继承的任何类,并分别调用其重写方法 是的,您可以通过使用接口来使用策略模式。 您可以通过instanceof识别它 为了更具体地回答您的问题,例如:
public interface ISet {
void set(int a, int b, int c);
}
public class Cube implements ISet {
@Override
public void set(int a, int b, int c) {
}
}
public class Primitive implements ISet {
@Override
public void set(int a, int b, int c) {
}
}
public class Tile implements ISet {
@Override
public void set(int a, int b, int c) {
}
}
public static void main(String[] args) {
ISet set = new Cube(); //just an example. it can be given
if (set instanceof Cube) {
System.out.println("instanceof Cube");
}
}
public class A {
public void set(ISet set) {
set.set(0 ,0, 0);
}
}
创建一个界面形状:
现在你可以这样称呼它:
class Test {
public static void acceptGenericType(Shape shape) {
shape.set(shape.getA(), shape.getB(), shape.getC());
}
public static void main(String[] args) {
Shape tile = new Tile();
acceptGenericType(tile); // calls Tile's set()
}
}
因此,您只调用该特定实例的集合。嘿,Kyle,谢谢您的回答。你认为没有继承还有别的办法吗?我的意思是,假设我的Tile类、Primitive类和Cube类彼此非常不同,并且不希望与公共类相关,它们的公共类中只有.set方法。我不太熟悉Java,但在C中,接口将是你的答案。为所有类提供相同的接口:接口集{void setint a,intb,intc}这将要求类具有Set方法的实现。因此,我们可以将它改为Set:void monsterSet tile,int a,int b,int cHi,非常感谢您的帮助。我可以多问一点吗?因此,在您的示例中,main方法不接受任何可能是平铺、基本体或立方体的指令。如果集合是多维数据集的实例,请执行以下操作。。。这是我想要避免的,因为这意味着如果ISet是基本体或平铺,我必须创建另一个,对吗?这看起来有点像识别输入立方体、平铺、原语并为它们分离3种不同的方法。我想我想做的恰恰相反。第2部分belowI希望我的main方法采用Cube、Primitive、Tile三个类中的任何一个,并运行相同的.set方法,无论输入是什么,因为我们假设这三个类中的任何一个都可以执行相同的任务,这是通过.set方法运行的。我编辑了我的原始问题,并在最底部插入了一个代码。根据您对另一个答案的评论所做的编辑。@Hydraxia:这个答案对您有用吗?如果是,请考虑接受一个。
interface Shape {
void set(int a, int b, int c);
}
class Tile implements Shape {
@Override
public void set(int a, int b, int c) {
}
}
class Cube implements Shape {
@Override
public void set(int a, int b, int c) {
}
}
class Test {
public static void acceptGenericType(Shape shape) {
shape.set(shape.getA(), shape.getB(), shape.getC());
}
public static void main(String[] args) {
Shape tile = new Tile();
acceptGenericType(tile); // calls Tile's set()
}
}