【问题标题】:Cant find Symbol error找不到符号错误
【发布时间】:2013-03-30 22:58:18
【问题描述】:

编译器给我一条错误消息,说它找不到电影[x].getTitle() 和电影[x].getYear。我一直想知道通过界面进入课程是否有问题

这是错误:

MathewBorumP5.java:68: error: cannot find symbol
System.out.printf("%-26s%-6s%-10s%-9s%-11s\n", movies[x]
.getTitle(),

^
symbol:   method getTitle()
location: interface Profitable
MathewBorumP5.java:69: error: cannot find symbol
                            movies[x].getYear(), movies[x].calcRevenue(),
                                     ^
symbol:   method getYear()
location: interface Profitable
2 errors

这是我的客户端类:

import java.util.Scanner;

public class MathewBorumP5 {
    public static void main(String[] args) {
        int choice;
        boolean restart = true;

        Scanner input = new Scanner(System.in);

        Profitable[] movies = new Profitable[6];
        movies[0] = new Animated("Beauty and the Beast", "Gary Trousdale", 1991,
        10.0, 5.0, 2.0);
        movies[1] = new Animated("Peter Pan", "Clyde Geronimi", 1953, 2.0, 1.2,
        .5);
        movies[2] = new Documentary("Planet Earth", "Alastair Fothergill", 2006,
        10, 20, 5);
    movies[3] = new Documentary("Drain the Ocean", "Steve Nichols", 2009, 9,
        2,3);
    movies[4] = new Drama("The Shawshank Redemption", "Frank Darabont",
        1994, 89, 7, 2);
    movies[5] = new Drama("The Godfather", "Francis Coppola", 1972, 10, 3,
        5);

    do {
        menu();
        System.out.print("Enter a number from 1 - 5: ");
        choice = input.nextInt();
        System.out.print("\n");

        switch(choice) {
            case 1:
                item1(movies);
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                restart = false;
                break;
            default:
                System.out.print("You didn't enter a number between 1"
                    + " and 5.\n");
                break;
        }
    } while(restart == true);
}

public static void menu() {
    System.out.print("Warren Moore Movie Menu\n");
    System.out.print("1. Show the list of movies in the array\n");
    System.out.print("2. Display the total number of movies and the total" +
        " revenues\n");
    System.out.print("3. Search movie by title\n");
    System.out.print("4. Display movies sorted by profit in decreasing" +
        " order\n");
    System.out.print("5. Exit\n");
}

public static void item1(Profitable[] movies) {
    double revenue;
    System.out.printf("%-26s%-6s%-10s%-9s%-11s\n", "Title", "Year", 
    "Revenue", "Profit", "Category");
    for(int x = 0; x <= 6; x++) {
        revenue = movies[x].calcRevenue();
        System.out.printf("%-26s%-6s%-10s%-9s%-11s\n", movies[x].getTitle(),
            movies[x].getYear(), movies[x].calcRevenue(),
            movies[x].calcProfit(revenue), movies[x].category());
    }
}
}

这是我的超类:

public class Movie implements Profitable {
protected String title;
protected String director;
protected int year;
protected double productionCost;
private int totalMovies = 0;

public Movie() {
    totalMovies++;
}
public Movie(String newTitle, String newDirector, int newYear,
    double newCost) {
    totalMovies++;
    title = newTitle;
    director = newDirector;
    year = newYear;
    productionCost = newCost;
}

public int getTotalMovies() {
    return totalMovies;
}

public String getTitle() {
    return title;
}

public void setTitle(String newTitle) {
    this.title = title;
}

public String getDirector() {
    return director;
}

public void setDirector(String director) {
    this.director = director;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}

public double getProductionCost() {
    return productionCost;
}

public void setProductionCost(double productionCost) {
    this.productionCost = productionCost;
}

public String toString() {
    return "";
}
}

这是我的单独课程。 公共类动画扩展电影实现盈利{ 私人双倍费率; 私人双收入;

public Animated() {
    super();
}

public Animated(String title, String director, int year, double cost,
    double rate, double income) {
    super(title, director, year, cost);
    this.rate = rate;
    this.income = income;
}

public double getRate() {
    return rate;
}

public void setRate(double rate) {
    this.rate = rate;
}

public double getIncome() {
    return income;
}

public void setIncome(double income) {
    this.income = income;
}

public String category() {
    return "Animated";
}

public double calcRevenue() {
    return (income * rate);
}

public double calcProfit(double revenue) {
    return (revenue - super.productionCost);
}

public String toString() {
    return (super.toString() + "");
}
}

2号

public class Documentary extends Movie implements Profitable {
private int distributors;
private double premium;

public Documentary() {
    super();
}

public Documentary(String title, String director, int year, double cost,
    int distributors, double premium) {
    super(title, director, year, cost);
    this.distributors = distributors;
    this.premium = premium;
}

public int getDistributors() {
    return distributors;
}

public void setDistributors(int distributors) {
    this.distributors = distributors;
}

public double getPremium() {
    return premium;
}

public void setPremium(double premium) {
    this.premium = premium;
}

public String category() {
    return "Documentary";
}

public double calcRevenue() {
    return (distributors * premium);
}

public double calcProfit(double revenue)  {
    return (revenue - super.productionCost);
}

public String toString() {
    return (super.toString() + "");
}
}

3 号

public class Drama extends Movie implements Profitable {
private int tickets;
private double avgPrice;

public Drama() {
    super();
}

public Drama(String title, String director, int year, double cost,
    int tickets, double avgPrice) {
    super(title, director, year, cost);
    this.tickets = tickets;
    this.avgPrice = avgPrice;
}

public int getTickets() {
    return tickets;
}

public void setTickets(int tickets) {
    this.tickets = tickets;
}

public String category() {
    return "Drama";
}

public double calcRevenue() {
    return (tickets * avgPrice);
}

public double calcProfit(double revenue) {
    return (revenue - super.productionCost);
}

public String toString() {
    return (super.toString() + "");
}
}

最后是我的界面。

public interface Profitable {
    public abstract String category();
    public abstract double calcRevenue();
    public abstract double calcProfit(double revenue);
}

【问题讨论】:

    标签: java class methods


    【解决方案1】:

    Profitable 接口没有您尝试调用的方法,因此编译器向您抱怨这一点是正确的。数组变量不知道每个数组项可能包含什么类型的对象,实际上可以保存一种类型的项,只是稍后更改它,因此编译器以这种方式运行是有意义的。一个组合是测试数组中的每个项目以查看它实际上是什么类型,例如使用 instanceof,但这是一个非常脆弱的解决方案,不推荐使用。最安全的方法是只对 Profitable 数组项调用 Profitable 方法,并尝试利用多态的魔力让每个对象对同一个方法调用执行不同的行为。

    请注意,如果数组中的所有项都始终是 Movie 或 Movie 子项,那么请务必使用 Movie 数组而不是 Profitable 数组。

    【讨论】:

    • 谢谢,所以我应该使用“Movie[] movies = new Movie[6];”代替?
    • @user2226282:如果数组的所有内容都是 Movie 或 Movie 子对象,那么可以,试试看。
    • 我做了,但现在找不到 calcRevenue、calcProfit 或类别。它只识别超类中的方法。
    • @user2226282:你没有展示你的 Movie 类,但我会假设它没有实现 Profitable 接口,这是一个错误。修复错误并让它实现接口。
    • 我将接口添加到电影并得到相同的错误加上一个说电影不是抽象的并且不会覆盖获利的抽象方法 calcProfit(double)。我还在上面添加了电影类
    【解决方案2】:

    您是对的,问题在于您的获利界面。 当您创建数组项(Profitable[] movies)时,这意味着您只能使用该接口中定义的方法。

    Movie 对象数组可能就是您在此处所追求的。所有这些对象似乎都扩展了 Movie 并实现了 Profitable,因此您可以通过这种方式访问​​所有内容。

    【讨论】:

      猜你喜欢
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 2015-05-12
      相关资源
      最近更新 更多