Python 创建矩形类
我真的不太懂课程,任何帮助都会很好 矩形类应具有以下私有数据属性:Python 创建矩形类,python,class,Python,Class,我真的不太懂课程,任何帮助都会很好 矩形类应具有以下私有数据属性: \u长度 \u宽度 矩形类应该有一个\uuuu init\uuu方法来创建这些属性并将它们初始化为1。它还应具有以下方法: 设置长度–此方法为设置长度字段指定一个值 设置宽度–此方法为宽度字段指定一个值 get_length–此方法返回\u length字段的值 get\u width–此方法返回\u width字段的值 get_area–此方法返回矩形区域 \uuuu str\uuuu–此方法返回对象的状态 您的类有
\u长度
\u宽度
\uuuu init\uuu
方法来创建这些属性并将它们初始化为1。它还应具有以下方法:
–此方法为设置长度
字段指定一个值设置长度
–此方法为设置宽度
字段指定一个值宽度
–此方法返回get_length
字段的值\u length
–此方法返回get\u width
字段的值\u width
–此方法返回矩形区域get_area
–此方法返回对象的状态\uuuu str\uuuu
您的
类有一些问题。见下面的评论
class Rectangle:
# Init function
def __init__(self):
# The only members are length and width
self.length = 1
self.width = 1
# Setters
def set_width(self, width):
self.width = width
def set_length(self, length):
self.length = length
# Getters
def get_width(self):
return self.width
def get_length(self):
return self.length
def get_area(self):
return self.length * self.width
# String representation
def __str__(self):
return 'length = {}, width = {}'.format(self.length, self.width)
测试课程
>>> a = Rectangle()
>>> a.set_width(3)
>>> a.set_length(5)
>>> a.get_width()
3
>>> a.get_length()
5
>>> a.get_area()
15
>>> print(a)
length = 5, width = 3
正如其他人所指出的,setter和getter在Python中是多余的,因为所有成员变量都是公共的。我知道你的任务需要这些方法,但在将来,你可以省去麻烦,直接访问成员
>>> a.length # Instead of the getter
5
>>> a.length = 2 # Instead of the setter
>>> a.length
2
您的类有一些问题。见下面的评论
class Rectangle:
# Init function
def __init__(self):
# The only members are length and width
self.length = 1
self.width = 1
# Setters
def set_width(self, width):
self.width = width
def set_length(self, length):
self.length = length
# Getters
def get_width(self):
return self.width
def get_length(self):
return self.length
def get_area(self):
return self.length * self.width
# String representation
def __str__(self):
return 'length = {}, width = {}'.format(self.length, self.width)
测试课程
>>> a = Rectangle()
>>> a.set_width(3)
>>> a.set_length(5)
>>> a.get_width()
3
>>> a.get_length()
5
>>> a.get_area()
15
>>> print(a)
length = 5, width = 3
正如其他人所指出的,setter和getter在Python中是多余的,因为所有成员变量都是公共的。我知道你的任务需要这些方法,但在将来,你可以省去麻烦,直接访问成员
>>> a.length # Instead of the getter
5
>>> a.length = 2 # Instead of the setter
>>> a.length
2
Python不限制对私有数据属性的访问,因此您很少会像使用更严格的语言那样编写“getter”和“setter”(我们都是成年人)
除非它是供内部使用的(将来可能会更改的实现细节),否则您只需将该属性公开给世界—因此更惯用的矩形如下所示:
class Rectangle(object):
def __init__(self, width=1, height=1):
self.width = width
self.height = height
@property
def area(self):
return self.width * self.height
然后:
当然,您可以使用getter和setter编写Rectangle类,但您只在需要验证或转换输入时才这样做,然后您可能需要了解更多有关的信息。Python不限制对私有数据属性的访问,因此您很少编写“getter”和“setter”就像在更严格的语言中(我们都是同意的成年人)
除非它是供内部使用的(将来可能会更改的实现细节),否则您只需将该属性公开给世界—因此更惯用的矩形如下所示:
class Rectangle(object):
def __init__(self, width=1, height=1):
self.width = width
self.height = height
@property
def area(self):
return self.width * self.height
然后:
当然,您可以使用getter和setter编写rectangle类,但是您只在需要验证或转换输入时才这样做,然后您可能需要了解更多有关的信息。首先,这个赋值是一个非常糟糕的主意。在Python中,您几乎从不需要“私有”属性以及getter和setter函数,无论是谁教您这样做都会让您误入歧途
但是,如果您只是想通过作业,而不是学习如何编写像样的Python代码,那么下面是您的方法
首先,要创建名为\uuu length
的属性,只需为其赋值,与任何其他属性相同:
def __init__(self):
self.__length = 1
现在,要为该属性编写getter和setter,请执行相同的操作:
def get_length(self):
return self.__length
def set_length(self, length):
self.__length = length
现在,get\u区域
有点棘手,因为你没有一个\u区域
。(这是一个愚蠢的想法,因为它看起来像一个getter函数,尽管它不是…)但是你知道如何计算矩形的面积:它只是长度乘以宽度,对吗
def get_area(self):
return self.__length * self.__width
\uuuu str\uuuu
方法是整个作业中唯一的好主意,尽管对于这样的类,编写一个\uuuuu str\uuuu
而不使用\uu repr\uuu
可能是个坏主意。无论如何,这两种方法都只是返回一个字符串,其中包含一些有用的对象表示形式。str
应该对最终用户友好,而repr
应该对程序员(您或使用您的类的人)有用。例如:
def __str__(self):
return '{} x {} rectangle'.format(self.__length, self.__width)
def __repr__(self):
return '{}({}, {})'.format(type(self).__name__, self.__length, self.__width)
首先,这个作业是个很糟糕的主意。在Python中,您几乎从不需要“私有”属性以及getter和setter函数,无论是谁教您这样做都会让您误入歧途
但是,如果您只是想通过作业,而不是学习如何编写像样的Python代码,那么下面是您的方法
首先,要创建名为\uuu length
的属性,只需为其赋值,与任何其他属性相同:
def __init__(self):
self.__length = 1
现在,要为该属性编写getter和setter,请执行相同的操作:
def get_length(self):
return self.__length
def set_length(self, length):
self.__length = length
现在,get\u区域
有点棘手,因为你没有一个\u区域
。(这是一个愚蠢的想法,因为它看起来像一个getter函数,尽管它不是…)但是你知道如何计算矩形的面积:它只是长度乘以宽度,对吗
def get_area(self):
return self.__length * self.__width
\uuuu str\uuuu
方法是整个作业中唯一的好主意,尽管对于这样的类,编写一个\uuuuu str\uuuu
而不使用\uu repr\uuu
可能是个坏主意。无论如何,这两种方法都只是返回一个字符串,其中包含一些有用的对象表示形式。str
应该对最终用户友好,而repr
应该对程序员(您或使用您的类的人)有用。例如:
def __str__(self):
return '{} x {} rectangle'.format(self.__length, self.__width)
def __repr__(self):
return '{}({}, {})'.format(type(self).__name__, self.__length, self.__width)
如果没有set_u和get_u的函数,您会做得非常好,并且在使用损坏的变量(例如uu variablename)时,您可能应该更加小心,但是这里有一个不太出色的代码可以满足您的需求。希望这能有所帮助
附言: