如何访问位于另一个对象内部的对象数组,该对象又位于超类数组内部?

发布于 2024-10-03 16:29:31 字数 2206 浏览 0 评论 0原文

我有一个抽象类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 Salesman

Order 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皓月长歌 2024-10-10 16:29:31

您的 Salesman 构造函数接受 Order[] 订单,但看起来您并没有在任何地方保留对它的引用(除非您在 getSalary 或在注释掉的部分)。

您需要在 Salesman 构造函数中添加类似

this.orders =orders;

的内容,以便您可以将该数组作为 Salesman< 的字段/属性引用/代码> 对象。

通常,您应该使用 Bean 模式,其中每个字段都有一个 getter/setter 方法:

public Order[] getOrders(){
    return orders;
}

public void setOrders(Order[] orders){
    this.orders=orders;
}

然后在构造函数中添加 setOrders(orders);

,然后在调试/输出代码中添加:

Salesman salesman = (Salesman) employees[i];
for (Order order : salesman.getOrders())
    System.out.println(order); // i don't know what fields order has!

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).

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.

Typically you should use the Bean pattern whereby each field has a getter/setter method:

public Order[] getOrders(){
    return orders;
}

public void setOrders(Order[] orders){
    this.orders=orders;
}

then in your constructor add 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!
简单气质女生网名 2024-10-10 16:29:31

如果您的意思是您有一个Employee数组,并且每个Salesman(仅)都有一个提供自己已销售订单的成员,那么您必须使用数组的数组。

乍一看,您的 Employee 数组更可能是一个向量,即一维数组。另外,您的 Salesman 类有一个 Order 数组,它似乎也是一个向量。如果这是您实际获得的,您只需迭代 Salesman.Orders 成员即可获取 Salesman 所做的销售。

由于它被标记为[homework],所以我拒绝编写代码并为您提供所有这些。

此外,这里有一些提示:

  1. 您已经遍历了 Employee 数组,因此您知道如何遍历一个数组;

  2. 您说 Salesman 派生自 Employee 类,因此使 Employee 可能可以类型转换为 Salesman,然后您将获得 Salesman 类的实例,它这个实际的Employee是一个Salesman,否则它应该返回null;

  3. < p>现在,您可以通过属性方法(getter 和 setters 方法)访问字段;

那么,如何才能从类外部访问 Orders 并将其公开为数组?

如果您能够将其公开为数组,那么如何迭代数组来获取每个订单金额,以便您可以计算此 Salesman 实例已售出多少?

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.

At first view, your Employee array is more likely a vector, that is, a one dimensional array. Plus, your Salesman class has an Order array which also seems to be a vector. If this is what you actually got, you only have to iterate through your Salesman.Orders member to get the sales your Salesman 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:

  1. You already iterate through an array of Employee, so you know how to iterate through one;

  2. 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;

  3. Now, you access your fields through property methods (getters and setters methods);

So, how could you make Orders accessible from the outside of your class, and expose it as an array?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文