编码新手并有疑问。这段代码中有一部分我不理解,想知道是否有人可以澄清。该代码可以正常工作,但我不理解这一段。这部分代码在做什么?
filledTrue = (filled == 1)
我猜想如果fill等于1,它将把fillTrue传递给超类,但是我还没有看到这样使用的代码,所以不确定。括号是否像If语句,但没有Else组件一样起作用?
非常感谢!
这是完整的代码:
class GeometricObject:
def __init__(self, color = "green" , filled = True):
self.__color = color
self.__filled = filled
def getColor(self):
return self.__color
def setColor(self, color):
self.__color = color
def isFilled(self):
return self.__filled
def setFilled(self, filled):
self.__filled = filled
def __str__(self):
return "color: " + self.__color + \
" and filled: " + str(self.__filled)
class Triangle(GeometricObject):
def __init__(self, side1 = 1.0 , side2 = 1.0, side3 = 1.0):
super().__init__()
self.__side1 = side1
self.__side2 = side2
self.__side3 = side3
def getSide1(self):
return self.__side1
def setSide1(self, side1):
self.__side1 = side1
def getSide2(self):
return self.__side2
def setSide2(self, side2):
self.__side2 = side2
def getSide3(self):
return self.__side3
def setSide3(self, side3):
self.__side3 = side3
def setArea(self):
self.area = area
def getArea(self):
s = (self.__side1 + self.__side2 + self.__side3) / 2
area = (s * (s - self.__side1) * (s - self.__side2) * (s - self.__side3)) ** 0.5
return area
def getPerimeter(self):
perimeter = self.__side1 + self.__side2 + self.__side3
return perimeter
def triangleString(self):
return "Triangle: Side 1: " + self.__side1 + "Side 2: " + self.__side2 + "Side 3: " + self.__side3
def main():
side1 = eval(input("Enter a number for side 1: "))
side2 = eval(input("Enter a number for side 2: "))
side3 = eval(input("Enter a number for side 3: "))
color = input("Enter a color for the triangle: ")
filled = eval(input("To fill the triangle, enter 1, otherwise enter 0: "))
filledTrue = (filled == 1)
#set the variables in the classes
triangleSolution = Triangle(side1, side2, side3) # this sets the triangle sides in Triangle Class
triangleSolution.setColor(color) # this sets the color in the superclass (through subclass)
triangleSolution.setFilled(filledTrue) # this sets the fill
#get the variables in the classes with printout
print("The triangle’s area is: ", triangleSolution.getArea(), '\n',
"The perimeter is: ", triangleSolution.getPerimeter(), '\n',
"The color is: ", triangleSolution.getColor(), '\n',
"The color is filled: ", triangleSolution.isFilled())
main()
答案 0 :(得分:0)
分配给比较器x == y
或其他变量(大于,小于等)的变量将采用布尔值(True
或False
):
x = 1==1 # True
y = 'a'=='b' # False
z = 2 <= 5 # True