python面向对象
python类和对象
类是描述对象属性和行为的集合,对象拥有自己的特征和行为。
在python中文件名和类名可以不一致
# 定义类
class ClassName:
class_variable1 = xxx
class_variable2 = xxx
count = 0
def __init__(self,xxx,,,,):
self.xxx = xxx
,,,,,,
ClassName.count += 1
def speciality(self):
print ...self.xxxx
#实例化对象
objectname = ClassName(self,xxx,,,,)
objectname.speciality()
新建一个类:
# 新建一个类
class Car:
doors = 4 # 这个里面是类里面自动生成
# 固定写法 init函数,规定这个类有哪些基本属性
# 每次init会自动运行初始化操作
def __init__(self, name, color): # 这两个参数是用来使用函数初始化时候使用的
self.name = name
self.color = color
def showinfo(self):
print 'This car is called %s,%s' % (self.name, self.color)
# 实例化对象,这里面就需要init初始化参数输入,否者会报错
car1 = Car('BMW', 'Black')
car2 = Car('QQ飞车', 'Yellow')
car1.showinfo()
car2.showinfo()
print car1.doors
输出:
This car is called BMW,Black
This car is called QQ飞车,Yellow
4
类的属性和方法:
1.类的属性直接写在类里面,不是写在方法里面,调用的时候直接使用ClassName.attriname
2.内置类属性
__dict__
类的属性__doc__
类的文档字符串__name__
类名__model__
模块__bases__
父亲属性
print Employee.__dict__ # 类的属性
print Employee.__doc__ # 类的文档
print Employee.__name__ # 类的类名
print Employee.__module__ # 类的模块
print Employee.__bases__ # 类的父类信息
3.类的私有变量和公有变量:在python中以__+name
开头的都是私有变量,其他的都是公有变量
class JustCounter:
__private_count = 0 # 类的私有变量,外部不能进行访问。这个是类的私有变量,调用是使用self的
public_connt = 0
def __init__(self):
pass
def count(self):
self.__private_count += 1 # 访问私有变量通过self
JustCounter.public_connt += 1 # 访问公有变量使用className
counter1 = JustCounter()
counter1.count()
counter1.count()
print JustCounter.public_count
print counter1.public_count
counter2 = JustCounter()
counter2.count()
counter2.count()
print JustCounter.public_count
print counter2.public_count
输出:
2
2
4
4
不管是对象还是类都不能访问私有对象
4.对类操作的一些方法:
getattr()
setattr()
hasattr()
delattr()
1.函数内可以执行外部函数
2.不要让 a = aaa()
,这赋值的不是函数,而是函数执行的结果 , a = aaa
这才是函数
Python类的继承(inherit)
1.代码的重用,通过子类父类,描述的对象之间的关系
python中object是所有对象的父类
class Animal(object):
def __init__(self, name, food):
self.name = name
self.food = food
def voice(self, v):
print self.name, '叫出了', v
class Dog(Animal):
def __init__(self, name, food, age):
super(Dog,self).__init__(name, food) # 继承自父类的属性
self.age = age # 自己的属性
class Cat(Animal):
def __init__(self, name, food, attract_num):
super(Cat,self).__init__(name,food)
self.attract_num = attract_num
# 布偶猫,又称“布拉多尔(Ragdoll)”,发源于美国,是一种杂交品种宠物猫。
# 是现存体型最大、体重最重的猫之一。
ragdoll = Cat('布偶', '鱼', 5)
Husky = Dog('饭团', '肉', 2)
Husky.voice('嗷呜。。') # 使用父类的方法
ragdoll.voice('m喵喵。。')
输出:
饭团 叫出了 嗷呜。。
布偶 叫出了 m喵喵。。
2.多重继承:如果在继承元组中列了一个以上的类,就是多重继承
先调用本身的方法,本身没有方法的再调用父类方法,父类之间有相同的方法时候,这个时候按照继承的顺序(person,girl)从左到右,谁在继承前面用谁的。
class Person(object):
def eye(self):
print 'two eyes'
def speak(self):
print '说的是person的'
class Girl(object):
def color(self):
print 'the girl\'s is white'
def speak(self):
print '说的是gril的'
class HotGirl(Person, Girl):
def eye(self):
print 'big eyes'
# 先调用本身的方法,本身没有方法的再调用父类方法,父类之间有相同的方法时候,这个时候按照继承的顺序(person,girl)从左到右,谁在继承前面用谁的。
littegirl = HotGirl()
littegirl.eye()
littegirl.speak()
输出:
big eyes
说的是person的
多重继承中参数使用的问题:
class First(object):
num = 1
class Second(First):
pass
two = Second()
print two.num
输出:
1
class First(object):
num = 1
class Second(First):
num = 2 #在子类中有参数了
two = Second()
print two.num
输出:
2
class First(object):
num = 1
class Second(First):
num = 2
two = Second()
two.num = 3 # 给赋值
print two.num
print Second.num
输出:
3
2
小结:继承中的一些特点
◆ 在继承中基类的构造(init()方法)不会被自动调用,它需要在其派生类的构造中亲自专门调用。
◆ 在调用基类的方法时,需要加上基类的类名前缀,且需要带上self参数变量。区别于在类中调用普通函数时并不需要带上self参数
◆ Python总是首先查找对应类型的方法,如果它不能在派生类中找到对应的方法,它才开始到基类中逐个查找。(先在本类中查找调用的方法,找不到才去基类中找)。