12-5

类的内置方法

  • 类中定义类
class People(object):
    color='yellow'
    __age=26

    class Chinese(object):
        print("i am chinese")

People.Chinese()
[root@localhost studypy]# python3 12-5.py
i am chinese


class Chinese(object):
    name = "i am chinese"

print(jack.name)

[root@localhost studypy]# python3 12-5.py
i am chinese

也可以使用这种办法

ren = People()
jack=ren.Chinese()
print(jack.name)

或者

print(People.Chinese().name)
  • 类的魔术方法
  • 内置方法
ren = People()
print(ren)
                                         
[root@localhost studypy]# python3 12-5.py
<__main__.People object at 0x7f14ee9e9518>

  • __str__()方法
def __str__(self):
        return "this is class method"
        

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

  • __init__()方法和__del__()方法
def __init__(self):
        self.color = 'yellow'
        
print(ren.color)
print(People.color)

这两个是不一样的

def __init__(self,c='yellow'):
        self.color = c

输出值
[root@localhost studypy]# python3 12-5.py
green
blue

在init中加入self.think()

实例化的时候就会自动执行


init中打开一个文件

在del中关闭文件

def __init__(self,c='yellow'):
    self.color = c
    self.think()
    self.fd=open('12-4.py','r')

def __del__(self):
        print("del ...")
        self.fd.close()
        
[root@localhost studypy]# python3 12-5.py
i am a black
i am a thinker
26
black
blue
del ...

类的继承

  • 继承是面向对象的重要特性之一

  • 继承关系:继承是相对两个类而言的父子关系,子类继承了父类的所有共有属性和私有属性

  • 继承实现了代码重用

  • 继承

#!/bin/python

class People(object):
    color='blue'

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

class Chinese(People):
   pass
   
ch = Chinese()
print(ch.color)

[root@localhost studypy]# python 12-5-1.py 
blue

#!/bin/python

class People(object):
    color='blue'

    def __init__(self):
        self.dwell='earth'

    def think(self):
        print("i am a %s"%(self.color))
        print("i am a thinker")    
    
class Chinese(People):
   pass

ch = Chinese()
print(ch.color)   
print(ch.dwell)

[root@localhost studypy]# python 12-5-1.py 
blue
earth

初始化中带参数的方法

#!/bin/python

class People(object):
    color='blue'

    def __init__(self,c):
        self.dwell='earth'
        print(c)

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

class Chinese(People):
   def __init__(self):
       People.__init__(self,'red')

ch = Chinese()
print(ch.color)
print(ch.dwell)

[root@localhost studypy]# python 12-5-1.py 
red
blue
earth

也可以使用super的方法

super(Chinese,self).__init__('red')

多重继承

  • 简单实例
#!/bin/python

class People(object):
    color='yellow'

    def __init__(self):
        self.dwell='earth'

    def think(self):
        print("i am a %s"%(self.color))
        print("my home is %s"%(self.dwell))

class Martian(object):
    color = 'red'
    def __init__(self):
        self.dwell='Martian'

class Chinese(People,Martian):
     pass

ch = Chinese()
ch.think()

[root@localhost studypy]# python3  12-5-2.py
i am a yellow
my home is earth

  • 另一种方法
#!/bin/python

class People(object):
    color='yellow'

    def __init__(self):
        self.dwell='earth'

    def think(self):
        print("i am a %s"%(self.color))
        print("my home is %s"%(self.dwell))

class Martian(object):
    color = 'red'
    def __init__(self):
        self.dwell='Martian'

class Chinese(Martian,People):
     pass

ch = Chinese()
ch.think()

[root@localhost studypy]# python3  12-5-2.py
i am a red
my home is Martian

  • 执行自己的初始化方法

#!/bin/python

class People(object):

    def __init__(self):
        self.dwell='earth'
        self.color='yellow'

    def think(self):
        print("i am a %s"%(self.color))
        print("my home is %s"%(self.dwell))    
    
class Martian(object):
    color = 'red'
    def __init__(self):
        self.dwell='Martian'

class Chinese(Martian,People):
     def __init__(self):
         People.__init__(self)

ch = Chinese()
ch.think()

[root@localhost studypy]# python3  12-5-2.py
i am a yellow
my home is earth

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值