# class Person:
# def __init__(self, name, age) :
# self.name = name
# self.age = age
# def say_hello(self) :
# print('name:', self.name, 'age:', self.age)
# mr_smith = Person('Smith', 30)
# mr_smith.say_hello()
# print(mr_smith)
class Person:
def __init__(self, name, age) :
self.name = name
self.age = age
def __str__(self) :
return 'name:{0} age:{1}'.format(self.name, self.age)
def say_hello(self) :
print('My name is {0} and I am {1} years old'.format(self.name, self.age))
mr_smith = Person('Smith', 30)
mr_smith.say_hello()
print(mr_smith) # name:Smith age:30
print(dir(mr_smith))
print(type(mr_smith))
Python——继承的实现
最新推荐文章于 2024-11-11 21:30:41 发布