当前位置: 首页 > 知识库问答 >
问题:

在类和被跳过的输入之间传递字符串

谭光辉
2023-03-14

这是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个答案

周育
2023-03-14
  1. 我想你误解了getters和setters通常是用来做什么的。它们通常用于让另一个类访问Person类中存储的数据

getName方法不应被赋予任何参数,并返回persons名称:

public String getName()
{
  return personName;
}

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

public void setName(String givenName)
{
  personName = givenName;
}
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
}
 类似资料:
  • 我试图在Java中输入某些字符串和整数变量的值。但是,如果我在整数之后输入字符串,那么在控制台中,字符串输入将被跳过并移动到下一个输入。 这是密码

  • 我有一个JPanel表单,其中包含一个JList和一些JButton。JPanel看起来像这样 当我单击Add List按钮时,会显示一个单独的JFrame表单。JFrame表单将如下所示 单击JFrame上的add按钮时,我需要将JTextfield(命名列表名)的值添加到上一个JPanel上的JList。我想知道如何将值从JFrame传递到JPanel?如有任何建议,将不胜感激。 下面是 JP

  • 相对来说使用频率不高,暂时不编写,以后加上

  • 问题内容: 我一直遵循本文中@tulskiy的建议,通过JNI将Java中的c指针存储在Java中通过JNI在C和Java之间传递指针 诀窍是将指针转换为jlong​​。所以从c我有 我返回一个jlong​​(始终为64位),因为我希望我的代码在64位和32位系统上都能工作。64位计算机上64位指针在内存中的大小为64,而在32位计算机上,内存中指针的大小为32位。 问题是在32位计算机上,我收到

  • 我需要在python脚本和java引擎之间通过stdin和stdout进行一些进程间通信。 我是否可以将python对象转换为JSON,并将整个对象转换为字符串,通过stdin传递到Java,然后将该字符串转换回JSON进行处理,然后将其作为通过字符串转换的JSON对象返回到python 我只想知道是否可以通过stdin发送一个JSON对象,或者有什么可能的方法可以做到这一点

  • 我得到了这个错误: “变量'a'已赋值,但其值永远不可用”