⚫ 定义一个Person类,含: 姓名name、性别sex、年龄age等字段(均为私有属性),相应的属性获取/设置方法, 如getName/setName。 两个构造方法,一个无参一个有参,有参构造方法要能对以上三个属性初始化。 ⚫ Teacher类,继承自Person类。,增加: 职称(jobTitile)、部门(dept)等字段,对应的属性获取/设置方法; 同样两个构造方法,至少一个要能对父类初始化。 ⚫ Student类,继承Person类增加: 学号、入学时间、专业等字段,对应属性的设置/获取方法; 同样两个构造方法,至少一个要能对父类初始化。 ⚫ 给出主类Main类,定义main方法,分别创建以上对象,并测试方法。 注意,以上类如果在同一个文件中,只能给出一个public类。不要在一个类中定义类, 即不要出现下面的形式: publicclassMain{ publicclassPerson{ … } } 2. 相关要求如下。 ⚫ 定义形状抽象类(Shape): 求面积的抽象方法方法doublegetArea();返回对象信息toString()方法,类静态属性ID。 ⚫ 定义圆(Circle)类,含半径、圆心坐标等属性,并继承形状类,定义toString方法, 类属性ID及获取的静态方法。 ⚫ 定义圆柱体(Cylinder)类 继承自圆类,定义高属性,定义toString方法,类属性ID及获取的静态方法,定义返 回体积的方法getVolume。 ⚫ 主类main方法中定义不同类对象,并测试相关方法。用
时间: 2024-03-26 11:40:51 浏览: 130
Java语言实现一个Person类、Teacher类和Student类以及Shape类、Circle类和Cylinder类,并对其方法进行测试。
1. Person类、Teacher类和Student类的实现:
Person.java
```java
public class Person {
private String name;
private String sex;
private int age;
public Person() {
}
public Person(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
Teacher.java
```java
public class Teacher extends Person {
private String jobTitle;
private String dept;
public Teacher() {
}
public Teacher(String name, String sex, int age, String jobTitle, String dept) {
super(name, sex, age);
this.jobTitle = jobTitle;
this.dept = dept;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
}
```
Student.java
```java
public class Student extends Person {
private String studentId;
private String admissionTime;
private String major;
public Student() {
}
public Student(String name, String sex, int age, String studentId, String admissionTime, String major) {
super(name, sex, age);
this.studentId = studentId;
this.admissionTime = admissionTime;
this.major = major;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getAdmissionTime() {
return admissionTime;
}
public void setAdmissionTime(String admissionTime) {
this.admissionTime = admissionTime;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
```
2. Shape类、Circle类和Cylinder类的实现:
Shape.java
```java
public abstract class Shape {
private static int ID = 0;
public abstract double getArea();
public String toString() {
return "Shape";
}
public static int getID() {
return ID;
}
}
```
Circle.java
```java
public class Circle extends Shape {
private double radius;
private double x;
private double y;
public Circle(double radius, double x, double y) {
this.radius = radius;
this.x = x;
this.y = y;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getArea() {
return Math.PI * radius * radius;
}
public String toString() {
return "Circle";
}
public static int getID() {
return Shape.getID() + 1;
}
}
```
Cylinder.java
```java
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double x, double y, double height) {
super(radius, x, y);
this.height = height;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getVolume() {
return Math.PI * getRadius() * getRadius() * height;
}
public String toString() {
return "Cylinder";
}
public static int getID() {
return Circle.getID() + 1;
}
}
```
3. Main类的实现:
Main.java
```java
public class Main {
public static void main(String[] args) {
// 测试Person、Teacher和Student类
Person person = new Person();
person.setName("Tom");
person.setSex("male");
person.setAge(20);
System.out.println(person.getName() + " " + person.getSex() + " " + person.getAge());
Teacher teacher = new Teacher("Kate", "female", 30, "Professor", "Math");
System.out.println(teacher.getName() + " " + teacher.getSex() + " " + teacher.getAge() + " " + teacher.getJobTitle() + " " + teacher.getDept());
Student student = new Student("Jack", "male", 18, "20210001", "2021-09-01", "Computer Science");
System.out.println(student.getName() + " " + student.getSex() + " " + student.getAge() + " " + student.getStudentId() + " " + student.getAdmissionTime() + " " + student.getMajor());
// 测试Shape、Circle和Cylinder类
Circle circle = new Circle(2.0, 1.0, 1.0);
System.out.println(circle.toString() + " ID: " + circle.getID() + " radius: " + circle.getRadius() + " x: " + circle.getX() + " y: " + circle.getY() + " area: " + circle.getArea());
Cylinder cylinder = new Cylinder(2.0, 1.0, 1.0, 3.0);
System.out.println(cylinder.toString() + " ID: " + cylinder.getID() + " radius: " + cylinder.getRadius() + " x: " + cylinder.getX() + " y: " + cylinder.getY() + " height: " + cylinder.getHeight() + " volume: " + cylinder.getVolume());
}
}
```
阅读全文