[英]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.