在创建AlienInvasion类时,给属性self.ship赋值,赋值对象为Ship(self)。
调用Ship()是提供的参数为AlienInvasion的实例,所以为self。
那为什么会报错呀?逻辑没问题呀?
应该是赋值方法不对,参考一下
class Calculator:
name="jisuanqi" #这是固有属性
price=28
#初始化,里面的参数可以自己定义
def __init__(self,name,price,hight,width,weight):
self.name=name
self.price=price
self.h=hight
self.w=width
self.weight=weight
def add(self,x,y):
print(self.name)
result=x+y
print(result)
def subtract(self,x,y):
print(x-y)
def multiply(self,x,y):
print(x*y)
def divide(self,x,y):
print(x/y)
#这里必须传入参数才可以
calc=Calculator('good calc',280,30,30,100)
print(calc.name)#jisuanqi
print(calc.weight)#100
print(calc.price)#280
有用的话请采纳,谢谢!
错误提示说没有 rect属性,检查下代码,或者有没有大小写问题等