【问题标题】:Using two different inputs for a class constructor为类构造函数使用两个不同的输入
【发布时间】:2013-10-04 22:05:05
【问题描述】:

我正在编写一个程序,我需要多个不同的类来记录有关员工的信息。此信息包括员工编号、姓名、地址和雇用日期。我已经完成了员工编号课程,但是我的姓名课程有问题。这是我的代码:

import javax.swing.JOptionPane;

public class AssignmentTen
{
    public static void main (String[] args)
    {   
        int input1 = getInt ("Enter Employee Number:");
        Employee e1 = new Employee(input1);
        System.out.println(e1.number);

        Name n1 = new Name();
        System.out.println(n1.firstName + " " + n1.lastName);
    }

    public static int getInt(String paramString)
    {
        String str = JOptionPane.showInputDialog(paramString);
        return Integer.parseInt(str);
    }
}

class Employee
{
    int number;

    Employee(int newNumber)
    {
        number = newNumber;
    }
}

class Name
{
    String firstName;
    String lastName;

如何获取创建 Name n1 以包含名字和姓氏的语句?我希望通过用户输入单独记录这两个名称,但将它们组合起来以创建 Name 对象。

基本上,我希望名字和姓氏是单独的输入字符串,但在创建名称 n1 时将它们放在一起。谁能告诉我应该怎么做?

【问题讨论】:

  • "在创建名称 n1 时将它们放在一起" -- 您的意思是合并为一个字符串吗?
  • 我不明白。您的 Name 类已经有名字和姓氏的单独字符串。只需添加一个带有两个参数的构造函数即可!
  • @david 如果这会导致两者在命令行中看起来像一个全名,那么是的。
  • @isnot2bad 我该怎么做?它看起来像'Name n1 = new Name (firstName, lastName);'吗?抱歉,我对 Java 很陌生。

标签: java string class object constructor


【解决方案1】:

改变你的主要方法..

    String firstName = JOptionPane.showInputDialog("Enter firstname");
    String lastName =  JOptionPane.showInputDialog("Enter lastname");      
    Name n1 = new Name(firstName,lastName);
    System.out.println(n1.firstName + " " + n1.lastName);

在名称类中

static class Name {

    private String firstName;
    private String lastName;

    public Name(String firstName,String lastName){
         this.firstName = firstName;
         this.lastName = lastName;
    }

}

【讨论】:

    【解决方案2】:

    我认为在创建名称对象时,您应该通过构造函数限制用户传递两个名称。

    class Name
    {
        private String firstName;
        private String lastName;
        public Name(String fname,String lname){
             this.firstName = fname;
             this.lastName = lname;
        }
    }
    

    所以名称对象必须有这个属性值。

    【讨论】:

      【解决方案3】:

      Name 类定义一个构造函数,该构造函数接受两个参数,名字和姓氏,如@SubhrajyotiMajumder 所示。

      然后要求用户输入这两个名字,并用它们构造一个Name对象:

      Scanner sc = new Scanner(System.in);
      System.out.println("Enter first name : ");
      String firstName = sc.nextLine();
      System.out.println("Enter last name : ");
      String lastName = sc.nextLine();
      
      Name n1 = new Name(firstName, lastName);
      

      【讨论】:

        【解决方案4】:

        您是否需要这样的双参数构造函数:

        class Name {
            String firstName;
            String lastName;
        
            public Name(String first, String last) {
                this.firstName = first;
                this.lastName = last;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-30
          • 1970-01-01
          • 2019-09-24
          • 2021-02-03
          • 2022-01-25
          • 1970-01-01
          相关资源
          最近更新 更多