您可以使用方法创建类的对象吗? [重复]

问题描述 投票:0回答:2

我尝试通过使用使用Scanner方法接受用户输入的方法来创建类的对象。问题是,如果我尝试将其创建到主体中,将给我一个错误,指出无法从静态上下文中引用非静态方法。如果我尝试使该方法静态化并尝试从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);
  }
}
java class methods static non-static
2个回答
1
投票

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

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

然后您可以轻松地使用它。

SalariedEmployee salariedEmployee = SalariedEmployee.createSalaried(id_assign);

1
投票

您的createSalaried看起来应该是工厂方法,因此使其为静态。

然后像这样使用它:

public static void main(String[] args) {
  SalariedEmployee anObject = SalariedEmployee.createSalaried(1);
}
© www.soinside.com 2019 - 2024. All rights reserved.