Python基础-属性和方法

it2025-05-04  24

1. 属性和方法 

# 定义一个类 class A(object): # 类属性 # 实例属性 # 类方法 # 实例方法 # 静态方法 # 类属性,直接在类中定义的属性是类属性 # 类属性可以通过类或类的实例访问到 # 但是类属性只能通过类对象来修改,无法通过实例对象修改 count = 0 def __init__(self): # 实例属性,通过实例对象添加的属性属于实例属性 # 实例属性只能通过实例对象来访问和修改,类对象无法访问修改 self.name = '孙悟空' # 实例方法 # 在类中定义,以self为第一个参数的方法都是实例方法 # 实例方法在调用时,Python会将调用对象作为self传入 # 实例方法可以通过实例和类去调用 # 当通过实例调用时,会自动将当前调用对象作为self传入 # 当通过类调用时,不会自动传递self,此时我们必须手动传递self def test(self): print('这是test方法~~~ ' , self) # 类方法 # 在类内部使用 @classmethod 来修饰的方法属于类方法 # 类方法的第一个参数是cls,也会被自动传递,cls就是当前的类对象 # 类方法和实例方法的区别,实例方法的第一个参数是self,而类方法的第一个参数是cls # 类方法可以通过类去调用,也可以通过实例调用,没有区别 @classmethod def test_2(cls): print('这是test_2方法,他是一个类方法~~~ ',cls) print(cls.count) # 静态方法 # 在类中使用 @staticmethod 来修饰的方法属于静态方法 # 静态方法不需要指定任何的默认参数,静态方法可以通过类和实例去调用 # 静态方法,基本上是一个和当前类无关的方法,它只是一个保存到当前类中的函数 # 静态方法一般都是一些工具方法,和当前类无关 @staticmethod def test_3(): print('test_3执行了~~~') a = A() # 实例属性,通过实例对象添加的属性属于实例属性 # a.count = 10 # A.count = 100 # print('A ,',A.count) # print('a ,',a.count) # print('A ,',A.name) # print('a ,',a.name) # a.test() 等价于 A.test(a) # A.test_2() 等价于 a.test_2() A.test_3() a.test_3()

2. 特殊方法 

# 特殊方法,也称为魔术方法 # 特殊方法都是使用__开头和结尾的 # 特殊方法一般不需要我们手动调用,需要在一些特殊情况下自动执行 # 定义一个Person类 class Person(object): """人类""" def __init__(self, name , age): self.name = name self.age = age # __str__()这个特殊方法会在尝试将对象转换为字符串的时候调用 # 它的作用可以用来指定对象转换为字符串的结果 (print函数) def __str__(self): return 'Person [name=%s , age=%d]'%(self.name,self.age) # __repr__()这个特殊方法会在对当前对象使用repr()函数时调用 # 它的作用是指定对象在 ‘交互模式’中直接输出的效果 def __repr__(self): return 'Hello' # object.__add__(self, other) # object.__sub__(self, other) # object.__mul__(self, other) # object.__matmul__(self, other) # object.__truediv__(self, other) # object.__floordiv__(self, other) # object.__mod__(self, other) # object.__divmod__(self, other) # object.__pow__(self, other[, modulo]) # object.__lshift__(self, other) # object.__rshift__(self, other) # object.__and__(self, other) # object.__xor__(self, other) # object.__or__(self, other) # object.__lt__(self, other) 小于 < # object.__le__(self, other) 小于等于 <= # object.__eq__(self, other) 等于 == # object.__ne__(self, other) 不等于 != # object.__gt__(self, other) 大于 > # object.__ge__(self, other) 大于等于 >= # __len__()获取对象的长度 # object.__bool__(self) # 可以通过bool来指定对象转换为布尔值的情况 def __bool__(self): return self.age > 17 # __gt__会在对象做大于比较的时候调用,该方法的返回值将会作为比较的结果 # 他需要两个参数,一个self表示当前对象,other表示和当前对象比较的对象 # self > other def __gt__(self , other): return self.age > other.age # 创建两个Person类的实例 p1 = Person('孙悟空',18) p2 = Person('猪八戒',28) # 打印p1 # 当我们打印一个对象时,实际上打印的是对象的中特殊方法 __str__()的返回值 # print(p1) # <__main__.Person object at 0x04E95090> # print(p1) # print(p2) # print(repr(p1)) # t = 1,2,3 # print(t) # (1, 2, 3) # print(p1 > p2) # print(p2 > p1) # print(bool(p1)) # if p1 : # print(p1.name,'已经成年了') # else : # print(p1.name,'还未成年了')

 

最新回复(0)