Python 面向对象编程 总结

一、 一些基础:

1、如何创建类
    class 类名:
        pass
        
2、创建方法
    构造方法,__init__(self,arg)
        obj = 类('a1')
    普通方法
        obj = 类(‘xxx’)
        obj.普通方法名()
        
3、面向对象三大特性之一:封装

    class Bar:
        def __init__(self, n,a):
            self.name = n
            self.age = a
            self.xue = 'o'
            
    b1 = Bar('alex', 123)
    
    b2 = Bar('eric', 456)
    
    
4、适用场景:
        如果多个函数中有一些相同参数时,转换成面向对象

    class DataBaseHelper:
    
        def __init__(self, ip, port, username, pwd):
            self.ip = ip
            self.port = port
            self.username = username
            self.pwd = pwd
        
        def add(self,content):
            # 利用self中封装的用户名、密码等   链接数据
            print('content')
            # 关闭数据链接
        
        def delete(self,content):
            # 利用self中封装的用户名、密码等   链接数据
            print('content')
            # 关闭数据链接
        
        def update(self,content):
            # 利用self中封装的用户名、密码等   链接数据
            print('content')
            # 关闭数据链接
            
        def get(self,content):
            # 利用self中封装的用户名、密码等   链接数据
            print('content')
            # 关闭数据链接

s1 = DataBaseHelper('1.1.1.1',3306, 'alex', 'sb')

5、面向对象三大特性之二:继承

    1、继承
        
        class 父类:
            pass
            
        class 子类(父类):
            pass
            
    2、重写
        防止执行父类中的方法
        
    3、self永远是执行改方法的调用者

    4、
       super(子类, self).父类中的方法(...)
       父类名.父类中的方法(self,...)
       
       
       
    5、Python中支持多继承

        a. 左侧优先
        b. 一条道走到黑
        c. 同一个根时,根最后执行
    
6、面向对象三大特性之三:多态
    ====> 原生多态
    
    # Java
        string v = 'alex'
        
        def func(string arg):
            print(arg)
            
        func('alex')
        func(123)
    
    # Python 
        v = 'alex'
        
        def func(arg):
            print(arg)
            
            
        func(1)
        func('alex')

  

二、面向对象中高级编程

类成员:
# 字段
- 普通字段,保存在对象中,执行只能通过对象访问
- 静态字段,保存在类中, 执行 可以通过对象访问 也可以通过类访问

举例:

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

    def __init__(self, name):
        # 普通字段,属于对象
        self.name = name


# 方法
- 普通方法,保存在类中,由对象来调用,self=》对象
- 静态方法,保存在类中,由类直接调用
- 类方法,保存在类中,由类直接调用,cls=》当前类

class Foo:
    def __init__(self):
        self.name ='a'

    def bar(self):  # 普通方法
        # self是对象
        print('bar')

    @staticmethod
    def sta():      # 静态方法
        print('123')

    @staticmethod
    def stat(a1,a2):
        print(a1,a2)

    @classmethod
    def classmd(cls):   # 类方法
        # cls 是类名
        print(cls)
        print('classmd')


# 静态方法、类方法的调用
Foo.stat(1,2)
Foo.classmd()

# 普通方法的调用
obj = Foo()
obj.bar()
Foo.bar(obj)

# 应用场景:
如果对象中需要保存一些值,执行某功能时,需要使用对象中的值 -> 普通方法
不需要任何对象中的值,静态方法

# 属性,特性
- 不伦不类:调用时不带括号,但是是一个方法(函数)

class Foo:
    def __init__(self):
        self.name = 'a'
        # obj.name
        self.name_list = ['alex']

    # obj.bar()
    def bar(self):
        # self是对象
        print('bar')
    # 用于执行 obj.per
    @property
    def perr(self):

        return 123

    # obj.per = 123
    @perr.setter
    def perr(self, val):
        print(val)

    @perr.deleter
    def perr(self):

        print(666)

obj = Foo()
r = obj.perr   # return 123
print(r)

obj.perr = 123  # 调用的是@perr.setter 之后的方法

del obj.perr       # 调用的是perr.deleter 之后的方法

  

# property函数

property() 函数的作用是在新式类中返回属性值。

语法

以下是 property() 方法的语法:

class property([fget[, fset[, fdel[, doc]]]])

参数

  • fget -- 获取属性值的函数
  • fset -- 设置属性值的函数
  • fdel -- 删除属性值函数
  • doc -- 属性描述信息

定义一个可控属性值 x

class C(object):
    def __init__(self):
        self._x = None
 
    def getx(self):
        return self._x
 
    def setx(self, value):
        self._x = value
 
    def delx(self):
        del self._x
 
    x = property(getx, setx, delx, "I'm the 'x' property.")

解析:

如果 c 是 C 的实例化, c.x 将触发 getter,c.x = value 将触发 setter , del c.x 触发 deleter。

如果给定 doc 参数,其将成为这个属性值的 docstring,否则 property 函数就会复制 fget 函数的 docstring(如果有的话)。

将 property 函数用作装饰器可以很方便的创建只读属性:

class Parrot(object):
    def __init__(self):
        self._voltage = 100000
 
    @property
    def voltage(self):
        """Get the current voltage."""
        return self._voltage

## 上面的代码将 voltage() 方法转化成同名只读属性的 getter 方法。

  

property 的 getter,setter 和 deleter 方法同样可以用作装饰器:

class C(object):
    def __init__(self):
        self._x = None
 
    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x
 
    @x.setter
    def x(self, value):
        self._x = value
 
    @x.deleter
    def x(self):
        del self._x

  

原文地址:https://www.cnblogs.com/sddai/p/14206868.html