Day8之面向对象复习1

因为还不是很熟悉这一块的知识准备复习个两天再学习下一章
#!/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('请输入0100的数字')

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是一个listb = Animal() # b是一个Animalc = Cat() # c是一个Catprint('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'))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值