python面向对象操作3(速通版)

 

目录

一、多态和类名

1.标准多态

2.实例属性和实例方法

3.类对象和类属性

4.对象保存

二、方法

1.类方法

3.四种方法的区别

三、模块

1.导入模块

2.自动模块导入

3.模块导入的几种形式

3.1模块导入的两种方式和别名

3.2 from 模块 import 成员

4.两种方法的区别和问题

7.包和模块

8.__init__和__all__属性的特性

9.小结

四、异常处理

1.基本异常处理

2.异常的传递

3.捕捉多个异常

4.else和finally

5.自定义异常抛出

结语


一、多态和类名

多种形态

程序中的意义:

当调用一个方法名的时候,得到的结果不同

在一般面向对象语言中,多态是由继承来实现的

但是在python中,python天生具有多态

1.标准多态

class Father(object):
    def cure(self):
        print("中医")

class Son(Father):
    def cure(self):
        print("中西医结合")

class Person(object):
    #需要一个大夫治病
    def need_docter(self,doctor):
        doctor.cure()

p = Person()

p.need_docter(Father())
p.need_docter(Son())

2.实例属性和实例方法

class Cat(object):
    def __init__(self,name):
        self.name = name

    def info(self):
        print(self.name)
    def show(self):
        print("show")
        self.info()

tom = Cat('tom')
#使用实例属性
print(tom.name)
#实例方法
tom.info()
tom.show()

实例方法

class Cat(object):
    def __init__(self,name):
        self.name = name

    def info(self):
        print(self.name)
    def show(self):
        print("show")
        self.info()

tom = Cat('tom')
#使用实例属性
print(tom.name)
#实例方法
tom.info()
tom.show()
Cat.show(tom)

#类名不能调用对象属性
print(Cat.name)

3.类对象和类属性

4.对象保存

class Cat(object):
    def __init__(self,name):
        self.name = name
        self.__age = 1

    def public_meth(self):
        print("公有对象方法")

    def __priv_meth(self):
        print("私有对象方法")

tom = Cat('Tom')

jack = Cat('Jack')

print(Cat.__dict__)
print(tom.__dict__)
print(jack.__dict__)

tom.public_meth()

Cat.public_meth(tom)


二、方法

1.类方法

定义格式:和写普通方法没什么区别,就是有个修饰

调用格式

应用场景

注意:类方法中不能使用self,但是可以使用其他对象填充cls,当前类对象,这个参数也是自动传递的

class MyMath(object):
    #定义一个类属性
    n = 999
    @classmethod
    #cls默认是这个类对象
    def sum(cls,*args):
        m = 0
        for i in args:
            m += i
        return m
sum1 = MyMath.sum(1,2,3,4,5)
print(sum1)

2.静态方法

格式:

@staticmethod

def 方法名(参数列表。。。):

        pass

调用方式:

        同类方法

        类对象.静态方法名()

设计目的:

静态方法实际上就是放到类中的一堆普通函数

作用:

        静态方法同类方法相似,也是在不需要实例对象产生的条件下,可以使用静态方法来实现

        一般这两种方法,都是用在工具类的实现上

class EncodeUtil(object):

    @staticmethod
    def encode_data(data,format):
        print(f'对数据{data} 使用 {format} 格式进行编码')

    @staticmethod
    def decode_data(data,format):
        print(f'对数据{data} 使用 {format} 格式进行解码')


EncodeUtil.encode_data("hello",'utf-8')

EncodeUtil.decode_data("hello", 'GBK')

3.四种方法的区别

# 实例方法,必须通过实例对象调用执行,(第一个参数是当前调用该方法的实例)

# 类方法:当不需要产生实例对象时,可以使用类方法来实现显影的代码功能,类方法可以直接使用类对象来调用,类方法的第一个参数时cls,用来接收当前类对象,通过各个参数,可以在各个类方法中进行共享数据

# 静态方法:作用同类方法相似,但是静态方法不接收任何默认参数(实例对象或类对象),静态方法其实就是将一些相关联的普通方法进行类的整合

类方法和静态方法大多数用来实现工具类


三、模块

在python中,一个py文件就是一个模块

模块也是对象

1.导入模块

import os

import time

import random

import sys

import functools

import os
import time
import sys

print(os.getcwd())

#阻塞函数
time.sleep(3)

print(sys.argv)

2.自动模块导入

n = 1

def show():
    print("mode show")

class Cat(object):
    def show(self):
        print("Cat_show")

print('mode ',n)
show()
c = Cat()
c.show()

我们导入到main里面

#导入模块时,需要去创建该模块的对象,也就会去执行该模块文件中的所有代码

import mode

3.模块导入的几种形式

3.1模块导入的两种方式和别名

 

#导入模块时,需要去创建该模块的对象,也就会去执行该模块文件中的所有代码
import mode

#模块名调用
print("___________________")
print(mode)
mode.show()
print("____________________")

#类对象模块名调用
rom = mode.Cat()
rom.show()

print("____________________")

#别名
import mode as m

m.show()

m.Cat().show()

3.2 from 模块 import 成员



from mode import n
print("____________________________________________")
print(n)

from mode import show
print("____________________________________________")
show()
#起别名
from mode import Cat as C
print("____________________________________________")
C().show()


#导出所有
from mode import *
print("____________________________________________")
Cat().show()
show()
print(n)

4.两种方法的区别和问题

main导入

#import没有限制
import mode as m
print(m.x)
print(m._y)
print(m.__z)

用from import导入

#import没有限制
from mode import *
print(x)
print(_y)
print(__z)

可以通过__all__来解决这个问题

但是我们不提倡用这个方法

#改变规则来让私有属性能被引用
__all__ = ['x','_y','__z']

x = 1   #全局变量,模块间的公有变量
_y = 2  #私有变量,文件内私有变量
__z = 3 #私有变量,对象私有或类私有,不会直接在模块中定义

main


#import没有限制
from mode import *
print(x)
print(_y)
print(__z)

5.模块导入顺序

6.__name__属性

 mode模块

n = 1
def show():
    print('show')

class Cat(object):
    def show(self):
        print("Cat_show")

#当使用__name__属性在获取当前模块名时,会有两种效果
print('name: ',__name__)

main


import mode

#这里运行是代表人口
print('name2: ',__name__)

前面我们载入模块会自动执行一些调用的方法,我们可以用__name__来解决这个问题

n = 1
def show():
    print('show')

class Cat(object):
    def show(self):
        print("Cat_show")

#当使用__name__属性在获取当前模块名时,会有两种效果
print('name: ',__name__)

if __name__ == '__main__':
    print(n)
    show()
    Cat().show()

import mode

#这里运行是代表人口
print('name2: ',__name__)

 

7.包和模块

导入包中的模块

调用

import normal.a

print(normal.a.m)

from import

from normal import a

print(a.m)

8.__init__和__all__属性的特性

目录结构

cn.ou.web.*

当在使用import方式,或from-import方式导入包时,需要在__init__.py文件中,明确的指出可以被导入的模块有哪些。

使用from . import 模块名形式指定

如果在该文件中没有指定可以调入的模块时,默认不导入任何模块

from cn.ou import web
print(web.a.m)
print(web.b.c)

from cn.ou import web
print(web.a.m)
print(web.b.c)

__all__导入方式

__all__ = ['a','b']  #导入a,b模块

#__all__ = ['a']

from import才可以导入__all__

from cn.ou.web import *
print(a.m)
print(b.c)

9.小结


四、异常处理

1.基本异常处理

常见异常

这些错误都继承于Exception

index函数报错

s = "hello"

print(s.index('O'))

print('over')

这个函数错误不会返回null,而是直接抛出一个异常,中断程序

那么我们捕获这个异常让他继续运行

s = "hello"

try:
    print(s.index('O'))
except ValueError:
    print("查找的字串不存在")

因为异常原因可能不知道,所以可以用Exception来接收这个异常,来抛出

s = "hello"

try:
    print(s.index('O'))
except Exception:
    print("查找的字串不存在")

2.异常的传递

当代码出现了很多层级的调用时,在其中发生了异常,如果没有处理这个异常,那么这个异常会自动向上抛出,如果上层也没有处理,就会继续向上抛出,知道抛到解释器。

解释器默认处理异常的方式就是中断程序,将异常信息显示到工作台上

def func_a():
    print("Func a run...")
    func_b()

def func_b():
    print("Func b run...")
    func_c()

def func_c():
    print("Func c run...")
    print(1/0)

func_a()

处理掉这个异常

def func_a():
    print("Func a run...")
    func_b()

def func_b():
    print("Func b run...")
    func_c()

def func_c():
    print("Func c run...")
    try:
        print(1/0)
    except Exception:
        print('你的除数为0了')

func_a()

3.捕捉多个异常

def func_a():
    print("Func a run...")
    func_b()

def func_b():
    print("Func b run...")
    func_c()

def func_c():
    print("Func c run...")
    try:
        print(1/0)
    except (ZeroDivisionError,NameError) as e:
        print('出现异常了',e)

func_a()

4.else和finally

打开文件问题

try:
    f = open('a.txt','r')
except Exception as e:
    print("要打开的文件不存在")
    print(e)

else

无文件报异常

try:
    f = open('a.txt','r')
except Exception as e:
    print("要打开的文件不存在")
    print(e)
else:
    print(f.read())

有文件直接读取输出

try:
    f = open('a.txt','r')
except Exception as e:
    print("要打开的文件不存在")
    print(e)
else:
    print(f.read())
    f.close()

finally无论是否出现异常,都会执行finally语句块

try:
    f = open('a.txt','r')
except Exception as e:
    print("要打开的文件不存在")
    print(e)
else:
    print(f.read())
finally:
    f.close()

a.txt不存在时

这下又有异常了

这个问题处理

f = None
try:
    f = open('a.txt','r')
except Exception as e:
    print("要打开的文件不存在")
    print(e)
else:
    print(f.read())
finally:
    if f != None:
        f.close()

5.自定义异常抛出

raise 异常对象

class PhoneNumberNotDigitError(Exception):
    def __init__(self,msg):
        self.__msg = msg
    def __str__(self):
        return self.__msg

class PhoneNumberLengthError(Exception):
    def __init__(self,msg):
        self.__msg = msg
    def __str__(self):
        return self.__msg

def get__phone_number():
    pn = input("请输入一个11位的手机号:")
    if pn.isdigit() == False:
        raise PhoneNumberNotDigitError("请输入正确的手机号码")
    elif len(pn) != 11:
        raise PhoneNumberLengthError('手机号位数不正确')

    print("输入的手机号是合法的: ",pn)

get__phone_number()

输入a1234567890

输入正常格式的手机号

输入长度超过的数字

from import引用


class PhoneNumberNotDigitError(Exception):
    def __init__(self,msg):
        self.__msg = msg
    def __str__(self):
        return self.__msg

class PhoneNumberLengthError(Exception):
    def __init__(self,msg):
        self.__msg = msg
    def __str__(self):
        return self.__msg

def get__phone_number():
    pn = input("请输入一个11位的手机号:")
    if pn.isdigit() == False:
        raise PhoneNumberNotDigitError("请输入正确的手机号码")
    elif len(pn) != 11:
        raise PhoneNumberLengthError('手机号位数不正确')

    return pn

from mode import *

try:
    num = get__phone_number()
except(PhoneNumberLengthError,PhoneNumberNotDigitError) as e:
    print(e)
else:
    print(num)


结语

双休了!光速更新!!!求点赞

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白帽小丑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值