一、类的概念python
面向对象编程的2个很是重要的概念:类和对象是面向对象编程的核心。编程
在使用对象的过程当中,为了将具备共同特征和行为的一组对象抽象定义,提出了另一个新的概念——类安全
例如:人类,汽车类,鸟类,狗类,都是多个具备相同特征事物的合集概念。函数
二、对象的概念(万事万物皆对象)this
对象是具体事物,具备惟一性,必定能肯定是哪个的就是对象。spa
例如:周杰伦、地球、老王的宝马、小李的泰迪设计
类(Class) 由3个部分构成指针
狗类的设计code
定义一个类,格式以下:对象
class 类名: 方法列表
举个例子:定义一个Car类
#定义类
class Car: #方法
def getCarInfo(self): print("这是一辆%s牌汽车"%self.name) def move(self) print("车正在移动")
说明:
经过上面,定义了一个Car类;就比如有车一个张图纸,那么接下来就应该把图纸交给生成工人们去生成了
python中,能够根据已经定义的类去建立出一个个对象
建立对象的格式为:
对象名 = 类名()
建立对象demo
#定义类
class Car: def move(self): print("车在奔跑") #建立对象
BMW = Car() #对象调用方法
BMW.move() #给对象添加属性
BMW.name = "宝马"
#经过点语法调用对象属性
print(BMW.name)
运行结果为:
车在奔跑
宝马
说明:
在上一面的demo中,咱们已经给BMW这个对象添加了1个属性name,试想若是再次建立一个对象的话,确定也须要进行添加属性,显然这样作很费事,那么有没有办法可以在建立对象的时候,就顺便把车这个对象的属性给设置呢?
答案就是:_ _init_ _()方法
一、使用方法
class 类名: #初始化方法,用来完成一些默认的设定
def __init__(): pass
二、_ _init_ _()方法的调用
class Car: def __init__(self): self.name = "宝马"
def move(): print("车在跑") #建立对象
bmw = Car() print("这是一辆%s牌汽车"%bmw.name)
运行结果为:这是一辆宝马牌汽车
说明:当建立Car对象后,在没有调用__init__()方法的前提下,bmw就默认有了name属性为宝马
三、自定义_ _init_ _()方法
class Car: def __init__(self,newName,newColor): self.name = newName self.color = newColor def move(): print("车在跑") #建立对象
bmw = Car("宝马","黑色") print("这是一辆%s牌汽车"%bmw.name) print("这辆汽车的颜色是%s"%bmw.color)
运行结果为:
这是一辆宝马牌汽车
这辆汽车的颜色是黑色
说明:
class Car: def __init__(self,newName,newColor): self.name = newName self.color = newColor def move(): print("车在跑") def __str__(self): msg = "你好,我是一辆%s的%s牌汽车"%(self.color,self.name) return msg #建立对象
bmw = Car("宝马","黑色") print(bmw)
运行结果为:你好,我是一辆黑色的宝马牌汽车
说明:
若是有一个对象,当须要对其进行修改属性时,有2种方法
为了更好的保存属性安全,即不能随意修改,通常的处理方式为
1 class Person(object): 2 def __init__(self, name): 3 #以__开头的属性表示私有属性,不容许外部访问
4 self.__name = name 5
6 def getName(self): 7 return self.__name
8
9 def setName(self,newName): 10 if len(newName)>=5: 11 self.__name = newName 12 else: 13 print("Error:输入的名字长度不够") 14
15 xiaoming = Person("Se7eN_HOU") 16 print(xiaoming.__name)
运行结果为:
Traceback (most recent call last): File "C:\Users\Se7eN_HOU\Desktop\demo.py", line 16, in <module>
print(xiaoming.__name) AttributeError: 'Person' object has no attribute '__name'
修改成以下代码:
class Person(object): def __init__(self, name): #以__开头的属性表示私有属性,不容许外部访问
self.__name = name def getName(self): return self.__name
def setName(self,newName): if len(newName)>=5: self.__name = newName else: print("Error:输入的名字长度不够") xiaoming = Person("Se7eN") xiaoming.setName("Se7eN_HOU") print(xiaoming.getName()) xiaoming.setName("HOU") print(xiaoming.getName())
运行结果为:
Se7eN_HOU
Error:输入的名字长度不够
Se7eN_HOU
说明:
建立对象后,python解释器默认调用_ _init_ _()方法;
当删除一个对象时,python解释器也会默认调用一个方法,这个方法为_ _del_ _()方法
class Person(object): #初始化方法,建立对象会被自动调用
def __init__(self, name): print("__init__方法被调用") self.__name = name #析构方法:当对象删除是会被调用
def __del__(self): print("__del__方法被调用") def getName(self): return self.__name
def setName(self,newName): if len(newName)>=5: self.__name = newName else: print("Error:输入的名字长度不够") xiaoming = Person("Se7eN") print("------立刻删除xiaoming------") del xiaoming laowang = Person("laowang") laowang2 = laowang laowang3 = laowang print("------立刻删除laowang------") del laowang print("------立刻删除laowang2------") del laowang2 print("------立刻删除laowang3------") del laowang3
运行结果为:
__init__方法被调用
------立刻删除xiaoming------
__del__方法被调用
__init__方法被调用
------立刻删除laowang------
------立刻删除laowang2------
------立刻删除laowang3------
__del__方法被调用
说明: