# !/usr/bin/python3.4
# -*- coding: utf-8 -*-
'''
# 类的应用
class Student(object):
def __init__(self, name, score):
self.name = name
self.score = score
def print_score(self):
# 如果加上下划线,则外部不能访问
# bart.print_score()报错
# print('%s: %s' % (self.__name, self.__score))
print('%s: %s' % (self.name, self.score))
if __name__ == "__main__":
bart = Student('Bart_Simpson', 59)
lisa = Student('Lisa_Simpson', 87)
# Bart_Simpson:59
bart.print_score()
# Lisa_Simpson:87
lisa.print_score()
'''
'''
# 类的应用
class Student(object):
def __init__(self, name, score):
self.name = name
self.score = score
def get_grade(self):
if self.score >= 90:
return 'A'
elif self.score >= 60:
return 'B'
else:
return 'C'
if __name__ == "__main__":
bart = Student('Bart Simpson', 59)
# Bart Simpson
print(bart.name)
# 59
print(bart.score)
# C
print(bart.get_grade())
'''
'''
# 类的继承
class Animal(object):
def run(self):
print('Animal is running...')
# 继承多个类
# class Dog(Animal, Runnable):
# pass
# 类的继承
class Dog(Animal):
# 如果不写下面的run,那么运行出现的是Animal is running...
pass
# 如果有相同的run(),子类覆盖父类
def run(self):
print('Dog is running...')
# 类的继承
class Cat(Animal):
# 如果不写下面的run,那么运行出现的是Animal is running...
pass
# 如果有相同的run(),子类覆盖父类
def run(self):
print('Cat is running...')
def run_twice(animal):
animal.run()
if __name__ == "__main__":
# Dog is running...
dog = Dog()
dog.run()
# Cat is running...
cat = Cat()
cat.run()
# Animal is running...
run_twice(Animal())
run_twice(Dog())
run_twice(Cat())
# 获得Animal的所有属性和方法
print(dir(Animal))
'''