public class Student { private String name; private String id; private String gender; private int age; private String major; public Student(String name, String id, String gender, int age, String major) { this.name = name; this.id = id; this.gender = gender; this.age = age; this.major = major; } // getter and setter methods}
时间: 2023-06-25 20:05:38 浏览: 72
这是一个Java类,名为Student,它有5个私有属性,分别是name、id、gender、age和major。它还有一个构造函数,用于初始化这些属性的值。此外,它还包含一些getter和setter方法,用于获取和设置属性值。这个类的作用可能是表示一个学生对象,可以用于在学生信息管理系统中进行学生信息的存储和操作。
相关问题
使用java完成下列任务1. Read the sample code listing 5.1 to 5.3 of Core Java. 2. Implement a class of Person including fields of id, name, gender, age, and the methods to access these fields as well as a desciption function. 3. Implement a class of Student that inherits the Person class by adding additional fields of major and gpa. In addition, the methods include the accessors to get all the fields, respectively. 4. Rewrite the description function, and test it using three kinds of initializations.
Sure, I can help you with that. Here is the solution to your problem:
1. Sample code listing 5.1 to 5.3 of Core Java:
```
//listing 5.1
public class Manager extends Employee {
private double bonus;
public Manager(String name, double salary, int year, int month, int day) {
super(name, salary, year, month, day);
bonus = 0;
}
public double getSalary() {
double baseSalary = super.getSalary();
return baseSalary + bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
}
//listing 5.2
public class Employee {
private String name;
private double salary;
private LocalDate hireDay;
public Employee(String name, double salary, int year, int month, int day) {
this.name = name;
this.salary = salary;
hireDay = LocalDate.of(year, month, day);
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public LocalDate getHireDay() {
return hireDay;
}
public void raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
}
}
//listing 5.3
public class ManagerTest {
public static void main(String[] args) {
Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
boss.setBonus(5000);
Employee[] staff = new Employee[3];
staff[0] = boss;
staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);
for (Employee e : staff) {
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
}
}
}
```
2. Implementation of a class of Person including fields of id, name, gender, age, and the methods to access these fields as well as a description function:
```
public class Person {
private int id;
private String name;
private String gender;
private int age;
public Person(int id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
public String getDescription() {
return "ID: " + id + ", Name: " + name + ", Gender: " + gender + ", Age: " + age;
}
}
```
3. Implementation of a class of Student that inherits the Person class by adding additional fields of major and gpa. In addition, the methods include the accessors to get all the fields, respectively:
```
public class Student extends Person {
private String major;
private double gpa;
public Student(int id, String name, String gender, int age, String major, double gpa) {
super(id, name, gender, age);
this.major = major;
this.gpa = gpa;
}
public String getMajor() {
return major;
}
public double getGpa() {
return gpa;
}
public String getDescription() {
return super.getDescription() + ", Major: " + major + ", GPA: " + gpa;
}
}
```
4. Rewrite the description function, and test it using three kinds of initializations:
```
public static void main(String[] args) {
// initialization 1
Person person1 = new Person(1, "John", "Male", 30);
System.out.println(person1.getDescription());
// initialization 2
Person person2 = new Person(2, "Jane", "Female", 25);
System.out.println(person2.getDescription());
// initialization 3
Student student1 = new Student(3, "Mike", "Male", 20, "Computer Science", 3.8);
System.out.println(student1.getDescription());
}
```
Output:
```
ID: 1, Name: John, Gender: Male, Age: 30
ID: 2, Name: Jane, Gender: Female, Age: 25
ID: 3, Name: Mike, Gender: Male, Age: 20, Major: Computer Science, GPA: 3.8
```
I hope this solution helps you. Let me know if you have any questions or need further assistance.
4. 建立一个普通人员类 Person,包含姓名(m name)、性别(mgender)、年 龄(mage)成员变量。 ①建立 Person 类,包含Private 成员m name、m sex、m age 变量。 ②建立 Person 的构造函数。 ③ 建立一个显示过程 Show0,显示该对象的数据。 ④派生一个学生类Student, 增加班级(mclass)、专业(m major),设计这些类的 构造函数。 ⑤建立川class.m major对应的属性函数 sClassO、sMajor0。 建立品示成员函数Show0,显示该学生对象所有成员的数据。
好的,我理解了你的问题。下面是对应的C++代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string m_name;
string m_gender;
int m_age;
public:
Person(string name, string gender, int age) : m_name(name), m_gender(gender), m_age(age) {}
void Show0() {
cout << "姓名:" << m_name << endl;
cout << "性别:" << m_gender << endl;
cout << "年龄:" << m_age << endl;
}
};
class Student : public Person {
private:
string m_class;
string m_major;
public:
Student(string name, string gender, int age, string class_, string major) : Person(name, gender, age), m_class(class_), m_major(major) {}
string sClassO() { return m_class; }
string sMajorO() { return m_major; }
void Show0() {
Person::Show0();
cout << "班级:" << m_class << endl;
cout << "专业:" << m_major << endl;
}
};
int main() {
Student s("张三", "男", 20, "计算机科学与技术1班", "软件工程");
s.Show0();
return 0;
}
```
这是一个包含Person和Student类的C++程序。Person类包含姓名(m_name)、性别(m_gender)和年龄(m_age)成员变量,以及构造函数和显示过程Show0。Student类继承自Person类,增加了班级(m_class)和专业(m_major)成员变量,以及构造函数和Show0函数。在Show0函数中,通过调用基类Person的Show0函数来显示Person类的成员变量,然后再显示Student类的班级和专业成员变量。
希望这个代码对你有帮助!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)