类:把一类事物的相同的特征和动作整合到一起就是类,类是一个抽象的概念
对象:就是基于类而创建的一个具体的事物(具体存在的)也是特征和动作整合到一起
函数: def function(args): '函数文档字符串' '函数体' 类: class 类名: '类的文档字符串' '类体'
注意:类名规范,类名的首字母大写
#创建一个类
class Person:
pass
#用类Person实例化一个对象
d = Person() #返回一个对象
print(d) #<__main__.Person object at 0x0000022B3A244230>
class Student:
def __init__(self,name,age,sex):
self.name=name
self.age=age
self.sex=sex
def learn(self):
print('%s is learning' %self.name) #新增self.name
def eat(self):
print('%s is eating' %self.name)
def sleep(self):
print('%s is sleeping' %self.name)
s1=Student('李','男',18)
s2=Student('王','女',38)
s3=Student('牛','男',78)
print(s1.learn())
print(s2.name)
注意:__init__里面一定不可以return 任何值,会报错,只能return None ,但是一般也不会return None
类属性
类是用来描述一类事物,类的对象指的是这一类事物中的一个个体是事物就要有属性,属性分为
1:数据属性:就是变量
2:函数属性:就是函数,在面向对象里通常称为方法
注意:类和对象均用点来访问自己的属性
class Chinese:
#'这是一个中国人的类'
# age = '23'
def __init__(self, name,age):
self.name = name
self.age = age
def love_eat_food(self,food):
print('%s love eat %s' % (self.name, food))
p1 = Chinese('好人',234) # ---> __init__(self, name,age)
print(p1.name)
p1.love_eat_food('饼干')
类属性的增删改查
class ChinesePeople:
country = 'China'
def __init__(self,name):
self.name = name
def play_ball(self, ball):
print('%s 正在打 %s' % (self.name, ball))
def say_word(self, word):
print('%s'%word)
#查看类属性
print(ChinesePeople.country)
#修改类属性
ChinesePeople.country = 'CHINA'
print(ChinesePeople.country)
#删除类属性
del ChinesePeople.country
#增加类属性
ChinesePeople.country = 'CHINA'
ChinesePeople.location= 'lebron'
print(ChinesePeople.__dict__)
p1 = ChinesePeople('小王')
#输出一样
ChinesePeople.play_ball(p1,'排球')
p1.play_ball('排球')
ChinesePeople.say_word(p1,'你真美')
注意:__dict__
属性用于获取一个类或实例的所有属性和方法的字典
实例属性增删改查
#实例属性增删改查
class ChinesePeople:
country = 'China'
def __init__(self,name):
self.name = name
def play_ball(self, ball):
print('%s 正在打 %s' % (self.name, ball))
p1 = ChinesePeople('james')
print(p1.__dict__) # {'name': 'james'}
#查看
print(p1.name)
print(p1.country)
print(p1.play_ball)
#增加数据属性
p1.age = 18
print(p1.__dict__)
print(p1.age)
#------------------------注意-----------------------------------------------------
#增加函数属性,一般没人这么干
#这样子直接把数据属性和函数属性放在一起了
# {'name': 'james', 'age': 18, 'test': <function test at 0x000002374A43A200>}
# def test(self):
# print('我是实例的函数属性')
# p1.test = test
# print(p1.__dict__)
# p1.test(p1)
#注意:实例只有数据属性
#注意:不要修改底层的属性字典结构
p1.__dict__['sex']='male' #{'name': 'james', 'age': 18, 'sex': 'male'}
print(p1.__dict__)
print(p1.sex)
#-----------------------------------------------------------------------------------------
#修改
p1.age = 19
print(p1.__dict__)
print(p1.age)
#删除
del p1.age
print(p1.__dict__)
注意:
#增加函数属性,一般没人这么干
#这样子直接把数据属性和函数属性放在一起了
# {'name': 'james', 'age': 18, 'test': <function test at 0x000002374A43A200>}def test(self): print('我是实例的函数属性') p1.test = test print(p1.__dict__) p1.test(p1)#注意:实例只有数据属性
#注意:不要修改底层的属性字典结构
p1.__dict__['sex']='male' #{'name': 'james', 'age': 18, 'sex': 'male'}
print(p1.__dict__)
print(p1.sex)
类当一个作用域去用
#定义一个类,只当一个作用域去用,类似于C语言中的结构体
class Mydata:
pass
x = 10
y = 20
Mydata.x = 1
Mydata.y = 2
print(x,y ,Mydata.x,Mydata.y) #10 20 1 2
只是在实例的字典中新增一个字典,没有修改类本身的数据,只是在实例中新增了一个country而没有修改原本类中country的数据属性
class Chinese:
country = 'China'
def __init__(self, name):
self.name = name
def play_ball(self,ball):
print('%s 正在打 %s ' % (self.name, ball))
p1=Chinese('james')
print(p1.__dict__) #{'name': 'james'}
print(p1.country) #China
p1.country = '美国' #相当于在p1的字典里新增一个country {'name': 'james', 'country': '美国'}
print(p1.__dict__) #{'name': 'james', 'country': '美国'}
print('类的---->',Chinese .country) #China
print('实例的---->',p1.country) #美国
数据属性的查找
country = '中国'
class Chinese:
country = 'China'
def __init__(self, name):
self.name = name
print('---->',country) #----> 中国
p1 = Chinese('james')
#只有通过点‘.'来查找的才符合只在类里面找,但是我上面的country没有通过点来查找,所以可以输出数据
#Chinese.
#p.
print(Chinese.country) #China
p1 = Chinese('xxxx') #----> 中国
print(p1.country) # China
注意:只有通过点‘.'来查找的才符合只在类里面找,但是我上面的country没有通过点来查找,所以可以输出数据,可以看到我们有在类里面定义一个country,类外面也定义了一个country,但是我们没有用点的方式去调用,所以选的是外面的那个country,所以输出是'中国'
利用append修改类的数据属性
class Chinese:
country = 'China'
l = ['a','b']
def __init__(self, name):
self.name = name
p1 = Chinese('alex')
print(p1.l) #['a', 'b']
p1.l.append('c')
print(p1.__dict__) #{'name': 'alex'}
print(Chinese.l) # ['a', 'b', 'c']
将类中的方法加上@property修饰 # 跟实例绑定
class Room:
def __init__(self,name,owner,width,length):
self.name=name
self.owner=owner
self.width=width
self.length=length
@property #多加了这个将类中的方法变成静态属性,然后调用的时候就不用加括号了
def cal_area(self):
print('%s 住的 %s总面积是 %s' %(self.owner,self.name,self.width*self.length))
r1 = Room('皇宫','皇帝',100,100)
r1.cal_area
注意: @property 多加了这个将类中的方法变成静态属性,然后调用的时候就不用加括号了
@classmethod #跟类绑定
class Room:
tag = 1
def __init__(self,name,owner,width,length):
self.name=name
self.owner=owner
self.width=width
self.length=length
@classmethod
def tell_info(cls,x):
print(cls)
print('--->',cls.tag,x,)
#用实例的方法调用
r1 = Room('皇宫','皇帝',100,100)
r1.tell_info(10)
#直接用类来调用
Room.tell_info(1)
注意:加上 @classmethod 可以方便直接使用类方法调用的时候,调用的那个函数可以直接使用类的静态属性
staticmethod静态方法只是名义上的归属类管理,不能使用类变量和实例变量,是类的工具包
class Room:
tag = 1
def __init__(self,name,owner,width,length):
self.name=name
self.owner=owner
self.width=width
self.length=length
@property #多加了这个将类中的方法变成静态属性,然后调用的时候就不用加括号了
def cal_area(self):# 跟实例绑定
print('%s 住的 %s总面积是 %s' %(self.owner,self.name,self.width*self.length))
@classmethod #跟类绑定
def tell_info(cls,x):
print(cls)
print('--->',cls.tag,x,)
@staticmethod #类的工具包
def wash_body(a,b,c):
print('%s %s %s 正在洗澡'%(a,b,c))
#这个跟实例没有关系,只跟类有关系,实例是调用不了的,使用staticmethod就可以利用实例去调用了
def test(x,y):
print(x,y)
Room.wash_body('a','b','c')
Room.test(1,2)
r1 = Room('皇宫','皇帝',100,100)
# r1.test(1,2)#调用不了
r1.wash_body('a','b','c')#使用了类的工具包,所以实例出来的对象可以调用
注意:
r1 = Room('皇宫','皇帝',100,100)
# r1.test(1,2)#调用不了
r1.wash_body('a','b','c')#使用了类的工具包,所以实例出来的对象可以调用