0

我创建了一个超类(抽象),因为它及其子类从接口(名为 Profitable)继承数据。在编译期间,如果它无法识别方法 calcuateRevenue(),则会返回错误。该方法在接口中声明,然后在子类中定义(每个类使用不同的方式计算收益)。由于有几个类,我将包括超类、子类的一个示例、接口,当然还有主方法(BradleyLangrehrP5)。我错过了一些基本的东西,还是设计得非常糟糕?

public class BradleyLangrehrP5
{
    public static void main(String[] args)
    {
        //Create movieArray list
        Movie[] movieArrayList = new Movie[6];

        //Instantiate movies from each subclass
        movieArrayList[0] = new Animated(.1, 5.0 , "Finding Nemo", 
                "Andrew Stanton", 2003, 90.0);
        movieArrayList[1] = new Animated(.08, 3.0, "Shrek", 
                "Andrew Adamson", 2001, 60.0);
        movieArrayList[2] = new Documentary(4.0, 2.5, 
                "Fidel: The Untold Story", "Estela Bravo", 2001, 72.0);
        movieArrayList[3] = new Documentary(6.0, 3.25, 
                "An Inconvenient Truth", "David Guggenheim", 2006, 1.0);
        movieArrayList[4] = new Drama(25, 4.5, "Red Lights", 
                "Rodrigo Cortes", 2012, 17.0);
        movieArrayList[5] = new Drama(15, 3.75, "The Godfather", 
                "Francis Ford Coppola", 1972, 6.5);
        displayNumberOfMoviesAndRevenue(movieArrayList);
    }

    public static void displayMovieArrayList(Movie[] movieArrayList)

    {

        for(int i = 0; i < movieArrayList.length; i++)

        {

            System.out.println(movieArrayList[i].toString());

        }

    }


    public static void displayNumberOfMoviesAndRevenue(Movie[] movieArrayList)
    {
        for (int i = 0;i < movieArrayList.length; i++)
        {
            System.out.println(movieArrayList[i].totalMovies());
            System.out.println(movieArrayList[i].calculateRevenue());
        }
    }
}

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

    //Construct a default movie
    public Movie()
    {
        totalMovies++;
    }

    //Cosntruct movie with specific information
    public Movie(String title, String director, int year, double productionCost)
    {
        this.title = title;
        this.director = director;
        this.year = year;
        this.productionCost = productionCost;
        totalMovies++;
    }

    //Accessors
    public String getTitle()
    {
        return title;
    }

    public String getDirector()
    {
        return director;
    }

    public int getYear()
    {
        return year;
    }

    public double getProductionCost()
    {
        return productionCost;
    }

    public int totalMovies()
    {
        return totalMovies;
    }

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

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

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

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

    //Effectors
    public String toString()
    {
        return "Movie: " + getTitle() + "was directed by " + getDirector() + 
                "with a production cost of " + getProductionCost();
    }
}

public final class Animated extends Movie implements Profitable
{
    private double royaltyRate;
    private double incomeRelatedProducts;

    //No arg constructor
    public Animated()
    {
        super();
    }

    //Constructor with arguments
    public Animated(double royaltyRate, double incomeRelatedProducts, String title,
            String director, int year, double productionCost)
    {
        super(title, director, year, productionCost);
        this.royaltyRate = royaltyRate;
        this.incomeRelatedProducts = incomeRelatedProducts;
    }

    //Accessors
    public double getRoyaltyRate()
    {
        return royaltyRate;
    }

    public double getIncomeRelatedProducts()
    {
        return incomeRelatedProducts;
    }

    //Mutators
    public void setRoyaltyRate(double royaltyRate)
    {
        this.royaltyRate = royaltyRate;
    }

    public void setIncomeRelatedProducts(double incomeRelatedProducts)
    {
        this.incomeRelatedProducts = incomeRelatedProducts;
    }

    //Effectors
    public double calculateRevenue()
    {
        return incomeRelatedProducts * royaltyRate;
    }

    public double calculateProfit()
    {
        double profit = calculateRevenue() - getProductionCost();
        return profit;
    }

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

    public String toString()
    {
        return "This is an animated movie, and it is";
    }
}

public interface Profitable
{
    double calculateRevenue();
    double calculateProfit();
    String getCategory();
}//end of Profitable interface
4

3 回答 3

3
 System.out.println(movieArrayList[i].calculateRevenue());

您正在尝试calculateRevenue调用Movie. 但它是在接口中定义的Profitable,与Movie. 的实际子类Movie可能很好地实现Profitable,但编译器不知道这一点。

在调用该方法之前,您需要声明Movie implements Profitable或进行类型转换。Profitable

于 2012-10-16T04:42:51.530 回答
0

您可能希望让您的超类Movie实现Profitable,因为您可能希望为方法提供任何默认实现calculateRevenue。在这里,您在子类中实现接口Profitable,这意味着您提供了灵活性,Profitable即可以添加它,也可以不依赖于子类实现。如果这是您的意图,请保持原样,并且在您调用时对使用运算符calculateRevenue进行适当的类型检查并调用方法。但是,如果您希望您的类应该具有此功能,请务必让您的类实现接口。所以只有不这样做你的代码会抛出编译错误说在类中找不到ProfitableinstanceofMovieMovieProfitablecalculateRevenueMovie 这就是你得到的。

于 2012-10-16T04:56:38.617 回答
-1
for (int i = 0;i < movieArrayList.length; i++)
            {
                System.out.println(movieArrayList[i].totalMovies());
                System.out.println(movieArrayList[i].calculateRevenue());
            }

这是对 class 的引用Movie。尝试添加演员表。

for (int i = 0;i < movieArrayList.length; i++)
        {
            if(movieArrayList[i] instanceof Animated){
               System.out.println((Animated)movieArrayList[i].totalMovies());
               System.out.println((Animated)movieArrayList[i].calculateRevenue());
             }
             //...do other IFs

        }
于 2012-10-16T04:46:43.907 回答