Python 设计班级结构
我是一个完全的面向对象编程初学者,我有一个奇怪的家庭作业 基本上是关于圣诞老人的,他是如何在某个城市,挨家挨户地给人们送礼物的 我作业的第一部分是这样的:Python 设计班级结构,python,class,constructor,structure,Python,Class,Constructor,Structure,我是一个完全的面向对象编程初学者,我有一个奇怪的家庭作业 基本上是关于圣诞老人的,他是如何在某个城市,挨家挨户地给人们送礼物的 我作业的第一部分是这样的: class City: def __init__(self): self.coordinates = set() def gift(self, x, y): self.coordinates.add((x, y)) def was_gifted(self, x, y): return (x, y) in self
class City:
def __init__(self):
self.coordinates = set()
def gift(self, x, y):
self.coordinates.add((x, y))
def was_gifted(self, x, y):
return (x, y) in self.coordinates # Does this return True or False?????
def all_gifted(self):
return self.coordinates
class Santa(City):
def __init__(self, City):
self.city = City
self.coords = (0, 0)
def move(self, c):
if c == "^":
self.coords[1] -= 1
elif c == "ˇ":
self.coords[1] += 1
elif c == "<":
self.coords[0] -= 1
elif c == ">":
self.coords[0] += 1
def moves(self, path):
for i in path:
self.move(i)
def gift(self):
self.city.gift
class FastSanta:
?????????????
编写一个类City
- 构造函数没有参数。它能做什么取决于你:它必须为其他方法准备地形李>
- 方法
记录坐标gift(x,y)
处的房屋收到礼物(x,y)
- 如果位于
的房子收到了礼物,则方法(x,y)
返回被赠送(x,y)
<代码>错误否则李>True
- 方法
返回获得礼物的所有房屋的一组坐标李>all_gifted()
class City:
def __init__(self):
self.coordinates = set()
def gift(self, x, y):
self.coordinates.add((x, y))
def was_gifted(self, x, y):
return (x, y) in self.coordinates # Does this return True or False?????
def all_gifted(self):
return self.coordinates
class Santa(City):
def __init__(self, City):
self.city = City
self.coords = (0, 0)
def move(self, c):
if c == "^":
self.coords[1] -= 1
elif c == "ˇ":
self.coords[1] += 1
elif c == "<":
self.coords[0] -= 1
elif c == ">":
self.coords[0] += 1
def moves(self, path):
for i in path:
self.move(i)
def gift(self):
self.city.gift
class FastSanta:
?????????????
编写一个类Santa
- 构造器有一个参数,代表圣诞老人将在其中分发礼物的城市。开始时,他站在坐标
李>(0,0)
- 方法
获取字符move(c)
,“v”
,“^”
,并将圣诞老人向下、向上、向左或向右移动一个字段。坐标”
这次向上增加;i、 e.y
将其增加1,而“^”
将其减少李>“v”
- 方法
获取此类字符的字符串,并相应地移动圣诞老人李>moves(path)
- 方法
gift()。请注意,
没有参数(除了gift
)李>self
RapidSanta
,该类派生自Santa
。它的构造器获得一个城市
和速度
RapidSanta
与普通的不同之处在于,它不是通过单个字段移动,而是以我们在构造函数中设置的速度移动
谁能帮帮我,因为我都不知道该从哪里开始。如果有人知道类似的“问题”,请链接或在论坛上发布。我想掌握这一点,但坦率地说,我不是一个编程天才。提前谢谢你的帮助。以下是我的作业必须通过的测试
import unittest
class TestCity(unittest.TestCase):
def test_City(self):
a = City()
b = City()
self.assertSetEqual(a.all_gifted(), set())
self.assertSetEqual(b.all_gifted(), set())
self.assertFalse(a.is_gifted(1, 3))
self.assertFalse(b.is_gifted(1, 3))
a.gift(1, 3)
self.assertSetEqual(a.all_gifted(), {(1, 3)})
self.assertSetEqual(b.all_gifted(), set())
self.assertTrue(a.is_gifted(1, 3))
self.assertFalse(b.is_gifted(1, 3))
a.gift(1, 3)
self.assertSetEqual(a.all_gifted(), {(1, 3)})
self.assertSetEqual(b.all_gifted(), set())
self.assertTrue(a.is_gifted(1, 3))
self.assertFalse(a.is_gifted(-2, -3))
self.assertFalse(b.is_gifted(1, 3))
a.gift(-2, -3)
self.assertSetEqual(a.all_gifted(), {(1, 3), (-2, -3)})
self.assertSetEqual(b.all_gifted(), set())
self.assertTrue(a.is_gifted(1, 3))
self.assertTrue(a.is_gifted(-2, -3))
self.assertFalse(b.is_gifted(1, 3))
b.gift(0, 0)
self.assertSetEqual(a.all_gifted(), {(1, 3), (-2, -3)})
self.assertSetEqual(b.all_gifted(), {(0, 0)})
self.assertTrue(a.is_gifted(1, 3))
self.assertTrue(a.is_gifted(-2, -3))
self.assertFalse(b.is_gifted(1, 3))
self.assertTrue(b.is_gifted(0, 0))
self.assertFalse(a.is_gifted(0, 0))
class TestSanta(unittest.TestCase):
def test_gift(self):
m = City()
n = City()
b = Santa(m)
b.gift()
self.assertSetEqual(m.all_gifted(), {(0, 0)})
self.assertSetEqual(n.all_gifted(), set())
def test_move(self):
m = City()
n = City()
b = Santa(m)
c = Santa(m)
b.gift()
self.assertSetEqual(m.all_gifted(), {(0, 0)})
self.assertSetEqual(n.all_gifted(), set())
b.move("^")
b.move("<")
b.move("<")
b.gift()
b.move("v")
self.assertSetEqual(m.all_gifted(), {(0, 0), (-2, 1)})
self.assertSetEqual(n.all_gifted(), set())
c.gift()
self.assertSetEqual(m.all_gifted(), {(0, 0), (-2, 1)})
b.gift()
self.assertSetEqual(m.all_gifted(), {(0, 0), (-2, 1), (-2, 0)})
b.move(">")
self.assertSetEqual(m.all_gifted(), {(0, 0), (-2, 1), (-2, 0)})
b.gift()
self.assertSetEqual(m.all_gifted(), {(0, 0), (-2, 1), (-2, 0), (-1, 0)})
c.move("v")
self.assertSetEqual(m.all_gifted(), {(0, 0), (-2, 1), (-2, 0), (-1, 0)})
c.gift()
self.assertSetEqual(m.all_gifted(), {(0, 0), (-2, 1), (-2, 0), (-1, 0), (0, -1)})
self.assertSetEqual(n.all_gifted(), set())
def test_moves(self):
m = City()
b = Santa(m)
b.moves("v<<^^^>>>>>")
b.gift()
self.assertSetEqual(m.all_gifted(), {(3, 2)})
cc = ""
def p(c):
nonlocal cc
cc += c
b.move = p
b.moves("v<<^^^>>>>>")
self.assertEqual(
cc, "v<<^^^>>>>>",
"Write a method `moves` so that it will use method `move`")
def test_rapid_Santa(self):
m = City()
b = RapidSanta(m, 2)
c = RapidSanta(m, 3)
b.gift()
self.assertSetEqual(m.all_gifted(), {(0, 0)})
b.move("^")
b.gift()
self.assertSetEqual(m.all_gifted(), {(0, 0), (0, 2)})
b.move("<")
b.gift()
self.assertSetEqual(m.all_gifted(), {(0, 0), (0, 2), (-2, 2)})
c.move("<")
c.gift()
self.assertSetEqual(m.all_gifted(), {(0, 0), (0, 2), (-2, 2), (-3, 0)})
def test_methods(self):
self.assertIs(RapidSanta.moves, Santa.moves,
"RapidSanta has to inherit method moves")
self.assertIs(RapidSanta.gift, Santa.gift,
"RapidSanta has to inherit method gift")
导入单元测试
类TestCity(unittest.TestCase):
def测试城市(自身):
a=城市()
b=城市()
self.assertSetEqual(a.all_gifted(),set())
self.assertSetEqual(b.all_gifted(),set())
自我评价假(a.有天赋(1,3))
自我资产虚假(b.有天赋(1,3))
a、 礼物(1,3)
self.assertSetEqual(a.all_gifted(),{(1,3)})
self.assertSetEqual(b.all_gifted(),set())
自我评价真实(a.有天赋(1,3))
自我资产虚假(b.有天赋(1,3))
a、 礼物(1,3)
self.assertSetEqual(a.all_gifted(),{(1,3)})
self.assertSetEqual(b.all_gifted(),set())
自我评价真实(a.有天赋(1,3))
自我资产虚假(a.有天赋(-2,-3))
自我资产虚假(b.有天赋(1,3))
a、 礼物(-2,-3)
self.assertSetEqual(a.all_gifted(),{(1,3),(-2,-3})
self.assertSetEqual(b.all_gifted(),set())
自我评价真实(a.有天赋(1,3))
自我资产真实(a.有天赋(-2,-3))
自我资产虚假(b.有天赋(1,3))
b、 礼物(0,0)
self.assertSetEqual(a.all_gifted(),{(1,3),(-2,-3})
self.assertSetEqual(b.all_gifted(),{(0,0)})
自我评价真实(a.有天赋(1,3))
自我资产真实(a.有天赋(-2,-3))
自我资产虚假(b.有天赋(1,3))
自我评价真实(b.有天赋(0,0))
自我资产虚假(a.is_天才(0,0))
类TestSanta(unittest.TestCase):
def测试_礼品(自我):
m=城市()
n=城市()
b=圣诞老人(米)
b、 礼品()
self.assertSetEqual(m.all_gifted(),{(0,0)})
self.assertSetEqual(n.all_gifted(),set())
def测试_移动(自):
m=城市()
n=城市()
b=圣诞老人(米)
c=圣塔(m)
b、 礼品()
self.assertSetEqual(m.all_gifted(),{(0,0)})
self.assertSetEqual(n.all_gifted(),set())
b、 移动(“^”)
b、 移动(“>>”)
b、 礼品()
self.assertSetEqual(m.all_gifted(),{(3,2)})
cc=“”
def p(c):
非局部cc
cc+=c
b、 移动=p
b、 移动(“v>>>”)
自我评价资格(
抄送,“v>>>”,
“编写一个方法'move',以便它使用方法'move'”)
def测试_rapid_Santa(自身):
m=城市()
b=RapidSanta(m,2)
c=RapidSanta(m,3)
b、 礼品()
self.assertSetEqual(m.all_gifted(),{(0,0)})
b、 移动(“^”)
b、 礼品()
self.assertSetEqual(m.all_gifted(),{(0,0)、(0,2)})
b、 move(首先修复你的City
类(注意:假设Python2.x-不需要从Python3.x中的对象继承)
就我而言,Santa
类的规范非常清楚。您的类将需要一个city
(城市的实例)和coords
实例属性,第一个作为参数传递,第二个默认为(0,0)
move(self,c)
方法将根据字符c
的值更新self.coords
——即假设(0,0)
作为当前coords,santa.move('^')
应将self.coords
更新为(0,1)
,并且移动(self,moves)
方法只需迭代移动
并为移动中的每个字符调用self.move(c)
gift(self)
方法仅使用当前的coords调用self.city.gift()
wrt/FastSanta
,一旦你拥有了Santaclass Santa(object):
def __init__(self, city):
self.city = city
self.coords = (0, 0)
class Santa(City):
def __init__(self, City):
self.city = City
def __init__(self, city):
self.city = city
def gift(self):
self.city.gift
if <some-boolean-expression>:
return True
else:
return False
class City:
def __init__(self):
self.coordinates = set()
def gift(self, x, y):
self.coordinates.add((x, y))
def was_gifted(self, x, y):
return (x, y) in self.coordinates # Does this return True or False?????
def all_gifted(self):
return self.coordinates
class Santa(City):
def __init__(self, City):
self.city = City
self.coords = (0, 0)
def move(self, c):
if c == "^":
self.coords[1] -= 1
elif c == "ˇ":
self.coords[1] += 1
elif c == "<":
self.coords[0] -= 1
elif c == ">":
self.coords[0] += 1
def moves(self, path):
for i in path:
self.move(i)
def gift(self):
self.city.gift
class FastSanta:
?????????????