12-4

面向对象介绍

  • 面向过程和面向对象编程
  • 面向过程编程:函数式编程,C程序等
  • 面向对象编程:c++,java,python等
  • 类和对象:是面向对像中的两个重要概念
  • 类:是对事物的抽象,比如:人类,球类
  • 对象:是类的一个实例

python类定义

  • 类定义:
    类把需要的变量和函数组合成一起,这种包含称之为“封装”
    class A(object):

  • 类的结构:
    class类名:
    成员变量-属性
    成员函数-方法

  • 定义类的例子

#!/bin/python

class People(object):
    color='yellow'


ren=People()
print(ren)

[root@localhost studypy]# python3 !$
python3 12-4.py
<__main__.People object at 0x7fa3b95fd198>


访问类中的属性

#!/bin/python

class People(object):
    color='yellow'


ren=People()
print(ren)
print(ren.color)

[root@localhost studypy]# python3 12-4.py
<__main__.People object at 0x7fb6cdbba198>
yellow

添加一个方法
#!/bin/python

class People(object):
    color='yellow'
    def think(self):
        print("i am a thinker")

ren=People()
print(ren)
print(ren.color)
ren.think()

[root@localhost studypy]# python3 12-4.py
<__main__.People object at 0x7f81e45f3208>
yellow
i am a thinker

## 类的属性

  • 属性:

尽量把需要用户传入的属性作为实例属性,而把同类都一样的属性作为类属性。实例属性在每创造一个实例时都会初始化一遍,不同的实例的实例属性可能不同,不同实例的类属性都相同。从而减少内存。

  • 实例属性:
    最好在__init__(self,…)中初始化
    内部调用时都需要加上self.
    外部调用时用instancename.propertyname
  • 类属性:
    在__init__()外初始化
    在内部用classname.类属性名调用
    外部既可以用classname.类属性名又可以用instancename.类属性名来调用
  • 私有属性:
    1):单下划线_开头:只是告诉别人这是私有属性,外部依然可以访问更改
    2):双下划线__开头:外部不可通过instancename.propertyname来访问或者更改实际将其转化为了_classname__propertyname
#!/bin/python

class People(object):
    color='yellow'
    __age=26

    def think(self):
        self.color ="black"
        print("i am a %s"%(self.color))
        print("i am a thinker")
        print(self.__age)

ren=People()
print(ren)
print(ren.color)
ren.think()
print(ren.__dict__)

[root@localhost studypy]# python3 12-4.py
<__main__.People object at 0x7fbcf0b112b0>
yellow
i am a black
i am a thinker
26
{'color': 'black'}

类的方法

  • 方法的定义和函数一样,但是需要使用self作为第一个参数
  • 类的方法为
    1.共有方法
    2.私有方法
    3.类方法
    4.静态方法
  • 共有方法
#!/bin/python

class People(object):
    color='yellow'
    __age=26

    def think(self):
        self.color ="black"
        print("i am a %s"%(self.color))
        print("i am a thinker")
        print(self.__age)

    def test(self):
        self.think()

jake=People()
jake.test()

[root@localhost studypy]# python3 12-4.py
i am a black
i am a thinker
26

  • 私有方法
    def __talk(self):
        print("i am talking with tom")

[root@localhost studypy]# python3 12-4.py
i am talking with tom

  • 类方法
def test(self):
    print("testing ...")
cm= classmethod(test)

[root@localhost studypy]# python3 12-4.py
testing ....


@classmethod
def test(self):
    print("this is class method")

[root@localhost studypy]# python3 12-4.py
this is class method


  • 静态方法
sm=staticmethod(test)
[root@localhost studypy]# python3 12-4.py
this is func


@staticmethod
def test1():
    print("this is static method") 

[root@localhost studypy]# python3 12-4.py
this is static method

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值