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



在此示例中,我想将学校的学生添加到 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 的新手,只是不知道该怎么做。这似乎很容易。 我为性别制作了这两个类,因为以后很容易添加所有男性或女性的 CSV 文件,并将它们添加到分别调用男性和女性类的数据集中。

感谢您的时间和帮助。

通常,我们通过重写某个类的toString()方法,然后打印它来做到这一点。

以下作为建设性的批评:这里有一堆问题。

  1. 在这种情况下,您希望避免使用静态上下文,因为静态 变量属于一个类。不是每个学生都会有相同的 例如,名称。
  2. 同样,您不会打电话给Student.function(),而是会打电话给this.function(),只为那个学生打电话,而不是班上的学生。this是学生的一个实例。
  3. 你绝对不想在你的addStudent((方法中做ArrayList<Student> students = new ArrayList<Student>();。你是 每次添加学生时重置学生列表。保持 在类级别声明中或更好的是,添加一个 构造 函数。
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()); 
}
}

您的实现中存在一些错误:

  1. 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;
}
  1. MaleFemale必须更改。扩展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类继承的方法。

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

// 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();
}
}

最后,学生中的此方法不应在字符串中具有,因为它将再添加一个新行。

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

相关内容

最新更新