栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python面向对象

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python面向对象

我们先写一个类,同时创建对象

class ClassName:
    def __init__(self,a,b,c):
        print('动物类!')
        print(a)
        print(b)
        print(c)
first=ClassName('麻雀','狮子','老虎')

这是一个叫ClassName的类,它里面只有一个构造函数,__init__就是构造函数,在创建对象的时候由编译器自行调用。

我们再介绍它的成员函数或者叫方法

class Geese:
    def __init__(self,beak,wing,claw):
        print('我是大雁类!我有以下特征:')
        print(beak)
        print(wing)
        print(claw)
    def fly(self,state='111111'):#一样可以设置默认参数
        print(state)

brak_1='大'
brak_2='翅膀大'
claw='有爪子'
wildGoose=Geese(brak_1,brak_2,claw)
wildGoose.fly('666666')
wildGoose.fly()
#__init__差不多相当于构造函数,实例化对象就调用

这里除了提供了一个构造函数,还提供了一个类方法fly,值得注意的是self相当于Java或者C++的this,表示当前类的对象,可以调用当前类中的属性和方法。同时我们也可以像普通函数一个写作默认参数,示例都写好了。

创建类的数据成员并访问

class Geese:
    neck='脖子长'
    wing='翅膀大'
    leg='腿位于身体的中心'
    def __init__(self):
        print('大雁类有以下特征')
        print(Geese.neck)
        print(Geese.wing)
        print(Geese.leg)

geese=Geese()#创建对象
geese.neck='666'

在默认情况下,访问权限是public,即可以直接外部访问

通过类属性统计类实例化个数

class Geese:
    neck='脖子长'
    wing='翅膀大'
    leg='腿位于身体的中心'
    number=0
    def __init__(self):
        Geese.number+=1
        print('我是第'+str(Geese.number)+'个大雁,我有以下特征:')
        print(Geese.neck)
        print(Geese.wing)
        print(Geese.leg)
        print()

list=[]
for i in range(4):#调用四次构造实例化对象放入list列表内
    list.append(Geese())

print(Geese.number)
print('第二只大雁的翅膀',list[1].wing)#可以直接通过列表访问

通过这个例子我们可以看出来,number的值会一直增加,而不是像C++等一样,数据完全不共享,构造函数改变了成员数据number后,会一直记录着

我们再举一个例子,实例属性

class Geese:
    def __init__(self):
        neck='111'
        wing='222'
        leg='333'
        print('输出')
        print(neck)
        print(wing)
        print(leg)

geese=Geese()
#也可以直接在外部修改
geese.neck='100'
print(geese.neck)
class Geese:
    def __init__(self):
        self.neck='111'
        self.wing='222'
        self.leg='333'
        print('输出')
        print(self.neck)
        print(self.wing)
        print(self.leg)

geese=Geese()
#也可以直接在外部修改
geese.neck='100'
print(geese.neck)

这两段代码的结果没有任何不同,希望能意识到self的作用,就只是给我们看而已

限制类private私有访问权限

class Swan:#在Python中双下划线表示private私有属性
    __neck_swan='天鹅脖子很长'
    def __init__(self):
        print(Swan.__neck_swan)

swan=Swan()
print(swan._Swan__neck_swan)#访问私有名,得加上类名,在类名前面还得加上下划线,然后两个下划线表示私有,然后具体成员

Python的私有属性是可以被外部直接改变的,只需要满足注释上面的写法

为属性添加安全保护

class TVshow:
    def __init__(self,show):
        self.__show=show
    @property
    def show(self):
        return self.__show
    def demo(self):
        return self._a

tvshow=TVshow('正在播放《战狼2》')
print(tvshow.show)#show的属性是只读的
#tvshow.show='55'   #会抛出异常
tvshow.demo='55'   #方法被转化为属性,直接赋值,上面的无法赋是因为有歧义
print(tvshow.demo)

以单下划线开头的表示protected(保护)类型的成员,只允许类本身和子类进行访问,所以是允许外部类对象进行修改。就不再演示了

创建用于计算的类

class Rect:
    def __init__(self,width,height):
        self.width=width
        self.height=height
    @property       #将方法转换为属性
    def area(self):
        return self.height * self.width

rect=Rect(100,10)
print('面积为:',rect.area)

继承的基本语法

class Fruit:  # 定义水果类(基类)
    color = "绿色"    # 定义类属性
    def harvest(self, color):
        print("水果是:" + color + "的!")  # 输出的是形式参数color
        print("水果已经收获……")
        print("水果原来是:" + Fruit.color + "的!")  # 输出的是类属性color


class Apple(Fruit):  # 定义苹果类(派生类)
    color = "红色"
    def __init__(self):
        print("我是苹果")

class Orange(Fruit):  # 定义橘子类(派生类)
    color = "橙色"
    def __init__(self):
        print("n我是橘子")

     # 重写harvest()方法的代码
##    def harvest(self,color):
##        print("橘子是:"+color+"的!")           # 输出的是形式参数color
##        print("橘子已经收获……")
##        print("橘子原来是:"+Fruit.color+"的!");   # 输出的是类属性color


apple = Apple()  # 创建类的实例(苹果)
apple.harvest(apple.color)  # 调用基类的harvest()方法
orange = Orange()  # 创建类的实例(橘子)
orange.harvest(orange.color)  # 调用基类的harvest()方法

方法重写

class Fruit:  # 定义水果类(基类)
    color = "绿色"    # 定义类属性
    def harvest(self, color):
        print("水果是:" + color + "的!")  # 输出的是形式参数color
        print("水果已经收获……")
        print("水果原来是:" + Fruit.color + "的!")  # 输出的是类属性color

class Oranger(Fruit):
    color="橙色"
    def __init__(self):
        print('n我是橘子')
    def harvest(self, color):
        print('橘子是:'+color+'的!')
        print('橘子已经收获……')
        print('橘子原来是:'+Fruit.color+'的!!!!!!!!!!!!!!!!!!!')

Test=Oranger()
Test.harvest('红色')

派生类调用基类方法

class Fruit:
    def __init__(self,color='绿色'):
        Fruit.color=color
    def harvest(self):
        print('水果原来是:'+Fruit.color+'的!')

class Apple(Fruit):
    def __init__(self):
        super().__init__()#需要在派生类使用super函数调用基类方法
        print('我是苹果')

apple=Apple()
apple.harvest()

 和大部分面向对象的语言没什么区别,语法略有不同,如有错误还请指正

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/884157.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号