您的位置:首页 > 其它

Day8之面向对象复习1

2017-11-28 23:13 218 查看
因为还不是很熟悉这一块的知识准备复习个两天再学习下一章

#!/usr/bin/python3
# -*-coding:UTF-8-*-

class Student(object):
def __init__(self, name, score):
self.__name = name
self.__score = score

def info(self):
print('学生名称:%s, 成绩:%s' % (self.__name, self.__score))

# 定义私有方法
def __foo(self):
print('这是私有方法')

# 获取私有变量
def get_score(self):
return self.__score

# 修改私有变量
def set_score(self, score):
if 0 <= score <= 100:
self.__score = score
else:
print('请输入0到100的数字')

stu = Student('xiaomeng', 95)
stu.info()
print('修改前的成绩:', stu.get_score())
stu.set_score(-88)
print('修改后的成绩:', stu.get_score())

# 继承
class Animal(object):
def running(self):
print('Running...')

class Cat(Animal):
pass
# 子类会获得父类全部非私有的方法
cat = Cat()
cat.running()

# 多态
# 判断类isinstance()
a = list() # a是一个list类
b = Animal() # b是一个Animal类
c = Cat() # c是一个Cat类
print('a是否为list类:', isinstance(a, list))
print('b是否为Animal类:', isinstance(b, Animal))
print('c是否为Cat类:', isinstance(c, Cat))
print('c是否为Animal类:', isinstance(c, Animal))

# 封装
# 多重继承

# 获取对象信息
# type()
print('123 的类为:', type(123))
print('abs 的类:', type(abs))

import types
def func():
pass
print(type(func) == types.FunctionType)
print(type(abs) == types.BuiltinFunctionType)
print(type(lambda x: x) == types.LambdaType)
print(type((x for x in range(10))) == types.GeneratorType)

# isinstance()
# 判断继承关系

# dir()
# 获取一个对象所有的类和方法
print(dir('abc'))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  DAY