1、类的概念
面向对象编程的2个非常重要的概念:类和对象是面向对象编程的核心。
在使用对象的过程中,为了将具有共同特征和行为的一组对象抽象定义,提出了另外一个新的概念——类
例如:人类,汽车类,鸟类,狗类,都是多个具有相同特征事物的合集概念。
2、对象的概念(万事万物皆对象)
对象是具体事物,具有唯一性,一定能确定是哪一个的就是对象。
例如:周杰伦、地球、老王的宝马、小李的泰迪
类(Class) 由3个部分构成
狗类的设计
定义一个类,格式如下:
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_ _()方法
1、使用方法
class 类名:
#初始化方法,用来完成一些默认的设定
def __init__():
pass
2、_ _init_ _()方法的调用
class Car:
def __init__(self):
self.name = "宝马"
def move():
print("车在跑")
#创建对象
bmw = Car()
print("这是一辆%s牌汽车"%bmw.name)
运行结果为:这是一辆宝马牌汽车
说明:当创建Car对象后,在没有调用__init__()方法的前提下,bmw就默认有了name属性为宝马
3、自定义_ _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__方法被调用
说明: