我创建了一个超类(抽象),因为它及其子类从接口(名为 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