面向对象

语言的分类

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

Python进阶8---面向对象基础1 随笔 第1张

Python进阶8---面向对象基础1 随笔 第2张

Python进阶8---面向对象基础1 随笔 第3张

Python进阶8---面向对象基础1 随笔 第4张

Python进阶8---面向对象基础1 随笔 第5张

Python进阶8---面向对象基础1 随笔 第6张

Python进阶8---面向对象基础1 随笔 第7张

Python进阶8---面向对象基础1 随笔 第8张

 

Python的类

定义

class ClassName:
    pass

Python进阶8---面向对象基础1 随笔 第9张

class MyCalss:
    """A example class"""#文档字符串
    x = 'abc'#类属性
    
    def foo(self):#类属性foo,也是方法
        return 'myclass'

类对象及类属性

 Python进阶8---面向对象基础1 随笔 第10张

实例化

 

a = MyClass    #实例化

Python进阶8---面向对象基础1 随笔 第11张

__init__方法

Python进阶8---面向对象基础1 随笔 第12张

class MyCalss:

    def __init__(self):#初始化
        print('init')

a  = MyCalss()

Python进阶8---面向对象基础1 随笔 第13张

实例对象

 Python进阶8---面向对象基础1 随笔 第14张

Python进阶8---面向对象基础1 随笔 第15张

self

class Person:
    def __init__(self):
        print(id(self))

c = Person()    #会调用__init__
print('p={}'.format(id(c)))
#输出结果:
43079160
c=43079160

Python进阶8---面向对象基础1 随笔 第16张

 

实例变量和类变量

 Python进阶8---面向对象基础1 随笔 第17张

Python进阶8---面向对象基础1 随笔 第18张

class Person:
    age = 7
    height = 175
    def __init__(self,name,age=23):
        self.name = name
        self.age = age

tom  = Person('tom')
jerry = Person('jerry',20)

Person.age = 30
print(Person.age,tom.age,jerry.age)
print(Person.__dict__,tom.__dict__,jerry.__dict__,sep='\n')

Person.height += 5
print(Person.__dict__,tom.__dict__,jerry.__dict__,sep='\n')

tom.height = 176
print(Person.__dict__,tom.__dict__,jerry.__dict__,sep='\n')

Person.weight = 65
print(Person.__dict__['weight'])
print(tom.__dict__['weight'])#KeyError

Python进阶8---面向对象基础1 随笔 第19张

Python进阶8---面向对象基础1 随笔 第20张

Python进阶8---面向对象基础1 随笔 第21张

 

装饰一个类

 Python进阶8---面向对象基础1 随笔 第22张

#增加类变量
def add_name(name,cls):
    cls.NAME = name #动态增加类属性

#改进成装饰器(带参)
def add_name1(name):
    def wrapper(cls):
        cls.NAME = name
        return cls
    return wrapper

@add_name1('tom')
class Person:
    AGE = 1

print(Person.NAME)

Python进阶8---面向对象基础1 随笔 第23张

 

 

 类方法和静态方法

Python进阶8---面向对象基础1 随笔 第24张

普通函数

Python进阶8---面向对象基础1 随笔 第25张

Python进阶8---面向对象基础1 随笔 第26张

 

类方法

 Python进阶8---面向对象基础1 随笔 第27张

  Python进阶8---面向对象基础1 随笔 第28张

 Python进阶8---面向对象基础1 随笔 第29张

 

静态方法

Python进阶8---面向对象基础1 随笔 第30张

Python进阶8---面向对象基础1 随笔 第31张

 

方法的调用

 

class Person:
    def normal_method():
        print('nomal')

    def method(self):
        print("{}'s method ".format(self))

    @classmethod
    def class_method(cls):
        print('class = {0.__name__}  {0}'.format(cls))
        cls.HEIGHT  =175

    @staticmethod
    def static_method():
        print(Person.HEIGHT)

#~~~类访问~~~
print(1,Person.normal_method())
# print(2,Person.method())#报错
print(3,Person.class_method())
print(4,Person.static_method())
#~~~实例访问~~~
tom = Person()
# print(5,tom.normal_method())#报错
print(6,tom.method())
print(7,tom.class_method())
print(8,tom.static_method())

 Python进阶8---面向对象基础1 随笔 第32张

 

访问控制

私有属性Private

Python进阶8---面向对象基础1 随笔 第33张

Python进阶8---面向对象基础1 随笔 第34张

Python进阶8---面向对象基础1 随笔 第35张

Python进阶8---面向对象基础1 随笔 第36张

 Python进阶8---面向对象基础1 随笔 第37张

一般来说,可以在内部自定义一个方法来访问私有变量。

私有变量的本质

Python进阶8---面向对象基础1 随笔 第38张

Python进阶8---面向对象基础1 随笔 第39张

Python进阶8---面向对象基础1 随笔 第40张

 Python进阶8---面向对象基础1 随笔 第41张

Python进阶8---面向对象基础1 随笔 第42张

Python进阶8---面向对象基础1 随笔 第43张

Python进阶8---面向对象基础1 随笔 第44张

 

保护变量

 Python进阶8---面向对象基础1 随笔 第45张

Python进阶8---面向对象基础1 随笔 第46张

 Python进阶8---面向对象基础1 随笔 第47张

Python进阶8---面向对象基础1 随笔 第48张

 

 私有方法

Python进阶8---面向对象基础1 随笔 第49张

 Python进阶8---面向对象基础1 随笔 第50张

 Python进阶8---面向对象基础1 随笔 第51张

私有方法的本质

 Python进阶8---面向对象基础1 随笔 第52张

私有成员的总结

Python进阶8---面向对象基础1 随笔 第53张

Python进阶8---面向对象基础1 随笔 第54张

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄