首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >If语句,同时使用getters和setters?

If语句,同时使用getters和setters?
EN

Stack Overflow用户
提问于 2019-05-11 05:18:30
回答 1查看 161关注 0票数 1

我正在尝试返回基于"macType“的计算机的价格,这是计算机的大小。我不知道在哪里集成if语句到我的代码中,啊!

代码语言:javascript
复制
class apple:
    def __init__(self,pType,price):
        self.__pType=pType
        self.__price=price

    def setpType(self,pType):
            self.__pType=pType

    def setprice(self,price):
            self.__price=price

    def getpType(self):
        return self.__pType

    def getprice(self):
        return self.__price

class mac(apple):
    def __init__(self,pType,price,macType):
        apple.__init__(self,pType,price)
        self.__price=price
        self.__macType=macType

    def setmacType(self,macType):
            self.__macType=macType

    def setmacPrice(self,price):
        if(macType()=="11Inch"):
            self.__price=float(price*.9)
        elif(macType()=="13Inch"):
            self.__price=price
        elif(macType()=="15Inch"):
            self.__price=float(price*1.2)

    def getmacType(self):
        return self.__macType

    def getprice(self):
        if (self.__macType == "11inch"):
            return super(mac, self).getprice()*.9
        elif (self.__macType == "13inch"):
            return super(mac, self).getprice()
        else:
            return super(mac, self).getprice()*1.1

a1 = apple("computer",1000)
m1 = mac("computer",1000,"11Inch")
m2 = mac("computer",1000,"13Inch")
m3 = mac("computer",1000,"15Inch")

print("a1 is a ",a1.getpType(),"and it costs",a1.getprice())
print("m1 is a ",m1.getmacType(),"and it costs",m1.getprice())
print("m1 is a ",m2.getmacType(),"and it costs",m2.getprice())
print("m1 is a ",m3.getmacType(),"and it costs",m3.getprice())

实际输出应显示11英寸为900,13英寸为1000,15英寸为1100。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-11 05:25:48

Python字符串比较区分大小写。在你的getprice方法中,你使用"11inch",但是你给了你的构造"11Inch"注意到大小写的i?并不能相提并论。只需在任何地方使用相同的代码,或者更好的方法是查看enum模块。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56084961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档