列出并访问方法

时间:2018-02-19 20:53:25

标签: java

假设我已导入所有内容(Desktop是一个实现Product的抽象类,而Macintosh和Windows是扩展Desktop的具体类(都缺少compareTo方法),为什么我收到错误的潜在解决方法是什么?找不到compareTo()和getYear()方法。我不知道如何修复此错误,请不要提供复杂的解释,因为我还在学习java;只是引导我找到解决方案。

我收到以下错误:无法找到符号 - 方法compareTo(产品)且无法找到符号 - 方法getYear()

我尝试过各种各样的东西,例如将Comparable-Product更改为Comparable-Car,但这不起作用。我也尝试过铸造,但这并没有解决问题。另外,我只想在Car类中使用compareTo方法,而不是在任何其他类中。 情况是Product是一个接口,Car和抽象方法Desktop实现它们。桌面有两种具体的方法Macintosh和Windows扩展它。 Car应该具有Comparable接口的实现。 main方法在其自己的类Client中。

interface Product
{
  String getName();
  double getCost();
}

public class Car implements Product, Comparable<Product>
{
    private String name;
    private double cost;
    private int year;
    public Car(String name, double cost, int year)
    {
      this.name = name;
      this.cost = cost;
      this.year = year;
    }
    public String getName() {return name; }
    public double getCost() {return cost; }
    public int getYear() {return year;}
    public int compareTo(Product p)
    {
        if (getCost() < p.getCost())
              return -1;
        else if (getCost() == p.getCost())
              return 0;
        return 1;
    }
}
//Client class with java.util.* imported. 
public static void main(String[] args)
{
    List<Product> inventory = new ArrayList<Product>();
    inventory.add(new Windows("Surface Book Pro", 1200, 32));
    inventory.add(new Windows("Surface Book Pro", 1700, 64));
    inventory.add(new Macintosh("MacBook Pro", 1300, "a1278"));
    inventory.add(new Macintosh("MacBook Pro", 1500, "a1502"));
    inventory.add(new Car("Lamborghini Huracan", 200000, 2018));
    inventory.add(new Car("Lamborghini Aventador", 399500.0, 2016));
    int first = -1;
    int second = -1;
    for(int i = 0; i < inventory.size(); i++)
    {
      if (!(inventory.get(i) instanceof Car))
        continue;
      if (first == -1)
      {
        first = i;
        continue;
      }
      if (second == -1)
      {
        second = i;
        int year1 = inventory.get(first).getYear();//**Error Here**
        int year2 = inventory.get(second).getYear();//**Error Here**
        String name1 = inventory.get(first).getName();
        String name2 = inventory.get(second).getName();
        int comparison = inventory.get(first).compareTo(inventory.get(second));//**Error Here**
        if (comparison < 0)
          System.out.println("The " + year1 + " " + name1 + " is less expensive than the " + year2 + " " + name2);
        else if (comparison == 0)
          System.out.println("The " + year1 + " " + name1 + " is as expensive than the " + year2 + " " + name2);
        else
          System.out.println("The " + year1 + " " + name1 + " is more expensive than the " + year2 + " " + name2);
        first = second;
        second = -1;
      } 
    }
}

2 个答案:

答案 0 :(得分:0)

int year1 = inventory.get(first).getYear(); 

库存是产品列表,因此inventory.get(x)将返回Product,但Product没有方法.getYear();

比较适用于compareTo。编译器无法验证,这将是一个类似的条目。

按类型创建集合,可能是合并的集合,您需要在其他地方创建集合:

    public static void main(String[] args)
    {
        List<Product> inventory = new ArrayList<Product>();
        List<Car> cars = new ArrayList<Car>();
        List<Desktop> desktops = new ArrayList<Desktop>();

        desktops.add(new Windows ("Surface Book Pro", 1200, 32));
        desktops.add(new Windows ("Surface Book Pro", 1700, 64));
        desktops.add(new Macintosh ("MacBook Pro", 1300, "a1278"));
        desktops.add(new Macintosh ("MacBook Pro", 1500, "a1502"));
        cars.add (new Car ("Lamborghini Huracan", 200000, 2018));
        cars.add (new Car ("Lamborghini Aventador", 399500.0, 2016));

        for(Car car : cars)
        {
            inventory.add (car);
        }
        for (Desktop desktop : desktops)
        {
            inventory.add (desktop);
        }

        for(Car car1 : cars)
        {
            for (Car car2 : cars)
            {
                if (car1 != car2) {
                    int comparison = car1.compareTo (car2);
                    String cmp = "as";
                    if (comparison < 0)
                        cmp = "less";
                    else if (comparison > 0) 
                        cmp = "more";
                    System.out.println("The " + car1.getYear() + " " + car1.getName() + " is " + cmp + " expensive than the " + car2.getYear() + " " + car2.getName());
                }
            }
        }
    }

如果你想避免,比较car1和car2,以及后来的car2和car1,你可以将汽车复制到第二个集合,并从第二个列表中删除每个首先比较的汽车。然后你不需要比较你是否有想法的汽车:

    List<Car> cars2 = new ArrayList <> ();
    for(Car car : cars)
    {
        cars2.add (car);
    }
    for (Car car1 : cars)
    {
        cars2.remove (car1);        // new 

        for (Car car2 : cars2)
        {
            int comparison = car1.compareTo (car2);
            String cmp = "as";
            if (comparison < 0)
                cmp = "less";
            else if (comparison > 0) 
                cmp = "more";
            System.out.println("The " + car1.getYear() + " " + car1.getName() + " is " + cmp + " expensive than the " + car2.getYear() + " " + car2.getName());
        }
    }

答案 1 :(得分:0)

如果错误是关于web: gunicorn myapp.wsgi 没有方法的话。而且您的循环中只有Product才能使用。为什么不Car cast ProductCar&而使用它们?尝试一下,让我知道它是否有效。

[...] 
if (second == -1) { 

    second = i; 
    Car c1 = (Car) inventory.get(first) ;
    Car c2 = (Car) inventory.get(second) ;
    int year1 = c1.getYear(); 
    int year2 = c2.getYear() ;
    String name1 = c1.getName(); 
    String name2 = c2.getName(); 
    int comparison = c1.compareTo(c2);

    if (comparison < 0) 
           System.out.println("The " + year1 + " " + name1 + " is less expensive than the " + year2 + " " + name2); 
    else if (comparison == 0)            
            System.out.println("The " + year1 + " " + name1 + " is as expensive than the " + year2 + " " + name2); 
    else 
            System.out.println("The " + year1 + " " + name1 + " is more expensive than the " + year2 + " " + name2);
     first = second; 
     second = -1; 
    }
 }
 [...]