python 中类的部分方法

定义一个类对象:

 class Student():
     count = 0
     def __init__(self,name):
         self.name = name
         Student.count += 1
     def say(self):
         print('你好我叫',self.name)
     @classmethod                                       
         print(cls.count)
         print(Student.count)
     @staticmethod
     def hanshu2():
         print('静态')

想定义类属性的话必须使用   @classmethod   方法

类方法的定义 第一个参数一定是类本身    可以理解为   hanshu(cls) == hanshu(Student)

而  @staticmethod 静态方法

类中的魔术方法:

 class Student():
     count = 0
     def __init__(self,name):
         self.name = name
         Student.count += 1
     def __del__(self,name):
         Student.count -= 1
         print('删除了',self.name,'还剩下',Student.count)
 zs = Student('张三')
 ls = Student('李四')
 ww = Student('王五')

del 魔术方法:如果不主动del删除那么垃圾回收机制自动删除

 class Student():
     count = 0
     def __init__(self,name,age):
         self.name = name
         self.age = age
         Student.count += 1
     def __str__(self):
         return '我叫%s今年%d岁'%(self.name,self.age) 
 zs = Student('张三',28)
 ls = Student('李四',13)
 sentence = str(zs)                                     
 print(sentence)
 print(hasattr(Student,'count'))                       
 print(hasattr(Student,'inst'))
 Student.inst = 100                                    
 print(hasattr(Student,'inst'))
 print(not hasattr(Student,'inst'))                    

str魔术方法:

return 给str 将格式带入str 然后把张三的参数填入格式

str函数启动了类中str的魔术方法

扫描二维码关注公众号,回复: 6164737 查看本文章

hasattr 函数用来判断 类内是否有 此参数 有则返回True 没有返回Flase

 class Car():
     def __new__(cls, *args, **kwargs):                  
         print('我是new方法')
         return object.__new__(cls)                      #生成一个新的对象
     def __init__(self,name):                            #初始化的时候调用
         print('我是init方法')
         self.name = name
 zs = Car('劳斯莱斯')

new方法为生成新的对象方法

如果init之前没有使用new方法那么默认调用new方法创建

 class A():
     def __init__(self,num):
         self.num = num
     def __eq__(self, other):
         return self.num == other.num
     def __add__(self, other):
         return self.num + other.num
 a = A(5)
 b = A(5)
 print(a == b)                                               
 print(a is b)                                               
 print(a+b)                                                  

eq和add魔术方法

在打印时 == 默认调用__eq__魔术方法进行属性比较判断 而直接==比较为比较地址

is 时在比较地址

在打印时 + 默认调用__add__魔术方法进行相加


 class A():
     def __new__(cls, *args, **kwargs):
         if not hasattr(A,'hehe'):
             cls.hehe = object.__new__(cls)                    #将类里添加一个hehe这个属性
         return cls.hehe
     def __init__(self,name):
         self.name = name
 z = A('张三')
 b = z                                                         #相当于给z换了个名字 但是z 还是那个z不过是改了名字
 print(b == z)                                                 #这时候比b与z的地址
 print(b is z)                                                 #b是z吗? b是z
 class Car():
     def __new__(cls, *args, **kwargs):                          #单例模式 魔术方法
         if not hasattr(Car,'hehe'):                            #如果类没有hehe属性多的话
             cls.hehe = object.__new__(cls)                      #那么生成一个对象给类属性hehe
         return cls.hehe                                         #类.self
     def __init__(self,name):                                    #hehe.name = a.naem = b....自始至终用的是一个对象因为if判断只成立一次

单例模式为多个对象自始至终使用一个对象

抽象方法:


 from abc import ABC,abstractclassmethod,abstractmethod,abstractstaticmethod
 def Man(ABC):
     @abstractstaticmethod
     def chouyan():
         #收钱
         pass
     @abstractmethod
     def hejiu():
         pass
     @abstractclassmethod
     def chuiniu():
         pass

示范时使用

注:本博客为学习中记录要点个人摘录及记录学习点滴,如有纰漏还请多多指教 :D

猜你喜欢

转载自blog.csdn.net/weixin_44689392/article/details/88647785
今日推荐