简体   繁体   中英

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.

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()); 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? The restaurant instance will only hold null references and will not magically be able to use the waiter objects. You have to pass them in -- If you want restaurant to use the Waiter references, pass them in via the constructor.

 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. You just needed

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

You mentioned that the null s were put there automatically. 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. 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. Don't assume that automatically generated code is correct!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM