java - 在类之间传递字符串并跳过输入

标签 java string class input

所以我遇到的问题是我在类里面询问一个人的名字。然后该函数返回要由 getName() 使用的字符串。 getName() 函数将在 Employee 类中使用。主要问题是在调用 getName() 函数时,人名没有传递给它。第二个问题是“这个人的名字是什么”的输出问题被跳过了。该程序不会等待用户输入任何信息,即使有明确说明也是如此。除非我应该将要查找的数据放入要传递给 Employee 类的 main() 函数中,否则我不确定该怎么做。

这是 Person 类。

import java.util.Scanner;

public class Person {

    public static void main(String[] args) {

    }

    public Person(){
    }

    //asks for the name of the person and then returns the personName for the getName function to use it
    public static String setName(){
        Scanner input = new Scanner(System.in);
        System.out.println("What is the name of the person? ");
        String personName = input.toString();
        return personName;
    }

    public String getName(String personName){//returns the person name to be used with an Employee object
        return personName;
    }

}

这是 Employee 类。

import java.util.Scanner;

public class Employee extends Person {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Person person = new Person();
        Employee emp = new Employee();

        person.setName();//calls to the person class (because an Employee object extends the parent class)

        System.out.println("What is the salary? ");//asks for salary
        double annualSalary = input.nextDouble();

        System.out.println("What is the starting year? ");//ask for the starting year
        int startYear = input.nextInt();

        System.out.println("What is the insurance number? ");//ask for insurance number
        String insuranceNum = input.toString();

        System.out.println("Name: " + person.getName(person.setName()) + //intent is to retrieve the name of the person object
                           "\nSalary: " + emp.getAnnualSalary(annualSalary) + 
                           "\nStart Year: " + emp.getStartYear(startYear) + 
                           "\nInsurance Number: " + emp.getInsuranceNum(insuranceNum));
    }

    //constructor
    public Employee(){
    }

    //returns the salary of the employee
    public double getAnnualSalary(double annualSalary){
        return annualSalary;
    }

    //returns the year the employee started
    public int getStartYear(int startYear){
        return startYear;
    }

    //returns the insurance number of the employee
    public String getInsuranceNum(String insuranceNum){
        return insuranceNum;
    }
}

最佳答案

  1. 我认为您误解了 getter 和 setter 通常的用途。 它们通常用于让另一个类访问存储在 Person 类中的数据。

getName 方法不应提供任何参数,并返回人名:

public String getName()
{
  return personName;
}

通常应该向 setName 方法传递一个值以将 personName 设置为:

public void setName(String givenName)
{
  personName = givenName;
}

目前 Person 类没有在任何地方存储名称...您需要将 personName 设为全局 - 将 decleration 放在 setName 方法之外。 例如:

public class Person()
{
  private String personName;
  // constructor, you could give a person an initial name
  public Person(String name) 
  {
    personName = name;
  } 
  // setter/getter methods below
  // ...
  //end of setter/getter methods
}

2。 正如 codeNinja 所说,使用 input.nextLine(); 获取用户的输入。

关于java - 在类之间传递字符串并跳过输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22599088/

相关文章:

java - 从服务中删除正在进行的通知

ios - 验证只能包含字母、数字和下划线字符的字符串

rbind 不同类别的数据集

java - 寻找距离玩家最近的物体(障碍物)

JavaFX css类样式

Java:如何用分隔符值替换字符串中的空格和\r?

C语言: why c language has 'class' ?

css - 在一个元素上使用两个 CSS 类

java - 以编程方式与计算机建立无服务器蓝牙连接

python - datetime strptime - 设置格式以忽略字符串的结尾部分