定义一 - 个 person 类,其中.属性:姓名name、性别 sex、年龄age、学号: 方法:设置人员的各个属性的 set inplt 方法:获取各属性值的 getinpst方法。 定义好类以后,再定义 2个人员进行测试。
时间: 2023-05-03 17:01:56 浏览: 64
该题目是要求定义一个 person 类,其中包括姓名 name、性别 sex、年龄 age、学号。方法包括设置人员的各个属性的 set_inplt 方法以及获取各属性值的 get_inplt 方法。定义好类以后,再定义两个人员进行测试。
相关问题
⚫ 定义一个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方法中定义不同类对象,并测试相关方法。用
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());
}
}
```
定义Student类继承Person类,有以下几个成员属性 继承自父类的变量:姓名(name),字符串类型(String);性别(sex),字符型(char);年龄(age), 整型(int)。 子类新增加的变量: 学号(number),长整型; 三门功课的成绩:哲学(phi),double类型;英语(eng),double类型;计算机(comp),double类型。 子类新增加的方法: 求三门功课成绩的最高分max():该方法没有参数,返回值为double型; 覆盖父类的同名方法:toString() 返回学号、姓名、性别、最高分信息。 3. 编写一个主函数,利用上述类定义创建一个或多个学生对象,学生信息如下: 学号:1234567 姓名: 张三 性别:男 年龄:20 最高分:95.0 第二个学生信息请填入自己的学号、姓名、性别。 并利用toString()方法在屏幕上输出学生信息
好的,根据您的要求,这是一个可能的实现:
```java
// 定义 Person 类
class Person {
protected String name; // 姓名
protected char sex; // 性别
protected int age; // 年龄
// 构造函数
public Person(String name, char sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
// toString 方法
public String toString() {
return "姓名:" + name + ",性别:" + sex + ",年龄:" + age;
}
}
// 定义 Student 类,继承自 Person 类
class Student extends Person {
private long number; // 学号
private double phi; // 哲学成绩
private double eng; // 英语成绩
private double comp; // 计算机成绩
// 构造函数
public Student(long number, String name, char sex, int age, double phi, double eng, double comp) {
super(name, sex, age);
this.number = number;
this.phi = phi;
this.eng = eng;
this.comp = comp;
}
// 求最高分
public double max() {
return Math.max(Math.max(phi, eng), comp);
}
// 覆盖 toString 方法
public String toString() {
return "学号:" + number + "," + super.toString() + ",最高分:" + max();
}
}
// 主函数
public class Main {
public static void main(String[] args) {
// 创建学生对象
Student s1 = new Student(1234567, "张三", '男', 20, 95.0, 90.0, 92.0);
Student s2 = new Student(你的学号, "你的姓名", '你的性别', 你的年龄, 80.0, 85.0, 88.0);
// 输出学生信息
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
```
注意,为了实现子类新增加的变量和方法,我们需要在子类中定义新的成员变量和方法,并且在构造函数中调用父类的构造函数来初始化继承自父类的变量。同时,为了覆盖父类的同名方法,我们需要在子类中定义一个同名的 toString 方法,并且在其中调用父类的 toString 方法。最后,在主函数中创建学生对象,并且调用 toString 方法输出学生信息。