python基础之简单的面向对象

类的继承:
class Record:
    __Occupation='Test'
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def getOccupation(self):
        return self.__Occupation
    def showrecode(self):
        print("Occupation:",self.getOccupation())

class GirlRecord(Record):#继承Record中的成员
    def showrecode(self):
        Record.showrecode(self)
        # super().showrecode()
        print("The girl:",self.name,"Age:",self.age)

myc=GirlRecord('Anna',20)
myc.showrecode()
      除了通过Record.showrecode(self)来实现类的继承调用外,还可以通过super函数来实现,可以这么写super().showrecode(),通过super函数进行调用的父类方法会保证只执行一次
class FemaleRecord(Record):
    def showrecode(self):
        print("female:",self.name,":",self.age)
        # Record.showrecode(self)
        super().showrecode()
class RetireRecord(Record):
    def showrecode(self):
        print("retire:",self.name,":",self.age)
        # Record.showrecode(self)
        super().showrecode()
class ThisRecord(FemaleRecord,RetireRecord):
    def showrecord(self):
        print("The member detail as follow")
        # FemaleRecord.showrecode(self)
        # RetireRecord.showrecode(self)
        super().showrecode()

myc=ThisRecord('Anna',20)
myc.showrecord()

静态接口,RESTful API

class ChainURL(object):
    def __init__(self,attr=''):
        self._data=attr
    def __getattr__(self, attr):
        return ChainURL('%s/%s'%(self._data,attr))
    def __str__(self):
        return self._data

print(ChainURL().status.allusers.list)#输出:/status/allusers/list

动态接口:方便根据需要改变地址值

class ChainURL(object):
    def __init__(self,attr=''):
        self._data=attr
    def __getattr__(self, attr):#代理函数(delegation)
        return ChainURL('%s/%s'%(self._data,attr))
    def __call__(self, attr):#重载call运算符,使其具有调用的属性
        return ChainURL('%s/%s'%(self._data,attr))
    def __str__(self):
        return self._data
    __repr__=__str__#用于转嫁调用__str__函数
for i in range(1,10):
    print(ChainURL().users(i).info)

异常:

一般通过try:pass    except Exception as e: print("打印异常",e)实现异常处理

try:
    pass
except Exception as e:
    print("打印异常",e)

class MyEpt(Exception):#从Exception中继承
    def __init__(self,value):
        self.value=value
    def __str__(self):
        return repr(self.value)
#通过raise来创建一个自定义类型的异常
try:
    raise MyEpt('some thing is wrong')
except MyEpt as e:
    print("My Exception:", str(e))

使用迭代器实现字符翻转:

x=[1,2,3]
it=iter(x)
print(it.__next__())
print(it.__next__())
print(it.__next__())
class Reverse:
    def __init__(self,datastr):
        self.__data=datastr
        self.__index=len(datastr)
        self.reversedata=''
    def __iter__(self):
        return self
    def __next__(self):
        if self.__index==0:
            raise (StopIteration)#抛出异常
        self.__index-=1
        # print("位置",self.__index)
        # print("数据",self.__data[self.__index])
        self.reversedata+=self.__data[self.__index]
        return self.__data[self.__index]
    def show(self):
        print("翻转后的字符串",self.reversedata)

revstr=Reverse('Python')
for c in revstr:
    pass
revstr.show()

类的另一种写法,不用class定义

def fun(self):
    print("Hello Python!")
#type中的3个参数,分别是类型MyClass,父类Object,成员函数myfun,通过把fun绑定到myfun上实现
#其中父类和成员函数可以有多个
cls=type('MyClass',(object,),dict(myfun=fun))
test=cls()
test.myfun()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值