Python面向对象

1.类


Class Cat:
    #初始化方法,创建对象时会自动调用    
    def __init__(self, color, weight, weiba):
        self.color = color
        self.weight = weight
        self.weiba = weiba

    #类中的方法,都需要加self参数
    def eat(self):
        print("吃鱼")

xiaohuamao = Cat("white", 5, "有尾巴")
xiaohuamao.eat()

2.Demo1

#conding=utf-8 

class SweetPotato:

    def __init__(self):
        self.cookLevel = 0
        self.cookString = "生的"
        self.condiments  = []

    def cook(self,times):
        self.cookLevel += times
        if self.cookLevel > 0 and self.cookLevel <= 3:
            self.cookString = "生的"
        elif self.cookLevel > 3 and self.cookLevel <= 5:
            self.cookString = "半生不熟"
        elif self.cookLevel > 5 and self.cookLevel <= 8:
            self.cookString = "熟了"
        elif self.cookLevel > 8:
            self.cookString = "烤焦了"

    def addCondiments(self,temp):
        self.condiments.append(temp)

    def __str__(self):
        if len(self.condiments) == 0:
            s = "地瓜的cookLever为:%s,地瓜的生熟程度为:%s"%(str(self.cookLevel),self.cookString)
        else:
            zuoliao = ""
            for i,condiment in enumerate(self.condiments):
                if i == len(self.condiments) - 1:
                    zuoliao = zuoliao + condiment
                else:
                    zuoliao = zuoliao + condiment + ","
            s = "地瓜的cookLever为:%s,地瓜的生熟程度为:%s,添加的佐料为:%s"%(str(self.cookLevel),self.cookString,zuoliao)

        return s

potato = SweetPotato()
print(potato) 
potato.cook(1)
potato.addCondiments("胡椒")
print(potato)
potato.cook(3)
potato.addCondiments("辣椒")
print(potato)
potato.cook(2)
potato.addCondiments("酱油")
print(potato)
potato.cook(4)
potato.addCondiments("醋")
print(potato)

3.Demo2


#coding=utf-8

class Home:

    def __init__(self,area):
        self.area = area
        self.items = []

    def addItems(self,item):
        itemArea = item.getArea();
        self.area -= itemArea
        if self.area >= 0:
            print("添加家具成功")
            self.items.append(item)
        else:
            print("添加家具失败,空间不够了")
            self.area += itemArea

    def __str__(self):

        if len(self.items) > 0:
            s = "当前家里剩余的面积为:" + str(self.area) + "......家里的家具为:"
            for temp in self.items:
                s += temp.getName() + ","
            return s.strip(",")
        else:
            s = "当前家里剩余的面积为:" + str(self.area) + "......家里没有家具"
            return s


class Bed:

    def __init__(self,name,area):
        self.name = name 
        self.area = area

    def getName(self):
        return self.name

    def getArea(self):
        return self.area

    def __str__(self):
        s = "床的名字为:%s,床的面积为:%s"%(self.name,str(self.area))
        return s


home = Home(100)
print(home)

bed = Bed("大床",15)
print(bed)
home.addItems(bed)
print(home)

bed2 = Bed("婴儿床",150)
print(bed2)
home.addItems(bed2)
print(home)

4.私有属性和私有方法

在属性和方法前面加两个下划线,代表私有属性和私有方法。私有属性和私有方法只能在类内部使用,在类外部不能使用。在类外部通过本类对象来调用私有属性和私有方法也是不行的(和java的区别)。


#coding=utf-8  

class Person:

    def __init__(self):
        #这表示私有属性,外部不能直接访问
        self.__name = None
        self.__age = None

    def setName(self,name):
        self.__name = name

    def getName(self):
        return self.__name

    def setAge(self,age):
        self.__age = age

    def getAge(self):
        return self.__age

    #这是私有方法,外部不能直接调用
    #Python把私有方法的名字改了,改成_Person__test1()
    def __test1(self):
        print("test1======")

    #这个方法暴露给外部,来调用私有方法
    def test2(self):
        self.__test1()
        print("test2======")


person = Person()
person.setName("Tom")
person.setAge(18)

print(person.getName() + "......" + str(person.getAge()))

person.test2()

#这样可以调用私有方法 
person._Person__test1()

5.多继承

多继承中,两个父类有同名方法,子类调用该方法,则执行第一个父类里面的方法。(由mro算法决定)

6.多态

Python是弱类型语言,多态表现并不明显

7.类属性、类方法和静态方法


#coding=utf-8

class Test(object):

    #类属性
    num = 0

    #实例方法
    def __init__(self):
        #实例属性
        self.age = 10

    #实例方法
    def abc(self):
        print("实例方法")

    #类方法(主要作用是用来操作类属性的,比较安全,和静态方法区别开来)
    @classmethod
    def setNum(cls,num):
        cls.num = num 

    #静态方法(完成一个相对独立的功能)
    @staticmethod
    def testStatic():
        print("test static method")


t = Test()
t.abc()

print(Test.num)
#通过类方法来操作类属性,这样比较安全
Test.setNum(20)
print(Test.num)

t.testStatic()
Test.testStatic()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值