简体   繁体   English

在main中创建的对象在其他类中不执行

[英]Object created in main doesnt perform in other classess

I need help. 我需要帮助。 Im stuck with this. 我坚持这一点。

This is the class restaurant. 这是餐厅。 I want to use my objects wtr1,wtr2,wtr3 that are of waiters class.I initiated them in my main. 我想使用服务员类的对象wtr1,wtr2,wtr3。我在我的main中启动了它们。

public class restaurant extends player{
private final waiters wtr1 , wtr2 ,wtr3;




public restaurant(waiters wt1 ,waiters wt2 , waiters wt3 )
    {

        super();
    this.wtr1=wt1;
    this.wtr2=wt2;
    this.wtr3=wt3;


public float calculateSalary()
    {
        employee emp = new employee();

        float wtrsal1 = 0;
        float wtrsal2 = 0;
        float wtrsal3 = 0;
        float sal=0;

        switch (wtr1.wExp)
        {
        case "low":

            wtrsal1 = emp.salarywaiter[0];
            break;

        case "medium":

            wtrsal1 = emp.salarywaiter[1];
            break;

        case "high":

            wtrsal1 = emp.salarywaiter[2];
            break;


        }



        switch (wtr2.wExp)
        {
        case "low":

            wtrsal2 = emp.salarywaiter[0];
            break;

        case "medium":

            wtrsal2 = emp.salarywaiter[1];
            break;

        case "high":

            wtrsal2 = emp.salarywaiter[2];
            break;

        }



        switch (wtr3.wExp)
        {
        case "low":

            wtrsal3 = emp.salarywaiter[0];
            break;

        case "medium":

            wtrsal3 = emp.salarywaiter[1];
            break;

        case "high":

            wtrsal3 = emp.salarywaiter[2];
            break;

        }


        sal = wtrsal1 + wtrsal2 + wtrsal3;

        return sal;


        }

And this is my main.I dont know if in the arguments for rest should be nulls,but it was set by default.Program doesnt excecute,it gives me error that wt1.wExp is wrong and that System.out.println(rest.calculateSalary()); 这是我的主要知识。我不知道rest参数是否应该为null,但它是默认设置的。程序无法执行,它给我的错误是wt1.wExp错误,而System.out.println(rest.calculateSalary()); is wrong. 是错的。

waiters wt1 = new waiters (19 , 0 );
        waiters wt2 = new waiters(20 , 0 );
        waiters wt3 = new waiters (21 , 0);
        restaurant rest= new restaurant(null, null, null);
        System.out.println(rest.calculateSalary());

Here is the waiters class: 这是服务生班:

public class waiters extends employee {


    String wname;
    String wsurname;
    String wExp;


    public waiters(int i , int n )
    {
         wname = getname(i);
         wsurname = getsurname(i);
         wExp = ExpirienceLevel[n];
    }



}

Chit: 捷:

restaurant rest= new restaurant(null, null, null);

You're not passing in the Waiters that you've created but rather are only passing in nulls, so what do you expect will happen? 您没有传递您已经创建的Waiters,而是传递了null,那么您期望发生什么? The restaurant instance will only hold null references and will not magically be able to use the waiter objects. 餐厅实例将仅包含null引用,并且将无法神奇地使用服务对象。 You have to pass them in -- If you want restaurant to use the Waiter references, pass them in via the constructor. 必须传递它们-如果您希望餐厅使用Waiter引用,请通过构造函数传递它们。

 waiters wt1 = new waiters (19 , 0 );
 waiters wt2 = new waiters(20 , 0 );
 waiters wt3 = new waiters (21 , 0);
 restaurant rest= new restaurant(wt1, wt2, wt3);
 System.out.println(rest.calculateSalary());

In this line 在这条线

restaurant rest= new restaurant(null, null, null);

you're creating your restaurant, but the waiters you created are not being sent to the restaurant! 您正在创建餐厅,但是您创建的服务员没有被发送到餐厅! You've used three null waiters. 您已经使用了三个null服务员。 You just needed 你只需要

restaurant rest= new restaurant(wtr1, wtr2, wtr3);

You mentioned that the null s were put there automatically. 您提到过null被自动放置在那里。 When your IDE creates the outline for the method call for you, it might populate with allowable default values, depending on how it's configured. 当IDE为您创建方法调用的大纲时,它可能会填充允许的默认值,具体取决于其配置方式。 For object references, it'll default to giving you null s, because there's really nothing else it knows how to put in there for you. 对于对象引用,默认情况下将为您提供null ,因为实际上它不知道该如何为您放置null Don't assume that automatically generated code is correct! 不要以为自动生成的代码是正确的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过其他类构造函数在 main 方法中创建对象时访问对象属性 - Accessing object properties when object is created in main method via other class constructor 如何在同一 package 中访问主 class 中的类 - How to access classess in main class within same package 是否可以使用其他一些类,例如awt中的某些类或codenameone中的swing - Is it possible to used some other classes such as some classess in awt or swing in codenameone 如何在其他方法中引用main方法中创建的多个object的非静态字段? - How do I refer to non-static fields of more than one object created in the main method in other methods? 如何访问在main方法中创建的对象 - How to Access an object created in main method 访问在Main类中创建的Map Interface对象 - Accessing Map Interface Object created in Main class 无法从外部main引用main中创建的对象 - Cannot reference an object created in main from outside main 制作除字符串以外的主要接受对象? - Making main accept object other than string? 在其他类中创建 JLabel 并实现到主类未出现 - created JLabel in other class and implemented to main class not appears 关闭主Swing窗口时,未创建EventQueue上的另一个线程 - On closing the main Swing window, the other thread on the EventQueue gets not created
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM