| 名称 | 描述 |
|---|
| 特殊属性 | dict | 获得类对象或实例对象所绑定的所有属性和方法的字典 |
| 特殊方法 | len() | 通过重写_len_()方法,让内置函数len()的参数可以是自定义的类型 |
| 特殊方法 | add() | 通过重写_add_()方式,,可使自定义对象有‘+’的功能 |
| 特殊方法 | new() | 用于创建对象 |
| 特殊方法 | init() | 对创建的对象进行初始化 |
特殊属性
class A:
pass
class B:
pass
class C(A,B):
def _init_(self,name,age):
self.name=name
self.age=age
x=C('Jack',20)
print(x.__dict__)
print(C.__dict__)
print(c.__class__)
print(C.__base__)
print(C.__mro__)
print(A.__subclasses__)
特殊方法
a=20
b=100
c=a+b
d=a.__add__(b)
print(c)
print(d)
class Student:
def __init__(self,name):
self.name=name
def __add__(self,other):
return self.name+other.name
def __len__(self):
return len(self.name)
stu1=Student('张三')
stu2=Student('李四')
s=stu1+stu2
print(s)
lst=[11,22,33,44]
print(len(lst))
print(lst.__len__())
print(len(stu1))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
__new__与__init__创建对象过程
