目录
1.面向对象中主要内容:
2.类
3.类中方法
(1)普通方法
(2)类方法
(3)静态方法
(4)魔术方法
(5)类方法 与 静态方法 的 异同
(6)普通方法 与 类方法和静态方法 的区别
1.面向对象中主要内容:
1.类
2.对象
3.属性
4.方法
2.类
# 类:class # 1.所累类名要求首字母大写,多个单词使用驼峰式命名法 # 2.所有类默认继承Object # 3.格式: # class 类名[(父类)]: # 属性:特征 # 方法:动作
定义类
class Students:
school = '清华大学'
# 使用类,创建对象
stu1 = Students()
stu2 = Students()
print(stu1) # <__main__.Students object at 0x0000015B3AD88358>
print(stu2) # <__main__.Students object at 0x0000015B3AD884A8>
print(stu1.school) # 清华大学
stu2.school = '北京大学'
print(stu1.school) # 清华大学
print(stu2.school) # 北京大学
# PS:对象的属性查找,会先在自己的空间中找,找不到的时候会再去模型中查找
3.类中方法
(1)普通方法
1.普通方法 def 方法名(self[,参数1,参数2...]): pass
class Students:
school = '清华大学'
# 魔术方法:__init__() 初始化属性
# 只要是实例化对象之后,就会执行该方法
def __init__(self):
print('我是魔术方法----')
self.type = '白开水'
# 普通方法
def eatlunch(self):
print('正在吃午饭----')
print(self)
def study(self,subject):
print('正在学习{}科目----'.format(subject))
print(self)
def drink(self):
print('正在喝:',self.type)
# 实例化
wanzi = Students()
wanzi.eatlunch()
# 正在吃午饭----
# <__main__.Students object at 0x0000015173C68940>
wanzi.study('英语')
# 正在学习英语科目----
# <__main__.Students object at 0x0000015173C68940>
xiaoxin = Students()
xiaoxin.eatlunch()
# 正在吃午饭----
# <__main__.Students object at 0x000002E6061D8940>
xiaoxin.age = 12
print(xiaoxin.age) # 12
wanzi.drink() # 正在喝: 白开水
xiaoxin.type = '奶茶'
xiaoxin.drink() # 正在喝: 奶茶
(2)类方法
2.类方法 普通方法通过对象来调用,而类方法通过类调用 特点: -1.定义需要依赖装饰器@classmethod -2.类方法中参数不是一个对象,而是类 print(cls) # <class '__main__.Dog'> -3.类方法中只可以使用类属性,不能使用对象属性
类方法作用: 由于只能访问类属性和类方法,所以在对象创建之前,如果需要完成一些功能,可以使用类方法
class Dog:
type = '狗'
def __init__(self,name,age,color):
self.name = name
self.age = age
self.color = color
def cat(self,food):
print('{}在吃{}'.format(self.name,food))
def watchhome(self,pname):
print('{}正在给它的主人{}看家'.format(self.name,pname))
def rest(self,hour):
if hour < 1:
print('再玩一会吧!')
else:
print('休息时间结束啦!')
# 普通方法中调用普通方法
self.watchhome('mico')
# 类方法
# 普通方法通过对象来调用,而类方法通过类调用
'''
特点:
1.定义需要依赖装饰器@classmethod
2.类方法中参数不是一个对象,而是类
print(cls) # <class '__main__.Dog'>
3.类方法中只可以使用类属性,不能使用对象属性
'''
@classmethod
def test(cls): # cla = class
print('cls:',cls)
print(cls.type)
# print(cls.name) # 报错:不能调用方法的属性,方法的属性是对象调用的
dog1 = Dog('冰狗',2,'红色')
dog1.watchhome('堂主')
dog1.rest(3)
# 冰狗正在给它的主人堂主看家
# 休息时间结束啦!
# 冰狗正在给它的主人mico看家
# 类方法不需要对象就可以调用
Dog.test()
# cls: <class '__main__.Dog'>
# 狗
(3)静态方法
3.静态方法 (1)需要使用装饰器@staticmethod (2)不需要传递参数(cls,self) (3)只能访问类的属性和方法,对象的是无法访问的 (4)加载时机同类方法
class Dog:
type = '狗'
def __init__(self,name,age,color):
self.name = name
self.age = age
self.color = color
# 静态方法
@staticmethod
def test2():
print('我是静态方法')
print(Dog.type)
Dog.test2()
# 我是静态方法
# 狗
(4)魔术方法
4.魔术方法:__名字__() 打印对象名的时候自动触发
__init__: 初始化魔术方法
触发时机:初始化对象时触发(不是实例化触发,但是和实例化在一个操作中)
__new__: 实例化的魔术方法【作用:开辟空间】
触发时机:实例化时候触发
__call__: 对象调用方法【作用:需要将对象当函数使用的时候】
触发时机:将对象当做函数使用的时候,会默认调用函数中的内容
__del__: 机构魔术方法【垃圾回收机制,一般不自己重写】
1.对象赋值
p=Demo()
p1=p
说明:p,p1指向同一个地址
2.删除地址的引用
del p1 # 删除p1对地址的引用
3.查看对地址的引用次数
import sys
sys.getrefcount(p)
4.当一块空间没有了任何引用,会默认执行__del__
__str__
触发机制:打印对象名,自动触发去调用__str__的内容
PS:一定要加上return 才能得到希望看到的内容
以上常用的有:__init__ __str__
class Students:
school = '清华大学'
# 魔术方法:__init__() 初始化属性
# 只要是实例化对象之后,就会执行该方法
def __init__(self):
print('我是魔术方法----')
self.type = '白开水'
class meal:
def __init__(self,x1,x2):
self.x1 = x1
self.x2 = x2
meal1 = meal('饼干','咖啡')
print(meal1.x1)
print(meal1.x2)
# 饼干
# 咖啡
class Demo:
p = 1
def __init__(self):
print('--init--')
def __new__(cls,*args,**kwargs):
print('--new--')
return object.__new__(cls,*args,**kwargs) # 系统的__new__,
# 不创建__new__的时候会默认调用系统的,由于这里自定义了__new__
# 而__init__会依赖于系统的__new__,否则一些功能实现不了
# 所以这里调用系统的
# 将对象作为函数调用的时候
def __call__(cls,*args,**kwargs):
print('--call--')
def __del__(self):
print('--del--')
def __str__(self):
return 'p:'+ str(self.p)
a = Demo()
# --new--
# --init--
a() # --call--
p=a.p
p1=p
p2=p1
print('p---1:',p) # p---1: 1
del p1 # --del--
# print('p---2',p1) # NameError: name 'p1' is not defined
# print('p---2',p2) # NameError: name 'p2' is not defined
import sys
n = sys.getrefcount(p)
print(n) # 110
# 单纯打印对象名称,是一个地址信息,无太大意义
# 魔术方法__str__可以产生一些对于开发有意义的信息
print(a) # p:1
(5)类方法 与 静态方法 的 异同
类方法 VS 静态方法: 不同点: [1]装饰器不同 [2]类方法是有参的,静态方法没有参数 相同点: [1]只能访问类的属性和方法,对象的是无法访问的 [2]都可以通过类名直接调用 [3]都可以在创建对象之前使用,因为其均不依赖于对象
(6)普通方法 与 类方法和静态方法 的区别
普通方法 与 类方法和静态方法 的区别: 不同点: [1]没有装饰器 [2]普通方法永远是要以来对象,因为每个普通方法都有一个self [3]只有创建了对象才可以调用普通方法,否则无法调用