本文介绍了如何使用泛型并从父类继承而不引起名称冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Flight 的Java父类。我有子类: JetFlight , NormalFlight 等等,它们继承自 Flight 。



我希望所有子类都可以从 Comparable 中实现 compareTo $ c>界面。我希望它们从 Flight 继承,因为我想使用多态性(例如,启动一个 Flight 数组并填充它对象 JetFlight , NormalFlight 等)。



这是我父类的代码:

  public abstract class Flight {
public abstract int compareTo(Object o);
}

以下是其中一个子类的代码:

  public class JetFlight扩展了Flight implements Comparable< JetFlight> {
private int flightTime;
public JetFlight(int flightTime){
this.flightTime = flightTime;
}
public int compareTo(JetFlight j){
return this.flightTime - j.flightTime;






$ b

由于2个错误,此代码无法编译:在'Flight'中与'compareTo(Object)'冲突;在'java.lang.Comparable'中的compareTo(T)与'FlightTo'中的'compareTo(Object)'冲突。这两个对象都有相同的擦除,但都不会覆盖另一个对象。



2) Class'JetFlight'必须是在'Flight'中声明抽象或实现抽象方法'compareTo(Object)'



如果我想继承从一个类,同时在子类中使用泛型?

解决方案

选项1:由于您的比较基于飞行时间,据我所知,变量
flightTime
可以在父类中上推,因为所有航班都具有此功能。然后在父类本身中实现您的compareTo()方法。



选项2:如果您想保持当前代码的使用方式:

  public abstract class Flight implements Comparable< Flight> {
public abstract int compareTo(Flight o);






  public class JetFlight扩展了Flight {
private int flightTime;
public JetFlight(int flightTime){
this.flightTime = flightTime;
}
public int compareTo(Flight f){
if(!(f instanceof JetFlight)){
throw new IllegalArgumentException();
}
返回this.flightTime - ((JetFlight)f).flightTime;
}


I have a parent class in Java called Flight. I have children classes: JetFlight, NormalFlight, etc. which inherit from Flight.

I want all the children classes to implement compareTo from the Comparable interface. I want them to inherit from Flight because I want to use polymorphism (for example, initiate a Flight array and fill it with objects of JetFlight, NormalFlight, etc.).

This is my code for the parent class:

public abstract class Flight  {
    public abstract int compareTo(Object o);
}

and this is the code for one of the children classes:

public class JetFlight extends Flight implements Comparable<JetFlight> {
    private int flightTime;
    public JetFlight(int flightTime) {
        this.flightTime = flightTime;
    }
    public int compareTo(JetFlight j) {
        return this.flightTime - j.flightTime;
    }
}

This code won't compile because of 2 errors:

1) compareTo(T) in 'java.lang.Comparable' clashes with 'compareTo(Object)' in 'Flight'; both objects have the same erasure, yet neither overrides the other.

2) Class 'JetFlight' must either be declared abstract or implement abstract method 'compareTo(Object)' in 'Flight'

How do I go about the situation when I want to inherit from a class and at the same time use generics on the child class?

解决方案

Option 1: Since your comparison is based upon flight time, and as far as I know, the variable flightTimecan be pushed up in the parent class as all flights will have this feature. Then implement your compareTo() method in parent class itself.

Option 2: in case you want to keep your current code the way it is:

    public abstract class Flight implements Comparable<Flight> {
    public abstract int compareTo(Flight o);
}


public class JetFlight extends Flight {
private int flightTime;
public JetFlight(int flightTime) {
    this.flightTime = flightTime;
}
public int compareTo(Flight f) {
    if(!(f instanceof JetFlight)){
        throw new IllegalArgumentException();
    }
    return this.flightTime - ((JetFlight)f).flightTime;
}

这篇关于如何使用泛型并从父类继承而不引起名称冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:43