如何访问位于另一个对象内部的对象数组,该对象又位于超类数组内部?
我有一个抽象类Employee
,并基于该类,我创建了以下子类:
FulltimeEmployee
兼职员工
Salesman
以及一个独立的类 Orders
。
我使用 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);
/////////////////
}
稍后,我使用该数组来计算推销员获得的奖金,具体取决于他的销售额使。
在 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;
其中每一行都是不同类型的员工(工资 = 全职,partt = 兼职,销售额 =推销员)。
我的问题是我想使用员工数组打印每个推销员的订单。 到目前为止我所做的是
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 ");
}
问题出现在这里:
System.out.printf("ordernumber: %d orderdate:%s description: %s order amount(money): %,.0f\n ");
如何访问员工数组中 Salesman 对象内部的订单数组? 我尝试过强制转换,但它不起作用,因为 Orders 不是 Employee 的子类。
我需要它来打印,例如,
推销员的订单:john koun
订单总数:13000 每个销售员的订单
订单号:1 订单日期:10/2/2010 描述:机械销售订单金额:12000
订单号:2 订单日期:20/2/2010 描述:销售零件 订单金额:1000
I have an abstract class Employee
, and based on that, I created the following subclasses:
FulltimeEmployee
PartTimeEmployee
Salesman
as well as one standalone class, Orders
.
I use the Orders
class to "describe the salesman" by sending an array of orders in the Salesman constructor:
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
:
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).
My problem is that I want to print the orders of each salesman using the employees array.
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?
I have tried casting, but it won't work because Orders is not a subclass of Employee.
I need it to print, for example,
Orders of Salesman: john koun
Total amount of orders: 13000
Orders per SalesmanOrder number: 1 order date: 10/2/2010 description: machinery sale order amount: 12000
Order number: 2 order date: 20/2/2010 description: sales of parts order amount: 1000
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
Salesman
构造函数接受Order[]
订单,但看起来您并没有在任何地方保留对它的引用(除非您在getSalary
或在注释掉的部分)。您需要在
Salesman
构造函数中添加类似this.orders =orders;
的内容,以便您可以将该数组作为
Salesman< 的字段/属性引用/代码> 对象。
通常,您应该使用
Bean
模式,其中每个字段都有一个 getter/setter 方法:然后在构造函数中添加
setOrders(orders);
,然后在调试/输出代码中添加:
your
Salesman
constructor accepts anOrder[]
orders but it doesn't look like you keep a reference to it anywhere (unless you do that ingetSalary
or in the commented out portion).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 theSalesman
object.Typically you should use the
Bean
pattern whereby each field has a getter/setter method:then in your constructor add
setOrders(orders);
and then in your debug/output code add:
如果您的意思是您有一个
Employee
数组,并且每个Salesman
(仅)都有一个提供自己已销售订单的成员,那么您必须使用数组的数组。乍一看,您的
Employee
数组更可能是一个向量,即一维数组。另外,您的 Salesman 类有一个 Order 数组,它似乎也是一个向量。如果这是您实际获得的,您只需迭代Salesman.Orders
成员即可获取Salesman
所做的销售。由于它被标记为
[homework]
,所以我拒绝编写代码并为您提供所有这些。此外,这里有一些提示:
您已经遍历了
Employee
数组,因此您知道如何遍历一个数组;您说
Salesman
派生自Employee
类,因此使Employee
可能可以类型转换为Salesman
,然后您将获得Salesman
类的实例,它这个实际的Employee
是一个Salesman
,否则它应该返回null;If what you're saying is that you have an array of
Employee
, and eachSalesman
(only) has a member that provides the orders himself has sold, then you have to work with an array of array.At first view, your
Employee
array is more likely a vector, that is, a one dimensional array. Plus, yourSalesman
class has anOrder
array which also seems to be a vector. If this is what you actually got, you only have to iterate through yourSalesman.Orders
member to get the sales yourSalesman
did.Since it is tagged as
[homework]
, I refuse to write the code and give you it all cooked for you.Besides, here are some hints:
You already iterate through an array of
Employee
, so you know how to iterate through one;You say that a
Salesman
derives from theEmployee
class, hence makingEmployee
perhaps type-castable toSalesman
, and then you shall get your instance of theSalesman
class, it this actualEmployee
IS aSalesman
, otherwise it should return null;Now, you access your fields through property methods (getters and setters methods);