gpt4 book ai didi

java - 创建对象时遇到问题

转载 作者:行者123 更新时间:2023-12-01 18:51:35 25 4
gpt4 key购买 nike

我试图在我的应用程序的主方法中创建员工类的对象,但它找不到变量部门姓氏名字或薪水,我不确定为什么我的语句看起来像这样

Employee employee = new Employee( department, lastName, firstName, salary);

员工类别看起来像

package javaapplication14;
public class Employee implements DepartmentConstants, Displayable
{
private int department;
private String firstName;
private String lastName;
private double salary;

public Employee(int department, String lastName, String firstName,
double salary)
{
this.department = department;
this.lastName = lastName;
this.firstName = firstName;
this.salary = salary;
}

@Override
public String getDisplayText()
{
String displayText = firstName + lastName + salary + department ;
return displayText;
}
}

我不确定是否需要显示其实现的接口(interface),但如果需要,我将编辑我的帖子以包含它们。主要方法是

package javaapplication14;
public class DisplayableTestApp
{
public static void main(String args[])
{
System.out.println("Welcome to the Displayable Test application\n");

// create an Employee object
Employee employee = new Employee( department, lastName, firstName, salary);

// display the employee information
String displayTextEmployee = employee.getDisplayText();
System.out.println(displayTextEmployee);

// create a Product object
Product product = new Product();
// display the product information
String displayText = product.getDisplayText();
System.out.println(displayText);
}
}

最佳答案

I'm trying to create an object of the employee class in the main method of my app but it cant find the variables

它找不到变量,因为它们不存在于构造函数调用的范围内。您必须创建变量并将其传递给构造函数。

Employee 类采用构造函数:

public Employee(int department, String lastName, String firstName, double salary)

举个例子:

Employee employee = new Employee(1, "Smith", "John", 50000);

或者:

int department = 1;
String lastName = "Smith";
String firstName = "John";
double salary = 50000;
Employee employee = new Employee(department, lastName, firstName, salary);

关于java - 创建对象时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15725340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com