1、定义一个Person类完成如下功能:(1)在Person类中有私有属性name,age, sex。(2)完成Person类的构造方法、getter/setter(3)重写Person类的toString()方法在测试类TestPerson类中创建两个Person对象,第一个用带参构造方法创建,第二个用默认构造方法创建,由用户输入其属性值,最后输出两个Person对象的信息。(输出的信息写自己的姓名,年龄以及性别)2、在问题1的基础上:(1)定义Person类的子类xxx(xxx用自己的姓名全拼替代)(2)在xxx类中添加一个私有成员变量score(分数).(3)在xxx类中添加带4个参数的构造方法:参数的顺序是name、 age、sex、score,并且分别为对应的属性赋值.(4)在xxx类中重写toString()方法,使其可以完整输出xxx对象的所有属性信息(输出自己的姓名,年龄,性别以及成绩)。(5)写一个测试类Testxxx类中完成如下功能(xxx用自己的姓名全拼替代):创建两个xxx对象,第一个用带参构造方法创建,第二个用默认构造方法创建,由用户输入其属性值,最后输出两个xxx对象的信息。(其中xxx用自己的姓名全称代替)3、在问题1的基础上,定义一个Person类型的一维数组,通过循环,可向数组中录入n个Person对象的值并输出(n从键盘输入)。4、在问题1 的基础上定义一个Person类型的List,通过循环,可向列表中录入n个Person对象的值并输出。
时间: 2024-03-06 12:49:54 浏览: 136
设计一个Person类,有Person类派生一个Student类和一个Teacher类
1. 实现Person类:
```
public class Person {
private String name;
private int age;
private String sex;
public Person(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public Person() {}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
@Override
public String toString() {
return "Name: " + name + ", Age: " + age + ", Sex: " + sex;
}
}
```
测试类TestPerson:
```
import java.util.Scanner;
public class TestPerson {
public static void main(String[] args) {
Person p1 = new Person("张三", 18, "男");
Scanner scanner = new Scanner(System.in);
Person p2 = new Person();
System.out.print("请输入姓名:");
p2.setName(scanner.nextLine());
System.out.print("请输入年龄:");
p2.setAge(scanner.nextInt());
scanner.nextLine();
System.out.print("请输入性别:");
p2.setSex(scanner.nextLine());
System.out.println(p1);
System.out.println(p2);
}
}
```
2. 实现xxx类:
```
public class ZhangHaoqi extends Person {
private double score;
public ZhangHaoqi(String name, int age, String sex, double score) {
super(name, age, sex);
this.score = score;
}
public ZhangHaoqi() {}
public double getScore() { return score; }
public void setScore(double score) { this.score = score; }
@Override
public String toString() {
return super.toString() + ", Score: " + score;
}
}
```
测试类TestZhangHaoqi:
```
import java.util.Scanner;
public class TestZhangHaoqi {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ZhangHaoqi z1 = new ZhangHaoqi("张浩奇", 20, "男", 90.5);
ZhangHaoqi z2 = new ZhangHaoqi();
System.out.print("请输入姓名:");
z2.setName(scanner.nextLine());
System.out.print("请输入年龄:");
z2.setAge(scanner.nextInt());
scanner.nextLine();
System.out.print("请输入性别:");
z2.setSex(scanner.nextLine());
System.out.print("请输入成绩:");
z2.setScore(scanner.nextDouble());
System.out.println(z1);
System.out.println(z2);
}
}
```
3. 录入n个Person对象:
```
import java.util.Scanner;
public class TestPersonArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要录入的人数:");
int n = scanner.nextInt();
scanner.nextLine();
Person[] persons = new Person[n];
for (int i = 0; i < n; i++) {
System.out.print("请输入姓名:");
String name = scanner.nextLine();
System.out.print("请输入年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("请输入性别:");
String sex = scanner.nextLine();
persons[i] = new Person(name, age, sex);
}
for (Person person : persons) {
System.out.println(person);
}
}
}
```
4. 录入n个Person对象到List中:
```
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TestPersonList {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要录入的人数:");
int n = scanner.nextInt();
scanner.nextLine();
List<Person> persons = new ArrayList<>();
for (int i = 0; i < n; i++) {
System.out.print("请输入姓名:");
String name = scanner.nextLine();
System.out.print("请输入年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("请输入性别:");
String sex = scanner.nextLine();
persons.add(new Person(name, age, sex));
}
for (Person person : persons) {
System.out.println(person);
}
}
}
```
阅读全文