有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java你能用方法创建一个类的对象吗?

我尝试使用一个方法创建一个类的对象,该方法使用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);
  }
}

共 (2) 个答案

  1. # 1 楼答案

    createSalaried看起来应该是工厂方法,所以将其设置为静态

    然后像这样使用:

    public static void main(String[] args) {
      SalariedEmployee anObject = SalariedEmployee.createSalaried(1);
    }
    
  2. # 2 楼答案

    如果你不想通过一个实例来使用这个方法,你应该像这样使你的方法是静态的

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

    这样你就可以很容易地使用它了

    SalariedEmployee salariedEmployee = SalariedEmployee.createSalaried(id_assign);