找到你要的答案

Q:Can anyone tell me why am having Null for the object Staff and how can I fix it

Q:谁能告诉我为什么有空的对象的工作人员,以及如何我可以修复它

I have a class called main, and Staff that inherits from Person2

1) I should create an ArrayList that holds objects of type Staff

2) Then create Staff object using Person2 class and insert it in the list

3) Make user enter data for staff object then print it

I have a problem that when I run my main, I get Null values for what the user entered for object Staff which means there is something wrong I did

Main file

import java.util.ArrayList;
import java.util.Scanner;
public class Test {

public static void main(String[] args) {

    ArrayList<Staff>list=new ArrayList<Staff>();

    Scanner in=new Scanner(System.in);

    list.add(new Staff());
    list.add(new Staff("Marry", 23,500));

    System.out.println("Enter name");
    String name=in.next();

    System.out.println("Enter age");
    int age=in.nextInt();

    list.get(0).setName(name);
    list.get(0).setAge(age);

    Person2 c = new Staff(); 
    list.add((Staff) c); 
    System.out.println("Enter name");
    String name=in.next();

    System.out.println("Enter age");
    int age=in.nextInt();

    // Then what should I write here to not get null results for the object Staff


    for(int i=0; i<list.size();i++)
        System.out.println(list.get(i));

}

}

Person file

public class Person2 {

private String name;
private int age;
private String contactNumber;


public Person2()
{
    System.out.println(" Constructing default Person");

}

 Person2 ( String n, int a )
 { 
     System.out.println(" Constructing non-default Person");
     name = n;
     age = a;


 }

 public void setName( String name)
 {
     this.name = name;
 }

 public String getName()
 {
     return name;
 }

 public void setAge( int age)
 {
     this.age = age;
 }

 public int getAge()
 {
     return age;
 }


 public String toString()
 {
     return "Name : " +getName()
             +"\nAge :" +getAge()

 }
 }

Staff file

public class Staff extends Person2{

private double salary;

public Staff()
{
    System.out.println("Constructing default Staff ");
}

public Staff( String name, int age, double salary)
{
    super(name, age);
    System.out.println("Constructing non-default Staff ");
    this.salary = salary;
}

public void setSalary( double salary)
{
    this.salary = salary;
}

public double getSalary()
{
    return salary;
}

    public String toString()
{
    return super.toString() +"\nSalary : " +getSalary();
}
}

我有一类称为主,和工作人员,从2

1)我应该创建一个ArrayList对象持有型员工

2)建立员工对象使用2类并将它插入到列表

3)让用户输入数据为员工对象,然后打印它

我有一个问题,当我运行我的主,我得到的空值为用户输入对象的工作人员,这意味着有一些错误,我做了

主文件

import java.util.ArrayList;
import java.util.Scanner;
public class Test {

public static void main(String[] args) {

    ArrayList<Staff>list=new ArrayList<Staff>();

    Scanner in=new Scanner(System.in);

    list.add(new Staff());
    list.add(new Staff("Marry", 23,500));

    System.out.println("Enter name");
    String name=in.next();

    System.out.println("Enter age");
    int age=in.nextInt();

    list.get(0).setName(name);
    list.get(0).setAge(age);

    Person2 c = new Staff(); 
    list.add((Staff) c); 
    System.out.println("Enter name");
    String name=in.next();

    System.out.println("Enter age");
    int age=in.nextInt();

    // Then what should I write here to not get null results for the object Staff


    for(int i=0; i<list.size();i++)
        System.out.println(list.get(i));

}

}

个人档案

public class Person2 {

private String name;
private int age;
private String contactNumber;


public Person2()
{
    System.out.println(" Constructing default Person");

}

 Person2 ( String n, int a )
 { 
     System.out.println(" Constructing non-default Person");
     name = n;
     age = a;


 }

 public void setName( String name)
 {
     this.name = name;
 }

 public String getName()
 {
     return name;
 }

 public void setAge( int age)
 {
     this.age = age;
 }

 public int getAge()
 {
     return age;
 }


 public String toString()
 {
     return "Name : " +getName()
             +"\nAge :" +getAge()

 }
 }

员工档案

public class Staff extends Person2{

private double salary;

public Staff()
{
    System.out.println("Constructing default Staff ");
}

public Staff( String name, int age, double salary)
{
    super(name, age);
    System.out.println("Constructing non-default Staff ");
    this.salary = salary;
}

public void setSalary( double salary)
{
    this.salary = salary;
}

public double getSalary()
{
    return salary;
}

    public String toString()
{
    return super.toString() +"\nSalary : " +getSalary();
}
}
answer1: 回答1:

There are some serious mistakes in your main method. Lists are not supposed to be used like that.

I have provided a possible fix for the application. Took an assumption that you need to read details of 5 staff employees.

Not provided the salary part. You may add it if you wish

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {

public static void main(String[] args) {

    List<Staff>list=new ArrayList<Staff>();

    Scanner in=new Scanner(System.in);

    for (int i = 0; i<5; i ++) {
        Staff staff = new Staff();
        System.out.println("Enter name");
        String name=in.next();

        System.out.println("Enter age");
        int age=in.nextInt();

        staff.setAge(age);
        staff.setName(name);

        list.add(staff);
    }

    for (Staff staff : list) {
        System.out.println(staff.toString());
    }
}

}

I think this will do what you wish.

你的主要方法有一些严重的错误。列表不应该被使用。

我提供了一个可能的修复应用程序。假设你需要阅读5名员工的详细信息。

未提供工资部分。你可以添加它,如果你希望

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {

public static void main(String[] args) {

    List<Staff>list=new ArrayList<Staff>();

    Scanner in=new Scanner(System.in);

    for (int i = 0; i<5; i ++) {
        Staff staff = new Staff();
        System.out.println("Enter name");
        String name=in.next();

        System.out.println("Enter age");
        int age=in.nextInt();

        staff.setAge(age);
        staff.setName(name);

        list.add(staff);
    }

    for (Staff staff : list) {
        System.out.println(staff.toString());
    }
}

}

我想这会照你的意愿去做。

inheritance  arraylist  polymorphism