我写了一个训练的例子,但是我不明白为什么不能调用 netflix.displayInfo();
它,我也对public void displayInfo()
亚马逊类的问题感兴趣,我如何访问从公司类继承的字段。
package CompanyProgram;
public class Company {
private int avgSalary;
private String address;
public int worksNum;
public String companyName;
public static void main(String[] args) {
Company company = new Company("Company", 3, "West Hollywood", 40000);
Amazon amazon = new Amazon("Amazon", 1200, "Nord Hollywood",70000,7);
HBO hbo = new HBO("Amazon", 1200, "Nord Hollywood",70000,2);
Netflix netflix = new Netflix("Netflix", 3, "South Hollywood", 80000);
Netflix.displayInfo();
}
public Company(String companyName, int worksNum, String address, int avgSalary){
this.companyName = companyName;
this.worksNum = worksNum;
this.address = address;
this.avgSalary = avgSalary;
}
public String getAddress(){
return this.address;
}
public void displayInfo(){
System.out.printf("Company name: %d \n", companyName);
System.out.printf("Workers count: %d \n",worksNum);
System.out.printf("adress: %d \n", address);
System.out.printf("average salary: %d \n", avgSalary);
}
public void setAddress(String address){
this.address = address;
}
public String getAvgSalary(){
return this.companyName;
}
public void setAvgSalary(int avgSalary){
this.avgSalary = avgSalary;
}
}
class Amazon extends Company{
private int filmCount;
public Amazon(String companyName, int worksNum, String address, int avgSalary, int filmCount) {
super(companyName, worksNum, address, avgSalary);
this.filmCount = filmCount;
}
public int getFilmCount(){
return this.filmCount;
}
public void setFilmCount(int filmCount){
this.filmCount = filmCount;
}
public void displayInfo(){
// System.out.printf("Company name: %d \n", companyName);
// System.out.printf("Workers count: %d \n",worksNum);
// System.out.printf("address: %d \n", address);
// System.out.printf("average salary: %d \n", avgSalary());
System.out.printf("Film Count: %d \n", filmCount);
}
}
class Netflix extends Company{
public Netflix(String companyName, int worksNum, String address, int avgSalary) {
super(companyName, worksNum, address, avgSalary);
}
}
class HBO extends Amazon{
public HBO(String companyName, int worksNum, String address, int avgSalary, int filmCount) {
super(companyName, worksNum, address, avgSalary, filmCount);
}
}
问题是这样的:System.out.printf(),固定为 println。我正确理解封装)。