Java中如何在for循环语句中对类对象数组进行赋值?
1个回答
展开全部
abstract class Employee{
abstract double earnings();
}
class YearWorker extends Employee{
double earnings(){
return 15000;
}
}
class MonthWorker extends Employee{
double earnings(){
return 1200;
}
}
class WeekWorker extends Employee{
double earnings(){
return 280;
}
}
class Company{
int i=0;
Employee[] employee=new Employee[3];//主要看这里,有什么不懂的吗?
public void getSalary(Employee employee1,Employee employee2,Employee employee3){//我这代码写得不是很好,其实可以直接传个数组进了的,参数可以只写一个employee
this.employee[0]=employee1;
this.employee[1]=employee2;
this.employee[2]=employee3;
}
public double getAllSalary(){
double sum=0;
for(i=0;i<3;i++)
sum+=employee[i].earnings();
return sum;
}
}
public class Application1{
public static void main(String[] args){
Company company=new Company();
company.getSalary(new YearWorker(),new MonthWorker(),new WeekWorker());
System.out.println(company.getAllSalary());
}
}
//我写的一个例子,供你参考。
abstract double earnings();
}
class YearWorker extends Employee{
double earnings(){
return 15000;
}
}
class MonthWorker extends Employee{
double earnings(){
return 1200;
}
}
class WeekWorker extends Employee{
double earnings(){
return 280;
}
}
class Company{
int i=0;
Employee[] employee=new Employee[3];//主要看这里,有什么不懂的吗?
public void getSalary(Employee employee1,Employee employee2,Employee employee3){//我这代码写得不是很好,其实可以直接传个数组进了的,参数可以只写一个employee
this.employee[0]=employee1;
this.employee[1]=employee2;
this.employee[2]=employee3;
}
public double getAllSalary(){
double sum=0;
for(i=0;i<3;i++)
sum+=employee[i].earnings();
return sum;
}
}
public class Application1{
public static void main(String[] args){
Company company=new Company();
company.getSalary(new YearWorker(),new MonthWorker(),new WeekWorker());
System.out.println(company.getAllSalary());
}
}
//我写的一个例子,供你参考。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询