java接口方法输入本身

java接口方法输入本身,java,methods,types,parameters,interface,Java,Methods,Types,Parameters,Interface,我有一个接口,它有一个自己接受的方法,比如 public interface Vehicle { void bump(Vehicle other); } 现在,我想以这样一种方式实现这个接口,即车辆只会撞上自己类型的车辆。也就是说,我想要这样的东西 public class BumperCar implements Vehicle { public void bump(BumperCar other){ System.out.println("They bounce off h

我有一个接口,它有一个自己接受的方法,比如

public interface Vehicle {
  void bump(Vehicle other);
}
现在,我想以这样一种方式实现这个接口,即车辆只会撞上自己类型的车辆。也就是说,我想要这样的东西

public class BumperCar implements Vehicle {
  public void bump(BumperCar other){
    System.out.println("They bounce off harmlessly and continue going.")
  }
}

public class Train implements Vehicle {
  public void bump(Train other){
    System.out.println("Breaking news: Dozens die in horrible train on train collision.")
  }
}

但是,即使这两个级别都必须实现“碰撞”(车辆),保险杠车厢和火车之间的碰撞也不会起任何作用。实现这一点的最佳方法是什么?

我对“可能重复的评论”中提到的解决方案不太满意,因此我将展示我将如何做到这一点

首先,正如我在评论中所说的,您没有从接口重写该方法。签名需要匹配

为了获得正确的结果,创建一个带有接口签名的函数。在该方法中,使用
instanceof
操作符检查对象的类型是否正确。像这样:

public class BumperCar implements Vehicle {
  public void bump(Vehicle other){
    if(other instanceof BumperCar) {
        System.out.println("...");
    }
  }
}

我对“可能重复评论”中提到的解决方案不太满意,因此我将展示我将如何做到这一点

首先,正如我在评论中所说的,您没有从接口重写该方法。签名需要匹配

为了获得正确的结果,创建一个带有接口签名的函数。在该方法中,使用
instanceof
操作符检查对象的类型是否正确。像这样:

public class BumperCar implements Vehicle {
  public void bump(Vehicle other){
    if(other instanceof BumperCar) {
        System.out.println("...");
    }
  }
}

由于Java无法在类中使用
self
类型,因此通常使用一种称为“模拟self-type”的通用构造:

abstract class Vehicle<T extends Vehicle<T>> {
    public abstract void bump(T other);
}

public class Car extends Vehicle<Car> {
        @Override public void bump(Car other) {}
}
抽象类车辆{
公开摘要(T其他);
}
公车{
@覆盖公共无效碰撞(车辆其他){}
}
唯一需要注意的是,必须始终在类声明中指定类型


在核心Java库中,类是模拟自类型用法的一个示例。

由于Java无法在类中使用
self
类型,因此通常使用称为“模拟自类型”的通用构造:

abstract class Vehicle<T extends Vehicle<T>> {
    public abstract void bump(T other);
}

public class Car extends Vehicle<Car> {
        @Override public void bump(Car other) {}
}
抽象类车辆{
公开摘要(T其他);
}
公车{
@覆盖公共无效碰撞(车辆其他){}
}
唯一需要注意的是,必须始终在类声明中指定类型


在核心Java库中,类是模拟自类型用法的一个示例。

您需要使用自引用泛型类型:

    public interface Vehicle< T extends Vehicle<T> > {
        void bump(T other);
    }

    public class BumperCar implements Vehicle<BumperCar> {

        public void bump(BumperCar other){
        }
   }

    public class Train  implements Vehicle<Train > {

        public void bump(Train  other){
        }
   }
公共接口车辆{
空洞隆起(T其他);
}
公共级汽车保险杠{
公共空隙通气管(其他通气管){
}
}
公车{
公共停车场(其他列车){
}
}

您需要使用自引用泛型类型:

    public interface Vehicle< T extends Vehicle<T> > {
        void bump(T other);
    }

    public class BumperCar implements Vehicle<BumperCar> {

        public void bump(BumperCar other){
        }
   }

    public class Train  implements Vehicle<Train > {

        public void bump(Train  other){
        }
   }
公共接口车辆{
空洞隆起(T其他);
}
公共级汽车保险杠{
公共空隙通气管(其他通气管){
}
}
公车{
公共停车场(其他列车){
}
}

您的可能副本未覆盖该方法。它有一个不同的签名。你想不想让不同类型的车辆在它们之间行驶。?此外,在实际使它们碰撞的地方添加代码。可能的重复代码不会覆盖该方法。它有一个不同的签名。你想不想让不同类型的车辆在它们之间行驶。?除此之外,在你实际使它们碰撞的地方添加代码。我同意,我对这些答案不满意。我就是这样做的。谢谢我同意,我对这些答案不满意。这就是我要做的事。谢谢这是一个很好的答案,我可能会在我对代码和代码使用方式控制较少的环境中使用它,因为这样可以更好地强制类型。但由于这是我的代码,而且我对传递正确的类型更为积极,我将使用Lars的答案。这是一个很好的答案,我可能会在我对代码和如何使用代码控制较少的环境中使用它,因为这会更好地强制类型。但由于这是我的代码,而且我对传递正确的类型更为肯定,所以我将使用Lars的答案。