python中rect函数_使用类和函数的面向对象Python-rectangle

本文指导如何使用Python创建一个面向对象的Rectangle类,通过实例化和封装属性(如宽度、长度、面积和周长),展示继承、多态和属性访问控制。通过main函数展示如何操作对象并打印相关信息,以实现35分满分的标准输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我正在用Python创建一个程序,它将利用面向对象的编程来打印给定矩形的属性。该项目有以下限制: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. Your job is to determine what

attributes and methods the Rectangle class requires and create a class

that will satisfy the requirements.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)

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

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

以下是评估准则代码:main()函数相对不变3

代码:矩形类是用默认值声明的,因此它支持0、1和2参数3

代码:实例化矩形(5,7)2

代码:实例化Rectangle()2

代码:矩形类定义实例变量2

代码:为每个实例变量2定义getter和setter

代码:矩形类包括面积和周长方法4

代码:矩形类继承自某个对象,即使它是对象2

代码:矩形类定义宽度和长度属性4

代码:矩形包含派生的只读实例变量2

代码:当python文件作为main 2执行时调用main

代码:矩形类定义了返回字符串4的getStats()方法

执行:打印矩形a:1

执行:打印区域:351

执行:打印周长:24 1

执行:打印矩形b:1

执行:打印宽度:101

执行:打印高度:201

执行:打印区域:200 1

执行:打印周长:60 1

40分

我得到这个代码的开始是: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()

我应该得到这个输出:Rectangle a:

area: 35

perimeter: 24

Rectangle b:

width: 10

height: 20

area: 200

perimeter: 60

我已经走了这么远,但是我不能用矩形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()

我正在获取此输出:Rectangle a:

area: 35

perimeter: 24

Rectangle b:

area: 200

perimeter: 60

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值