"""
Version: 0.1
Author: freshbin
Date: 2019年8月21日
"""
print("=================================类 start================================================")
class Person:
pass
class Person01:
def __init__(self, name):
self.name = name
def say_hi(self):
print('Hello.', self.name)
p = Person01('myName')
p.say_hi()
print("=================================类 end================================================")
print("=================================类变量与对象变量 start================================================")
class Robot:
'''表示有一个带有名字的机器人。'''
#一个类变量,用来计数机器人的数量
population = 0
robots = []
def __init__(self, name):
'''
初始化数据
'''
self.name = name
print('(Initializing {})'.format(self.name))
#当有人被创建时,机器人将会增加人口数量
Robot.population += 1
Robot.robots.append(self)
def die(self):
'''我挂了
:return:
'''
Robot.robots.remove(self)
print('{} is being destroyed!'.format(self.name))
Robot.population -= 1
if Robot.population == 0:
print('{} was the last one.'.format(self.name))
else:
print('There are still {:d} robots working.'.format(Robot.population))
def say_hi(self):
'''
来自机器人的诚挚问候
没问题,你做得到。
:return:
'''
print('Greetings, my masters call me {}.'.format(self.name))
@classmethod
def displayRobots(cls):
if Robot.population > 0:
print('\nThe robots display:')
for robots in cls.robots:
print(robots.name)
@classmethod
def how_many(cls):
'''打印出当前的人口数量
:return:
'''
print('We have {:d} robots.'.format(cls.population))
droid1 = Robot('R2-D2')
droid1.say_hi()
Robot.how_many()
droid2 = Robot('C-3P0')
droid2.say_hi()
Robot.how_many()
Robot.displayRobots()
print('\nRobots can do some work here.\n')
print("Robots have finished their work. So let's destroy them.")
droid1.die()
droid2.die()
Robot.how_many()
'''
任何在类或对象之中使用的变量其命名应以下划线开头,其
它所有非此格式的名称都将是公开的,并可以为其它任何类或对象所使用。请记得这只是一
个约定,Python 并不强制如此(除了双下划线前缀这点)。
'''
print("=================================类变量与对象变量 end================================================")
print("=================================继承 start================================================")
class SchoolMember:
'''代表任何学校离的成员。'''
def __init__(self, name, age):
self.name = name
self.age = age
print('(Initialized SchoolMember: {})'.format(self.name))
def tell(self):
'''告诉我有关我的细节。
'''
print('Name:"{}" Age:"{}"'.format(self.name, self.age), end=" ")
class Teacher(SchoolMember):
'''代表一位老师'''
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print('(Initialized Teacher: {})'.format(self.name))
def tell(self):
SchoolMember.tell(self)
print('Salary: "{:d}"'.format(self.salary))
class Student(SchoolMember):
'''代表一位学生。'''
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print('(Initialized Student: {})'.format(self.name))
def tell(self):
'''
:return:
'''
SchoolMember.tell(self)
print('Marks: "{:d}"'.format(self.marks))
t = Teacher('Mrs.Shrividya', 40, 30000)
s = Student('Swaroop', 25, 75)
print()
members = [t, s]
for member in members:
# 对全体师生工作
member.tell()
print("=================================继承 end================================================")