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'))
相关文章推荐
- Day8之面向对象复习2
- 【学习笔记】java面向对象复习05
- 简单实例复习java面向对象
- JavaSE复习之二 基础知识:面向对象 补充(4)
- 面向对象程序设计-复习C++
- C#面向对象复习概要
- Python学习记录day8-面向对象进阶、反射和异常处理
- Java复习总结:Java面向对象程序设计 —面向对象(基础篇)
- java面向对象复习
- Java面向对象复习(二)
- 面向对象复习
- C++面向对象复习(一)——构造函数
- java基础复习 面向对象,String类,正则表达式
- day05--Java面向对象复习--Java常用类
- 黑马程序员-面向对象基本知识复习(1)
- 面向对象 复习(二)
- 面向对象复习
- C++面向对象复习(二)——析构函数
- Java-J2SE面向对象复习
- 简单的面向对象复习