gpt4 book ai didi

java - 可以使用方法创建类的对象吗?

转载 作者:行者123 更新时间:2023-12-02 00:56:37 25 4
gpt4 key购买 nike

我尝试使用使用 Scanner 方法接受用户输入的方法来创建类的对象。问题是,如果我尝试将它创建到 main 中,它会给我一个错误,指出不能从静态上下文引用非静态方法。如果我尝试使方法静态并尝试从 main() 使用它,它将要求将参数传递给它。我能做什么?

下面是该类的源代码,方法名为createSalaried() :

package com.company;

import java.util.ArrayList;
import java.util.Scanner;

public class SalariedEmployee extends Employee {
public ArrayList<SalariedPayslip> salaried_payslips = new ArrayList<>();
int annual_salary;
String work_mode;
String job_department;

public SalariedEmployee(
int id,
String title,
String first_name,
String last_name,
String date_birth,
String national_insurance,
String job_titles,
String job_department,
String work_mode,
int salary) {
this.employee_id = id;
this.employee_title = title;
this.first_name = first_name;
this.last_name = last_name;
this.date_of_birth = date_birth;
this.national_insurance = national_insurance;
this.job_titles = job_titles;
this.job_department = job_department;
this.work_mode = work_mode;
this.annual_salary = salary;
}

public int getSalary() {
return annual_salary;
}

public int getId() {
return employee_id;
}

// Returns a string with all the data of the specified employee.
public String toString() {
return String.format(
" Employee id: %d\n Title: %s\n First name: %s\n Last name: %s\n Date of birth: %s\n National Insurance: %s\n Job titles: %s\n Job department: %s\n Work mode: %s\n Annual salary: %d",
employee_id,
employee_title,
first_name,
last_name,
date_of_birth,
national_insurance,
job_titles,
job_department,
work_mode,
annual_salary);
}

public SalariedEmployee createSalaried(int id_assign) {
Scanner input_scanner = new Scanner(System.in);
System.out.println("Title:");
String employee_title = input_scanner.nextLine();
System.out.println("First name:");
String first_name = input_scanner.nextLine();
System.out.println("Last name");
String last_name = input_scanner.nextLine();
System.out.println("Date of birth:");
String date_of_birth = input_scanner.nextLine();
System.out.println("National insurance number");
String national_insurance = input_scanner.nextLine();
System.out.println("Job titles:");
String job_titles = input_scanner.nextLine();
System.out.println("Job department:");
String job_department = input_scanner.nextLine();
System.out.println("Full-time or part-time?:");
String work_mode = input_scanner.nextLine();
System.out.println("Salary:");
int salary = input_scanner.nextInt();

return new SalariedEmployee(
id_assign,
employee_title,
first_name,
last_name,
date_of_birth,
national_insurance,
job_titles,
job_department,
work_mode,
salary);
}
}

最佳答案

如果您想不通过实例使用该方法,您应该像这样使您的方法静态。

public static SalariedEmployee createSalaried(int id_assign) {...}

然后你就可以轻松使用它了。

SalariedEmployee salariedEmployee = SalariedEmployee.createSalaried(id_assign);

关于java - 可以使用方法创建类的对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61278796/

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