python(面向对象-初级篇)

1.1. 方法分类

1)普通方法(实例函数) 保存在类中,由对象来调用,类不能直接调用,self=》对象  

2)静态方法(类函数) 保存在类中,由类直接调用,不需要传递参数  @staticmethod

3)类方法(类函数),保存在类中,由类直接调用,默认参数 cls=》当前类 @classmethod

4)特殊方法 

区别

1、静态方法和类方法都不可以访问实例变量

2、类方法可以访问类变量,静态方面不能访问类变量

3、类函数(静态方法、类方法)并不一定需要实例化对象,类可以直接调用

3、静态方法中实例对象共享一块内存地址

示例

1.1.1. 构造方法(待深入)

__new__() 方法是在类准备将自身实例化时调用

__new__() 方法始终都是类的静态方法,即使没有被加上静态方法装饰器

Python 首先调用 __new__() 方法,在 __init__() 启动之前

class Foo():

    def __init__(self, name,age):

        self.n = name

        self.a = age

        print('%s-%s'%(self.n,self.a))

    def __new__(cls, name,age):

        print('this is new')

        return object.__new__(cls)

Foo('zk','18')

1.1.2. 初始化

def __init__(self,arg)

    obj = 类('a1')

1     普通方法

def show(self):

print(self.name)

1.1.3. 静态方法

class X:

@ staticmethod

    def X1(n):

        name1 = n

        print(name1)

X.X1('zk')   #自动执行类中的X1方法

1.1.4. 类方法(类函数)

class X:

@classmethod

    def X1(cls,n):

        name1 = n

        print(name1)

X.X1('zk')   #自动执行类中的X1方法,不需要创建对象

1.1.5. 特殊方法

1、__init__  初始化,类()自动执行

2、__call__   #对象()  类()() 自动执行

class Foo:

    def __call__(self, *args, **kwargs):

        print("call")

Foo()()   #自动执行call里面的方法

3、__int__   int(对象)

class Foo:

    def __init__(self,n,a):

        self.n = n

        self.a = a

    def __int__(self):

        return self.n+self.a

obj = Foo(12,13)

print(obj, type(obj))

ret = int(obj)

print(type(ret))

print(ret)

4、  __str__  str() 

class Foo:

    def __init__(self,n,a):

        self.n = n

        self.a = a

    def __str__(self):

        return '%s-%s' %(self.n,self.a,)

obj = Foo(12,13)

print(obj)  #print(str(obj)) str(obj)   obj中__str__,并获取其返回值

5、__add__

 

class Foo:

 

    def __init__(self,n,a):

 

        self.n = n

 

        self.a = a

 

    def __add__(self,other):

 

        #obj1 = self

 

        #obj2 = other

 

        return (self.n+other.n,self.a+other.a)

 

obj1 = Foo(12,13)

 

obj2 = Foo(1,2)

 

r = obj1+obj2

 

print(r) 

6、  __dict__  将对象中封装的所有内容通过字典的形式返回

class Foo:

    v = 'zg'

    def __init__(self,n,a):

        self.n = n

        self.a = a

    def __add__(self,other):

        obj1 = self

        obj2 = other

        return (self.n+other.n,self.a+other.a)

print(Foo.__dict__)

7、__getitem__,__setitem__,__delitem__  切片(slice类型)或者索引

8、__iter__

# 如果类中有 __iter__ 方法,对象=》可迭代对象

# 对象.__iter__() 的返回值: 迭代器

# for 循环,迭代器,next

# for 循环,可迭代对象,对象.__iter__(),迭代器,next

# 1、执行li对象的类F类中的 __iter__方法,并获取其返回值

 # 2、循环上一步中返回的对象

class Foo:

    def __init__(self):

        self.r = [1,2,3,4]

    def __iter__(self):

        return iter(self.r)

obj = Foo()

for i in obj:

print(i)

# print(BaseReuqest.__dict__)   #包含该类的命名空间的字典

# print(BaseReuqest.__doc__)  #类文档字符串或无,如果未定义

# print(RequestHandler.serve_forever.__name__)  #类名或函数名

# print(RequestHandler.__module__)  #定义类的模块名称。此属性在交互模式下的值为“main”

# print(Son.__bases__)  #一个包含基类的空元组,按照它们在基类列表中出现的顺序

# print(RequestHandler.__mro__)

1.1.6. Metaclass  元类

class myType(type):

    def __init__(self,*args,**kwargs):

        print('myType')

    def __call__(self, *args, **kwargs):

        obj1 = self.__new__(self,*args,**kwargs)

        self.__init__(obj1)

class Foo(object,metaclass = myType):

    def __init__(self):

        self.r = [1,2,3,4]

        print(self.r)

    def __new__(cls,*args, **kwargs):

        return cls

    def func(self):

        print("func")

obj = Foo()

1.2. 属性

class X:

@property

    def X3(self):

        return 123

R = X()

obj = R.X3     #对象调用类方法的时候不需要加()

print(obj)

1.2.1. 属性应用

1.3. 字段(变量)

普通字段(实例变量),保存在对象中,执行只能通过对象访问

静态字段(类变量),保存在类中,  执行可以通过对象访问也可以通过类访问

class Province:

    country =  '中国'       # 静态字段,属于类

    def __init__(self, name):

        self.name = name       # 普通字段,属于对象

 

1.4. 成员修饰符

    共有成员

    私有成员, __字段名

        - 无法直接访问,只能间接访问

1.4.1. 私有普通字段

class Foo:

    def __init__(self, name, age):

        self.name = name

        # self.age = age

        self.__age = age # 私有,外部无法直接访问

    def show(self):

        return self.__age

obj = Foo('alex', 19)

# print(obj.__age)   #报错

print(obj.show())

1.4.2. 私有静态字段

class Foo:

    __v = 'zg'

    @staticmethod

    def show():

        print(Foo.__v)

# print(Foo.__v)  #报错

Foo.show()

1.4.3. 私有方法

class Foo:

    def __show(self):

        return 123

    def showall(self):

        r =  self.__show()

        return r

obj = Foo()

# print(obj.__show)

print(obj.showall())

1.5. 多态

class A:

    def func1(self):

        print('thin is func1')

def func2(var):

    pass

a = A()

func2(a.func1()) 

https://www.cnblogs.com/wupeiqi/p/4493506.html

猜你喜欢

转载自www.cnblogs.com/arctis-zk/p/10416711.html
今日推荐