Python 更加抽象-类和类型
2018-01-07 本文已影响0人
老生住长亭
Python 更加抽象
1、对象的特点
多态
继承
封装
类
特性
函数
方法 - 绑定方法
随意将函数的赋值一个变量,还会第self进行绑定
class Demo:
def method1(self):
print('I have a self')
def fun():
print('I dont have a self')
instance = Demo()
instance.method1()
instance.method1 = fun
instance.method1()
class Bird:
song = "sing song"
def sing(self):
print(self.song)
bird = Bird()
bird.sing()
aa = bird.sing
aa()
输出结果:
I have a self
I dont have a self
sing song
sing song
方法和属性的私有性,在属性或者方法前加两个下划线
class Method:
def __inAccessAble(self):
print("I am a inaccessable")
def accessAble(self):
self.__inAccessAble()
md = Method()
md.accessAble()
类的定义就是执行代码块
类可以有多个基类,即可以由多个父类,允许多继承
格式:class xx(父类1, 父类2)