如何将参数传递给 Python 中的 class?
How to pass arguments into class in Python?
我在 Python 开始学习 OOP。我有一个像这样的简单问题:我想创建一个 class 角,它代表多边形的角。每个角都有 (x,y) 作为其坐标。下面是我的代码
class corner(): # Creating class corner and pass x,y as coordinate (updated as guide)
def __init__(self, x, y):
self.x = x
self.y = y
#Creating objects from class (still doesn't work)
cornerA=corner(1,2)
print(cornerA) #Expected (1,2)
cornerB=corner(3,4)
print(cornerB) #Expected (3,4)
你的class角落应该是这样的:
class corner(): # Creating class corner and pass x,y as coordinate
def __init__(self, x, y):
self.x = x
self.y = y
我在 Python 开始学习 OOP。我有一个像这样的简单问题:我想创建一个 class 角,它代表多边形的角。每个角都有 (x,y) 作为其坐标。下面是我的代码
class corner(): # Creating class corner and pass x,y as coordinate (updated as guide)
def __init__(self, x, y):
self.x = x
self.y = y
#Creating objects from class (still doesn't work)
cornerA=corner(1,2)
print(cornerA) #Expected (1,2)
cornerB=corner(3,4)
print(cornerB) #Expected (3,4)
你的class角落应该是这样的:
class corner(): # Creating class corner and pass x,y as coordinate
def __init__(self, x, y):
self.x = x
self.y = y