gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:42:01 27 4
gpt4 key购买 nike

在此示例中,我想将一所学校的学生添加到 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()只称呼该学生,而不是类(class)学生。 this是 Student 的一个实例。
  3. 你绝对不想做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());
}
}

关于java - 在JAVA中创建一个方法来调用另一个类的特定格式来打印ArrayList的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59902046/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com