[英]How can I access an object array located inside of another object which is then inside a superclass array?
I have an abstract class Employee
, and based on that, I created the following subclasses: 我有一个抽象类
Employee
,并在此基础上创建了以下子类:
FulltimeEmployee
PartTimeEmployee
Salesman
as well as one standalone class, Orders
. 以及一个独立的类
Orders
。
I use the Orders
class to "describe the salesman" by sending an array of orders in the Salesman constructor: 我使用
Orders
类通过在Salesman构造函数中发送订单数组来“描述销售员”:
public Salesman(String firstname, String lastname,int code, String address,
String city,int tk,int phone,String email,int deptcode,int card,
double hours,String cat,int orderno,double salary,
Orders[] order){
super( firstname, lastname, code, address, city, tk, phone, email, deptcode,
card, hours, cat );
this.orderno=orderno;
setBaseSalary( salary );
getSalary(orders);
/////////////////
}
Later, I use that array to calculate the bonus a salesman gets, depending on the amount of sales he makes. 后来,我使用该数组来计算推销员所获得的奖金,具体取决于他的销售额。
In main
, I created an array of type Employee
: 在
main
,我创建了一个Employee
类型的数组:
Employee employees[] = new Employee[ 7 ];
employees[ 0 ] = salary1;
employees[ 1 ] = salary2;
employees[ 2 ] = salary3;
employees[ 3 ] = partt1;
employees[ 4 ] = partt2;
employees[ 5 ] = sales1;
employees[ 6 ] = sales;
where each row is a different type of employee (salary = full-time, partt = part-time, and sales = salesman). 其中每一行都是不同类型的员工(工资=全职,partt =兼职,而sales =业务员)。
My problem is that I want to print the orders of each salesman using the employees array. 我的问题是我想使用employees数组打印每个销售员的订单。 What I've done so far is
到目前为止,我所做的是
for (int i=5;i<employees.length;i++){
System.out.printf("Orders of Salesman: %S %S",
employees[i].getName(),employees[i].getSurname());
System.out.printf(" Total amount(money) from orders: %,.0f ",
employees[i].earnings());
int j=0;
((Salesman)employees[i]).getOrderNo(arr) ;
//((Orders)employees[i]).getOrderNo();
System.out.printf("ordernumber: %d orderdate:%s description: %s
order amount(money): %,.0f\n ");
}
The problem comes here: 问题来了:
System.out.printf("ordernumber: %d orderdate:%s description: %s order amount(money): %,.0f\\n ");
How do I access the orders array inside of the Salesman object on employees array? 如何访问employee数组上Salesman对象内部的order数组? I have tried casting, but it won't work because Orders is not a subclass of Employee.
我已经尝试过强制转换,但由于Orders不是Employee的子类,因此无法正常工作。
I need it to print, for example, 例如,我需要它来打印,
Orders of Salesman: john koun
推销员订单:约翰·库恩
Total amount of orders: 13000 Orders per Salesman
订单总数:每个销售员13000个订单
Order number: 1 order date: 10/2/2010 description: machinery sale order amount: 12000
订单号:1订单日期:10/2/2010说明:机械销售订单金额:12000
Order number: 2 order date: 20/2/2010 description: sales of parts order amount: 1000
订单号:2订单日期:20/2/2010说明:零件销售订单金额:1000
your Salesman
constructor accepts an Order[]
orders but it doesn't look like you keep a reference to it anywhere (unless you do that in getSalary
or in the commented out portion). 您的
Salesman
构造函数接受Order[]
订单,但它看起来好像没有在任何地方保留对其的引用(除非您在getSalary
或注释掉的部分中进行了引用)。
You will need to add something like 您将需要添加类似
this.orders = orders;
in your Salesman
constructor so you can refer to the array as a field/property of the Salesman
object. 在
Salesman
构造函数中,因此您可以将数组引用为Salesman
对象的字段/属性。
Typically you should use the Bean
pattern whereby each field has a getter/setter method: 通常,您应该使用
Bean
模式,其中每个字段都有一个getter / setter方法:
public Order[] getOrders(){
return orders;
}
public void setOrders(Order[] orders){
this.orders=orders;
}
then in your constructor add setOrders(orders);
然后在构造函数中添加
setOrders(orders);
and then in your debug/output code add: 然后在您的调试/输出代码中添加:
Salesman salesman = (Salesman) employees[i];
for (Order order : salesman.getOrders())
System.out.println(order); // i don't know what fields order has!
If what you're saying is that you have an array of Employee
, and each Salesman
(only) has a member that provides the orders himself has sold, then you have to work with an array of array. 如果您要说的是您有一个
Employee
数组,并且每个Salesman
(仅)有一个成员来提供自己已售出的订单,那么您必须使用一个数组数组。
At first view, your Employee
array is more likely a vector, that is, a one dimensional array. 乍一看,您的
Employee
数组更可能是一个向量,即一维数组。 Plus, your Salesman
class has an Order
array which also seems to be a vector. 另外,您的
Salesman
类具有一个Order
数组,这似乎也是一个向量。 If this is what you actually got, you only have to iterate through your Salesman.Orders
member to get the sales your Salesman
did. 如果这是你实际得到的,你只需要通过你的迭代
Salesman.Orders
成员,让您的销售Salesman
做到了。
Since it is tagged as [homework]
, I refuse to write the code and give you it all cooked for you. 由于它被标记为
[homework]
,因此我拒绝编写代码,并为您准备全部内容。
Besides, here are some hints: 此外,这里有一些提示:
You already iterate through an array of Employee
, so you know how to iterate through one; 您已经遍历了
Employee
数组,因此您知道如何遍历一个Employee
。
You say that a Salesman
derives from the Employee
class, hence making Employee
perhaps type-castable to Salesman
, and then you shall get your instance of the Salesman
class, it this actual Employee
IS a Salesman
, otherwise it should return null; 您说一个
Salesman
从Employee
类派生的,因此使Employee
可能是类型可铸型到Salesman
,然后您将获得Salesman
类的实例,该实际Employee
是Salesman
,否则应返回null;
Now, you access your fields through property methods (getters and setters methods); 现在,您可以通过属性方法(getter和setter方法)访问字段;
So, how could you make
Orders
accessible from the outside of your class, and expose it as an array?因此,如何使
Orders
可以从类外部访问,并将其公开为数组?If you've been able to expose it as an array, how do you iterate through an array to get each of the orders amount so that you may calculate how much this instance of
Salesman
has sold?如果您能够将其公开为数组,如何遍历数组以获取每个订单金额,以便可以计算出此
Salesman
实例的销售量?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.