简体   繁体   English

面向对象的 Python - 使用类和函数的矩形

[英]Object Oriented Python - rectangle using classes and functions

I am creating a program in Python that will utilize object oriented programming to print the properties of a given rectangle.我正在用 Python 创建一个程序,它将利用面向对象的编程来打印给定矩形的属性。 The project has these given constraints:该项目具有以下限制条件:

The purpose of this lab is to give you practice creating your own object.本实验的目的是让您练习创建自己的对象。 You will be given a main function that expects an instance of the (yet undefined) Rectangle object.您将获得一个 main 函数,该函数需要一个(尚未定义的)Rectangle 对象的实例。 Your job is to determine what attributes and methods the Rectangle class requires and create a class that will satisfy the requirements.您的工作是确定 Rectangle 类需要哪些属性和方法,并创建一个满足要求的类。

  • Add only one feature at a time一次只添加一项功能
  • You may need to comment out parts of the main function for testing您可能需要注释掉主要功能的部分以进行测试
  • Constructor should take 0, 1, or 2 parameters (illustrating polymorphism)构造函数应采用 0、1 或 2 个参数(说明多态性)
  • Your class should be a subclass of something (illustrating inheritance)你的类应该是某个东西的子类(说明继承)
  • Your class should have methods and properties (illustrating encapsulation)你的类应该有方法和属性(说明封装)
  • Make your instance variables hidden (using the __ trick)隐藏您的实例变量(使用 __ 技巧)
  • Add setter and getter methods for each instance variable为每个实例变量添加 setter 和 getter 方法
  • Use properties to encapsulate instance variable access使用属性封装实例变量访问
  • Not all instance variables are real... Some are derived, and should be write-only并非所有实例变量都是真实的...有些是派生的,应该是只写的
  • You may not substantially change the main function (unless you're doing the blackbelt challenge)您可能不会大幅更改主要功能(除非您正在进行黑带挑战)
  • Be sure to add the needed code to run the main function when needed确保在需要时添加所需的代码以运行主函数

Here is the rubric这是标题

  • Code: main() function is relatively unchanged 3代码:main()函数相对不变3
  • Code: Rectangle class is declared with defaults so it supports 0, 1 and 2 parameters 3代码:矩形类声明为默认值,因此它支持 0、1 和 2 参数 3
  • Code: Instantiates Rectangle(5,7) 2代码:实例化 Rectangle(5,7) 2
  • Code: Instantiates Rectangle() 2代码:实例化 Rectangle() 2
  • Code: Rectangle class defines __ instance variables 2代码:Rectangle类定义__实例变量2
  • Code: Defines getters and setters for each instance variable 2代码:为每个实例变量定义getter和setter 2
  • Code: Rectangle class include area and perimeter methods 4代码:矩形类包括面积和周长方法 4
  • Code: Rectangle class inherits from something, even if it's object 2代码:矩形类继承自某物,即使它是对象 2
  • Code: Rectangle class defines width and length properties 4代码:矩形类定义宽度和长度属性 4
  • Code: Rectangle includes derived read-only instance variables 2代码:矩形包含派生的只读实例变量 2
  • Code: Invokes main when the python file is executed as main 2代码:当python文件作为main 2执行时调用main
  • Code: Rectangle class defines getStats() method that returns a string 4代码: Rectangle 类定义了返回字符串 4 的 getStats() 方法
  • Execution: prints Rectangle a: 1执行:打印 Rectangle a: 1
  • Execution: prints area: 35 1执行:打印面积:35 1
  • Execution: prints perimeter: 24 1执行:打印周长:24 1
  • Execution: prints Rectangle b: 1执行:打印 Rectangle b: 1
  • Execution: prints width: 10 1执行:打印宽度:10 1
  • Execution: prints height: 20 1执行:打印高度:20 1
  • Execution: prints area: 200 1执行:打印面积:200 1
  • Execution: prints perimeter: 60 1执行:打印周长:60 1

Score 40 40 分

I am given this code to start off with:我得到了这个代码来开始:

def main():
print "Rectangle a:"
a = Rectangle(5, 7)
print "area:      %d" % a.area
print "perimeter: %d" % a.perimeter

print ""
print "Rectangle b:"
b = Rectangle()
b.width = 10
b.height = 20
print b.getStats()

I am supposed to get this output:我应该得到这个输出:

Rectangle a:
area:      35
perimeter: 24
Rectangle b:
width:     10
height:    20
area:      200
perimeter: 60

I have gotten this far but I can not get Rectangle B to print the width and height Can you please take a look?我已经走了这么远,但我无法让矩形 B 打印宽度和高度你能看看吗?

class Rectangle:
    def __init__(self, width=0, height=0):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

    def perimeter(self):
        return 2 * self.height + 2 * self.width

    def setWidth(self, width):
        self.width = width

    def setHeight(self, height):
        self.height = height

    def getStats(self):
        return "area:      %s\nperimeter: %s" % (self.area(), self.perimeter())


def main():
    print ""
    print "Rectangle a:"
    a = Rectangle(5, 7)
    print "area:      %s" % a.area()
    print "perimeter: %s" % a.perimeter()

    print ""
    print "Rectangle b:"
    b = Rectangle()
    b.width = 10
    b.height = 20
    print b.getStats()
    print ""


main()

I am currently getting this output:我目前得到这个输出:

Rectangle a:
area:      35
perimeter: 24

Rectangle b:
area:      200
perimeter: 60


Process finished with exit code 0

Not sure I got your question right, but you may want to try:不确定我的问题是否正确,但您可能想尝试:

def getStats(self):
    return "width:      %s\nheight:      %s\narea:      %s\nperimeter: %s" % (self.width, self.height, self.area(), self.perimeter())

To satisfy requirements 4 and 6, I would suggest something like:为了满足要求 4 和 6,我建议如下:

class Shape(object):
    def area(self):
        raise NotImplementedError

    def perimeter(self):
        raise NotImplementedError

class Rectangle(Shape):
    def __init__(self, width=0, height=0):
        super(Rectangle, self).__init__() # this is useless in this case, but it's good practice 
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

    def perimeter(self):
        return 2 * self.height + 2 * self.width

    @property
    def width(self):
        return self.__width

    @property
    def height(self):
        return self.__height

    @width.setter
    def width(self, width):
        self.__width = width

    @height.setter
    def height(self, height):
        self.__height = height

    def getStats(self):
        return "width:      %s\nheight:      %s\narea:      %s\nperimeter: %s" % (self.width, self.height, self.area(), self.perimeter())

NOTE: This homework is an exact assignment from my computer science class.注意:这个作业是我计算机科学课上的一个精确的作业。 While you are welcome to get help from sites like stack overflow, you are still responsible for your own work, and if you turn in code from this site (which may or may not be acceptable) we will know, and we will consider it academic misconduct.虽然欢迎您从堆栈溢出等网站获得帮助,但您仍需对自己的工作负责,如果您从该网站上交代码(可能会或可能不会被接受)我们会知道,我们会认为它是学术性的不当行为。 We may also have made some quiet changes to the assignment, so if you turn this answer in without careful consideration, you are unlikely to get full credit even我们也可能对作业进行了一些悄悄的更改,因此如果您不经仔细考虑就提交此答案,即使您也不太可能获得全部学分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM