声明一个抽象类Person,此类中定义无参无返回值得抽象方法print(); 声明Teacher类、Student类,均继承自Person类,并复写了print()方法; 运用多态方式实例化子类对象并调用print()方法打印输出信息; 测试类: 定义父类成员p,分别指向不同的子类对象,调用print方法,观察结果。 【输入形式】无 【输出形式】 I am a Student,I love studying. I am a Teacher,I love teaching. 【样例输入】无
时间: 2024-03-12 22:43:10 浏览: 148
以下是代码实现:
```java
abstract class Person {
public abstract void print();
}
class Teacher extends Person {
public void print() {
System.out.println("I am a Teacher,I love teaching.");
}
}
class Student extends Person {
public void print() {
System.out.println("I am a Student,I love studying.");
}
}
public class Test {
public static void main(String[] args) {
Person p1 = new Teacher();
p1.print();
Person p2 = new Student();
p2.print();
}
}
```
输出结果为:
```
I am a Teacher,I love teaching.
I am a Student,I love studying.
```
相关问题
声明一个抽象类Person,此类中定义无参无返回值得抽象方法print(); 声明Teacher类、Student类,均继承自Person类,并复写了print()方法; 运用多态方式实例化子类对象并调用print()方法打印输出信息;
代码如下:
```java
// 定义抽象类 Person
abstract class Person {
public abstract void print();
}
// 定义 Teacher 类,继承自 Person
class Teacher extends Person {
@Override
public void print() {
System.out.println("我是一名老师。");
}
}
// 定义 Student 类,继承自 Person
class Student extends Person {
@Override
public void print() {
System.out.println("我是一名学生。");
}
}
// 测试类
public class Test {
public static void main(String[] args) {
// 多态方式实例化子类对象
Person p1 = new Teacher();
Person p2 = new Student();
// 调用 print() 方法打印输出信息
p1.print(); // 输出:我是一名老师。
p2.print(); // 输出:我是一名学生。
}
}
```
运行结果:
```
我是一名老师。
我是一名学生。
```
1.编写程序:在教学管理系统中有教师、教学管理人员和学生,定义一个抽象类 Person,用 Teacher、 Manager、 Student去继承 Person类,完成类的设计.
以下是一个可能的程序实现:
```python
# 定义 Person 抽象类
from abc import ABC, abstractmethod
class Person(ABC):
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
@abstractmethod
def introduce(self):
pass
# 定义 Teacher 类,继承自 Person 类
class Teacher(Person):
def __init__(self, name, age, gender, department):
super().__init__(name, age, gender)
self.department = department
def introduce(self):
print(f"I am a teacher. My name is {self.name}, and I am {self.age} years old. I work in the {self.department} department.")
# 定义 Manager 类,继承自 Person 类
class Manager(Person):
def __init__(self, name, age, gender, title):
super().__init__(name, age, gender)
self.title = title
def introduce(self):
print(f"I am a manager. My name is {self.name}, and I am {self.age} years old. My title is {self.title}.")
# 定义 Student 类,继承自 Person 类
class Student(Person):
def __init__(self, name, age, gender, grade):
super().__init__(name, age, gender)
self.grade = grade
def introduce(self):
print(f"I am a student. My name is {self.name}, and I am {self.age} years old. I am in grade {self.grade}.")
```
在这个程序中,我们定义了一个 `Person` 抽象类,它有一个抽象方法 `introduce`,用于在子类中实现自我介绍。然后,我们定义了三个子类:`Teacher`、`Manager` 和 `Student`,它们都继承自 `Person` 类,并实现了 `introduce` 方法。每个类都有一些特定的属性,例如 `Teacher` 类有一个 `department` 属性,表示教学部门。
使用这个程序,我们可以创建不同类型的人物对象,例如:
```python
t = Teacher("Tom", 35, "male", "mathematics")
t.introduce() # 输出:I am a teacher. My name is Tom, and I am 35 years old. I work in the mathematics department.
m = Manager("Mary", 40, "female", "dean")
m.introduce() # 输出:I am a manager. My name is Mary, and I am 40 years old. My title is dean.
s = Student("Sam", 18, "male", 12)
s.introduce() # 输出:I am a student. My name is Sam, and I am 18 years old. I am in grade 12.
```
这样,我们就完成了题目中要求的类的设计。
阅读全文