以下程序输出的是 class People: def say(self): print("名字是",self.name) class Animal: def display(self): print("animal") class Person(People, Animal): pass zhangsan=Person() zhangsanname="tom" zhangsan.say()
时间: 2024-01-22 11:21:27 浏览: 65
这段程序会输出以下内容:
```
名字是 tom
```
首先定义了两个类 `People` 和 `Animal`,`People` 类中有一个实例方法 `say`,用于输出实例的名字,`Animal` 类中有一个实例方法 `display`,用于输出字符串 "animal"。
然后定义了一个类 `Person`,继承自 `People` 和 `Animal` 两个类。由于 `Person` 类没有定义任何实例方法,所以其实例可以调用 `People` 和 `Animal` 中的实例方法。
接下来创建了一个 `Person` 类的实例 `zhangsan`,并将其名字设置为 "tom"。最后调用 `zhangsan` 的 `say` 方法,输出了名字 "tom"。
相关问题
完善代码#include<iostream.h> void main() {class Person: def __init__(self, name, gender, age, profession): self.name = name self.gender = gender self.age = age self.profession = profession def set_name(self, name): self.name = name def set_gender(self, gender): self.gender = gender def set_age(self, age): self.age = age def set_profession(self, profession): self.profession = profession def display_name(self): print("Name:", self.name) def display_gender(self): print("Gender:", self.gender) def display_age(self): print("Age:", self.age) def display_profession(self): print("Profession:", self.profession) def display_all(self): self.display_name() self.display_gender() self.display_age() self.display_profession() class Student(Person): def __init__(self, name, gender, age, profession, school, major, grade): super().__init__(name, gender, age, profession) self.school = school self.major = major self.grade = grade def set_school(self, school): self.school = school def set_major(self, major): self.major = major def set_grade(self, grade): self.grade = grade def display_school(self): print("School:", self.school) def display_major(self): print("Major:", self.major) def display_grade(self): print("Grade:", self.grade) def display_all(self): super().display_all() self.display_school() self.display_major() self.display_grade() person1 = Person("Alice", "Female", 25, "Engineer") person1.display_all() student1 = Student("Bob", "Male", 20, "Student", "XYZ University", "Computer Science", "Sophomore") student1.display_all() }
抱歉,这段代码不是 C++ 代码,而是 Python 代码。可以参考下面的完整代码:
```python
class Person:
def __init__(self, name, gender, age, profession):
self.name = name
self.gender = gender
self.age = age
self.profession = profession
def set_name(self, name):
self.name = name
def set_gender(self, gender):
self.gender = gender
def set_age(self, age):
self.age = age
def set_profession(self, profession):
self.profession = profession
def display_name(self):
print("Name:", self.name)
def display_gender(self):
print("Gender:", self.gender)
def display_age(self):
print("Age:", self.age)
def display_profession(self):
print("Profession:", self.profession)
def display_all(self):
self.display_name()
self.display_gender()
self.display_age()
self.display_profession()
class Student(Person):
def __init__(self, name, gender, age, profession, school, major, grade):
super().__init__(name, gender, age, profession)
self.school = school
self.major = major
self.grade = grade
def set_school(self, school):
self.school = school
def set_major(self, major):
self.major = major
def set_grade(self, grade):
self.grade = grade
def display_school(self):
print("School:", self.school)
def display_major(self):
print("Major:", self.major)
def display_grade(self):
print("Grade:", self.grade)
def display_all(self):
super().display_all()
self.display_school()
self.display_major()
self.display_grade()
person1 = Person("Alice", "Female", 25, "Engineer")
person1.display_all()
student1 = Student("Bob", "Male", 20, "Student", "XYZ University", "Computer Science", "Sophomore")
student1.display_all()
```
这个代码定义了两个类:`Person` 和 `Student`,其中 `Student` 是 `Person` 的子类。`Person` 类有四个属性:姓名、性别、年龄、职业,以及相应的 getter 和 setter 方法。`Student` 类在 `Person` 的基础上增加了三个属性:学校、专业、年级,也有相应的 getter 和 setter 方法。同时,`Student` 类重写了 `display_all()` 方法,打印出所有属性,包括父类属性和子类属性。
在代码的最后,创建了一个 `Person` 对象和一个 `Student` 对象,分别调用了它们的 `display_all()` 方法,打印出所有属性。
用Javaclass Animal: def __init__(self): self.sound = "叽里咕噜" def make_sound(self): print(self.sound)class Cat(Animal): def __init__(self): super().__init__() self.sound = "喵喵"class Dog(Animal): def __init__(self): super().__init__() self.sound = "汪汪"class Duck(Animal): def __init__(self): super().__init__() self.sound = "嘎嘎"class Chicken(Animal): def __init__(self): super().__init__() self.sound = "咯咯"# 测试代码animal_name = input("请输入动物名:")if animal_name == "猫": cat = Cat() print("猫 : " + cat.sound)elif animal_name == "狗": dog = Dog() print("狗 : " + dog.sound)elif animal_name == "鸭": duck = Duck() print("鸭 : " + duck.sound)elif animal_name == "鸡": chicken = Chicken() print("鸡 : " + chicken.sound)else: animal = Animal() print(animal_name + " : " + animal.sound)
这段代码定义了一个Animal类和四个子类Cat、Dog、Duck和Chicken。Animal类有一个属性sound和一个方法make_sound,子类继承了Animal类并重写了sound属性。每个子类都有自己的声音,分别是喵喵、汪汪、嘎嘎和咯咯。当调用make_sound方法时,会输出对应子类的声音。
阅读全文