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

python对象类

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

python对象类

'''
class Geese:
    def __init__(self):
        print("fuck")
wildgooes=Geese()
class Goose:
    neck="long"
    leg="xi"
    wing="wu"
    def __init__(self,wing,mouth):    #带参构造方法
        print("fuck")
        print(wing+"big")
        print(mouth+"red")
    def fly(self,w):
        print(w)
    def fun1(self):
        print(Goose.neck)
go=Goose("11","22")
go.fly(1)
go.fun1()
print(go.neck)

class Swan:
    _neck_swan="swan's neck is long"       #protected,允许类和子类访问
    __neck_swan="long"                     #private,允许类本身访问n
    def __init__(self):
        print(Swan._neck_swan)
        print(Swan.__neck_swan)
swan=Swan()
print(swan._neck_swan)

class Rect:
    def __init__(self,width,height):
        self.width=width
        self.height=height
    @property                         #将方法转化为属性
    def area(self):
        return self.width*self.height
rect=Rect(10,10)
print(rect.area)
class TVshow:
    list_film=["zhanglang2","bbbs","ybb"]
    def __init__(self,show):
        self.__show=show
    @property
    def show(self):
        return self.__show
    @show.setter                            #修改方法属性
    def show(self,value):
        if value in TVshow.list_film:
            self.__show="you chose <<"+value+">>"
        else:
            self.__show="don't exsit"
tvshow=TVshow("www")
print(tvshow.show)
tvshow.show="ybb"
print(tvshow.show)
tvshow.show="233"
print(tvshow.show)
'''
class fruit:
    color="green"
    def __init__(self,color):
        print(color)
    def harvest(self,color):
        print("the color is"+color)
        print("fruit has been harvested")
        print("the original color is"+fruit.color)
class apple(fruit):
    color="red"
    def __init__(self):
        print("this is applr")
class orange(fruit):             #继承,可以继承方法,属性
    def __init__(self):
        print("this is orange")
Apple=apple()
Apple.harvest(Apple.color)
Orange=orange()
Orange.harvest(Orange.color)
class peach(fruit):
    color ="pink"
    def __init__(self):
        super().__init__(super().color)             #用super()调用基类的__init__和属性
        print("this is peach")
    def harvest(self):
        print("the color of peach is "+peach.color)
Peach=peach()
Peach.harvest()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/445116.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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