前言
类是一个十分抽象的概念 你可以简单的把他理解为一个代码块 或者具体理解为某种对象,它具有某种属性,可以执行某种操作
定义一个类
在python中定义一个类的格式有两种
# 第一种
class Empty:
pass
# 第二种
class Empty():
pass
在上面,只是定义了一个空类,没有任何实际意义 python创建类的时候,可以不带括号,也可以带,也可以显示继承object,如果带个()空括号,隐式的继承了object。这三种方式是相等的。
或者如果你想要继承其余的类,可以将其加入到括号之内
定义一个简单类
我们定义一个十分简单的类
类的名字为Dog
他属于斑点狗(spot),作为整体的类变量 然后,我们赋予他初始的一些属性 年龄 10岁 名字 tom 体重 10斤 然后,赋予他吃饭的动作,此动作回事其体重增加1/2进食的食物 定义一只dog,其动作为eat,属性为age 10,name=tom
class Dog:
kind = 'spot'
def __init__(self):
self.age = 10
self.name = 'tom'
self.weight = 10
def eat(self,food):
self.weight = self.weight + food/2
return self.weight
# 首先,我们实例化一个类,类名+括号就是实例化的具体表现
a = Dog()
# 然后,我们看下此时a的各项状态
print('age:',a.age)
print('name:',a.name)
print('weight:',a.weight)
print('kind:',a.kind)
age: 10
name: tom
weight: 10
kind: spot
此时,我们可以看到a的属性 年龄,名字,体重等等 假如我们需要一只新的狗子 那么,我们可以进行如下操作
b = Dog()
b.age =20
b.name = 'Happy'
b.weight = 50
# 然后,我们看下此时b的各项状态
print('age:',b.age)
print('name:',b.name)
print('weight:',b.weight)
age: 20
name: Happy
weight: 50
你可能注意到了,我并没有修改类属性,也就是a和b都是spot 类属性,是此类的共有属性
我们也可以对其进行修改
#例如
print('origin:',a.kind)
a.kind='big'
print('changed:',a.kind)
origin: spot
changed: big
a.kind
'big'
现在我们给狗子喂食
print('origin:',a.weight)
for i in range(1,10,1):
a.eat(i)
print('dog eat and the weight is ',a.weight)
origin: 10
dog eat and the weight is 10.5
dog eat and the weight is 11.5
dog eat and the weight is 13.0
dog eat and the weight is 15.0
dog eat and the weight is 17.5
dog eat and the weight is 20.5
dog eat and the weight is 24.0
dog eat and the weight is 28.0
dog eat and the weight is 32.5
加点细节
上面是,一个基本类 我们创建了一条狗子,并使其每次进食,体重都能增加1/2食物重量
现在,我们对其进行更多的,全方面的细节优化
1.加一些私有属性私有方法
在设计类时,有些属性和方法,我们并不需要完全展示给外界
所以,我们增加一些私有属性和方法
方法很简单在前面加__,两个下划线
class Dog:
kind = 'spot' # 共有属性
__lover = 'Happy' # 私有属性
def __init__(self):
self.age = 10
self.name = 'tom'
self.weight = 10
def eat(self,food):
self.weight = self.weight + food/2
return self.weight
def dog_lover(self):
print(self.__lover)
def __check_weight(self):
if self.weight > 40:
return 'Stop'
def auto_eat(self,days,food):
for i in range(1,days,1):
self.eat(food)
print(self.weight)
if self.__check_weight() == 'Stop':
return(self.weight)
break
# 然后初始化这个实例
a = Dog()
#看下dog的lover对象
print(a.__lover)
---------------------------------------------------------------------------
AttributeError
Traceback (most recent call last) in
1 #看下dog的lover对象
----> 2 print(a.__lover)
AttributeError: 'Dog' object has no attribute '__lover'
# 出现了错误
# 私有属性,私有属性在类外部无法直接进行访问
# 制作私有属性的目的也是为了让人无法直接访问
# 但是其在内部可以正常访问
# 例如
a.dog_lover()
Happy
a._Dog__check_weight()
# 当然,我们也可以使用其私有方法
a.auto_eat(100,10)
15.0
20.0
25.0
30.0
35.0
40.0
45.0
45.0
# 我们可以看到,经过内部的判定,其体重已经到了45公斤,停止进食
a.weight
45.0
2.实例变量
我们在上面可以看到实例变量始终为:
self.age = 10
self.name = 'tom'
self.weight = 10
其与类属性基本没有什么区别
那么,我们来编写一个很明显的可以区别实例变量和类属性的
class Dog:
kind = 'spot' # 共有属性
__lover = 'Happy' # 私有属性
def __init__(self,age,name,weight):
self.age = age
self.name = name
self.weight = weight
def eat(self,food):
self.weight = self.weight + food/2
return self.weight
def dog_lover(self):
print(self.__lover)
def __check_weight(self):
if self.weight > 40:
return 'Stop'
def auto_eat(self,days,food):
for i in range(1,days,1):
self.eat(food)
print(self.weight)
if self.__check_weight() == 'Stop':
return(self.weight)
break
c = Dog(20,'Puppy',50)
# 现在来看一下实例的属性
print('age:',c.age)
print('name:',c.name)
print('weight:',c.weight)
age: 20
name: Puppy
weight: 50
# 其类属性lover仍然为Happy,仍然是斑点狗
c.dog_lover()
print(c.kind)
Happy
spot
实例
我们此时已经创建了一个斑点狗的类属性,他们都喜欢‘Happy’
那么我们其实就可以进行真实的类的应用
假设,‘Happy’是一只漂亮的狗子,随机移动
我们有三只狗子,‘Bob’,‘Tom’,‘Chu’
这些狗子,每次会在吃完食物之后,随机移动一段距离
当狗子与happy之间的距离小于2时,那么此狗子将会变为lucky dog
import math
class Dog:
kind = 'spot' # 共有属性
__lover = 'Happy' # 私有属性
def __init__(self, age, name, weight):
self.age = age
self.name = name
self.weight = weight
self.coord_x = random.random() * 10
self.coord_y = random.random() * 10
def eat(self,food):
self.weight = self.weight + food/2
return self.weight
def dog_lover(self):
print(self.__lover)
def __check_weight(self):
if self.weight > 40:
return 'Stop'
def run(self):
if abs(self.coord_x) > 10 or abs(self.coord_y) > 10:
self.coord_x = random.random() * 10
self.coord_y = random.random() * 10
else:
self.coord_x = self.coord_x + math.sin((self.weight)) * random.random()
self.coord_y = self.coord_y + math.cos((self.weight)) * random.random()
return self.coord_x,self.coord_y
def auto_eat(self,days,food):
for i in range(1,days,1):
self.eat(food)
print(self.weight)
if self.__check_weight() == 'Stop':
return(self.weight)
break
# 现在定义一个happy
class DogH:
def __init__(self,weight):
self.weight = weight
self.coord_x = random.random() * 10
self.coord_y = random.random() * 10
def run(self):
if abs(self.coord_x) > 10 or abs(self.coord_y) > 10:
self.coord_x = random.random() * 10
self.coord_y = random.random() * 10
else:
self.coord_x = self.coord_x + math.sin((self.weight)) + random.random()
self.coord_y = self.coord_y + math.cos((self.weight)) + random.random()
return self.coord_x,self.coord_y
a = Dog(10,'Tom',10)
b = Dog(50,'Bob',20)
c = Dog(20,'Chu',30)
h = DogH(10)
from math import sqrt
for i in range(100):
print('Cycle:',i+1)
a.run()
b.run()
c.run()
h.run()
dis_a = sqrt((a.coord_x-h.coord_x)**2 + (a.coord_y-h.coord_y)**2)
dis_b = sqrt((b.coord_x-h.coord_x)**2 + (b.coord_y-h.coord_y)**2)
dis_c = sqrt((c.coord_x-h.coord_x)**2 + (c.coord_y-h.coord_y)**2)
print(a.name,b.name,c.name)
print(int(dis_a),int(dis_b),int(dis_c))
if min([dis_a,dis_b,dis_c]) << span=""> 2:
print('get happy dog')
break
Cycle: 1
Tom Bob Chu
3 5 7
Cycle: 2
Tom Bob Chu
4 6 8
Cycle: 3
Tom Bob Chu
3 6 7
Cycle: 4
Tom Bob Chu
2 7 7
Cycle: 5
Tom Bob Chu
1 8 6
get happy dog
目前,你可以看到在第5个cycle的时候,Dog Tom成为了happy dog