在java中创建一个方法来打印调用另一个类的特定格式的arraylist的内容

wz3gfoph  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(288)

在本例中,我想将一所学校的学生添加到arraylist中,并使用另一个类中定义的另一个方法的多态性格式打印数组列表。

import java.util.ArrayList;

class Student{
   public static String name;
   public static String gender;
   public static int age;

   //setting default values for the member variables
   public Student(){
       name = "default name";
       gender = "default gender";
       age = 0;
   }    

  //constructor of the parameters for the name, gender, and age.
   public Student(String name, String gender, int age){
       Student.name = name;
       Student.gender = gender;
       Student.age = age;
   }

   // Polymorphism to print with a specific format
   public static void printInfo(){
       System.out.println(name+", "+gender+", "+age+"\n");
   }
}

// creating a class for Male students
class Male extends Student{
    private static String gender;

    public Male(String name, int age){
        super(name, gender, age);

        Male.gender = "Male";
    }
}

// creating a class for Female students
class Female extends Student{
    private static String gender;

    public Female(String name, int age){
        super(name, gender, age);

        Female.gender = "Female";
    }
}

// create a class for school to collect all the students
class School{
      static ArrayList<Student> students;
      public static void addStudent(Student a){
          ArrayList<Student> students = new ArrayList<Student>();
          students.add(a);
      }
      public void printAllInfo(){
         //call the Student.printInfo method from the Student class
      }
}

public class SchoolBuilder{
    public static void main(String[] args){
       School.addStudent(new Male("Sam",13));
       School.addStudent(new Male("John",11));
       School.addStudent(new Female("Elle",12));
       School.addStudent(new Male("Paul",12));
       School.addStudent(new Female("Javinia",11));
       School.addStudent(new Male("Paperino",12));

       //PRint all by calling School.printAllInfo should print the formatted ArrayList 
    }
}

输出应如下所示:

Sam, Male, 13
  John, Male, 11
  Elle, Female, 12
  Paul, Male, 12
  Javinia, Female, 11
  Paperino, Male, 12

我是java新手,就是不知道怎么做。看起来很简单。我为gender创建了两个类,因为以后可以很容易地添加一个包含所有男性或女性的csv文件,并将它们添加到分别调用男性和女性类的数据集中。
谢谢你的时间和帮助。

lstz6jyr

lstz6jyr1#

这将是一类男性,同样是女性:

// creating a class for Male students
class Male extends Student{
    private static final String gender = "Male";//This should be constant for all instances of class and because of that it is "static final"

    public Male(String name, int age){
        super(name, gender, age);
    }
}

下一个学校不会有任何静态的东西。

import java.util.ArrayList;

// create a class for school to collect all the students
class School {
    private ArrayList<Student> students; //Should not be static, and initialization moved in constructor

    public School() {
        students = new ArrayList<>();
    }

    public void addStudent(Student a) {
        students.add(a);
    }

    public void printAllInfo() {
        for (Student s : students) {
            s.printInfo();
        }
    }
}

最后,主类应创建学校示例,添加学生并打印:

public class SchoolBuilder{
    public static void main(String[] args){
        School school=new School();
        school.addStudent(new Male("Sam",13));
        school.addStudent(new Male("John",11));
        school.addStudent(new Female("Elle",12));
        school.addStudent(new Male("Paul",12));
        school.addStudent(new Female("Javinia",11));
        school.addStudent(new Male("Paperino",12));

        school.printAllInfo();
    }
}

最后,student中的这个方法不应该有\n in string,因为它将再添加一行。

// Polymorphism to print with a specific format
public void printInfo(){
    System.out.println(name+", "+gender+", "+age+"\n");
}
ou6hu8tu

ou6hu8tu2#

通常,我们通过重写 toString() 方法,然后打印。
以下是建设性的批评:这里有很多问题。
在这种情况下,您希望避免使用静态上下文,因为静态变量属于一个类。例如,不是每个学生都有相同的名字。
同样,你也不会打电话 Student.function() ,你会打电话吗 this.function() 只为那个学生而不是班上的学生。 this 是学生的一个例子。
你肯定不想这么做 ArrayList<Student> students = new ArrayList<Student>(); 在addstudent()方法中。每次添加学生时都会重置学生列表。将其保留在类级声明中,或者最好添加一个构造函数。

import java.util.ArrayList;

class Student{
   public String name;
   public String gender;
   public int age;

   //setting default values for the member variables
   public Student(){
       name = "default name";
       gender = "default gender";
       age = 0;
   }    

  //constructor of the parameters for the name, gender, and age.
   public Student(String name, String gender, int age){
       this.name = name;
       this.gender = gender;
       this.age = age;
   }

   // Polymorphism to print with a specific format
   public String printInfo(){
       return name+", "+gender+", "+age+"\n";
   }
}

// creating a class for Male students
class Male extends Student{
    private static String gender;

    public Male(String name, int age){
        super(name, gender, age);

        Male.gender = "Male";
    }
}

// creating a class for Female students
class Female extends Student{
    private static String gender;

    public Female(String name, int age){
        super(name, gender, age);

        Female.gender = "Female";
    }
}

// create a class for school to collect all the students
class School{
      ArrayList<Student> students = new ArrayList<Student>();

      public void addStudent(Student a) {
          students.add(a);
      }

      public void printAllInfo(){
         //call the Student.printInfo method from the Student class
      }

      public String toString() {
          String string = "";

          for (Student s : students) {
              string += s.printInfo();
          }

          return string;
      }
}

public class Work{
    public static void main(String[] args){
        School sch = new School();

       sch.addStudent(new Male("Sam",13));
       sch.addStudent(new Male("John",11));
       sch.addStudent(new Female("Elle",12));
       sch.addStudent(new Male("Paul",12));
       sch.addStudent(new Female("Javinia",11));
       sch.addStudent(new Male("Paperino",12));

       System.out.println(sch.toString()); 
    }
}
pdtvr36n

pdtvr36n3#

您的实现中有一些错误:
这个 Student 对象不应具有 static 领域。 static 这意味着它们不是指向特定的对象,而是指向类本身。
对象是类的特定实现。你定义了一个 Student 类来定义一个学生的长相,即用特定的设置创建一个学生。
当你申报班级时,你说一个学生通常有一个名字,性别和年龄。
当你用构造器刺激学生时( new Student("Sam", "Male", 13) )您可以定义具体的实现。

class Student {
      public String name;
        public String gender;
        public int age;

        //setting default values for the member variables
        public Student(){
            name = "default name";
            gender = "default gender";
            age = 0;
        }    

       //constructor of the parameters for the name, gender, and age.
        public Student(String name, String gender, int age){
            this.name = name;
            this.gender = gender;
            this.age = age;
        }

班级 Male 以及 Female 必须改变。延长期限并没有错 Student 类将默认值设置为 gender 现场。例如,如果 Male 类有一个特定的字段(例如 favouriteSoccerPlayer )而一般学生不应该有。在这种情况下 Male 类将从父类继承字段( Student )并添加另一个额外字段:

class Male extends Student {
            private String favouriteSoccerPlayer;

       public Male(String name,  int age) {
           super(name, "Male", age); // call the constructor of the super class
       }
       public Male(String name,  int age, String favouriteSoccerPlayer) {
           super(name, "Male", age); // call the constructor of the super class
           this.favouriteSoccerPlayer = favouriteSoccerPlayer;
       }

       // getter and setter for only this field
    }

例如,你可以这样做:

Male male = new Male("John", 32, "Maradona");
System.out.println(male.getName());

您将调用 Male 对象继承自 Student 班级。

相关问题