我最近参加了有关python的课程的考试。我们被告知要创建一个名为 Place 的类以及两个子类 City 和 Home 。 Place 对象具有名称和位置(如果未输入,则为None)。 City 对象相同,但增加了人口和市长。 Home 对象具有名称和位置,并增加了床位和占用人数。每个对象还具有一个 visited 布尔值,从false开始。
Ex->
indiana = Place('Indiana')
iu = Place('IU Campus', btown)
library = Place('Wells Library', iu)
btown = City('Bloomington', indiana, 400, 'Jim')
rental = Home('Rental House', btown, 4, 3)
我应该实现一种方法 visit(),该方法将地点的 visited 布尔值更改为true,如果它位于 location 将该位置的 visited 布尔值更改为true,然后按以下方式打印输出...
Test code:
library.visit()
indiana.visit()
输出:
您访问Wells图书馆。
那意味着...您访问IU校园。
那意味着...您访问布卢明顿。
那意味着...您访问印第安纳州。
您已经访问过印第安纳州。
对实现 visit()方法有帮助吗?
答案 0 :(得分:1)
使用python 3:
class Place:
def __init__(self, name=None, location=None):
self.name = name
self.location = location
self.visited = False
def visit(self):
if self.visited:
print(f"You already visited {self.name}.")
else:
print(f"You visit {self.name}.")
location = self.location
while location is not None:
print(f"That means... You visit {location.name}.")
location.visited = True
location = location.location
class City(Place):
def __init__(self, name, location, population, mayor):
super().__init__(name, location)
self.population = population
self.mayor = mayor
class Home(Place):
def __init__(self, name, location, num_beds, occupancy):
super().__init__(name, location)
self.num_beds = num_beds
self.occupancy = occupancy
然后在您这样做时:
indiana = Place('Indiana')
btown = City('Bloomington', indiana, 400, 'Jim')
iu = Place('IU Campus', btown)
library = Place('Wells Library', iu)
rental = Home('Rental House', btown, 4, 3)
library.visit()
indiana.visit()
输出:
您访问Wells图书馆。
那意味着...您访问IU校园。
那意味着...您访问布卢明顿。
那意味着...您访问印第安纳州。
您已经访问过印第安纳州。
答案 1 :(得分:0)
我创建了一个示例,并使用注释显示了每个作品的工作方式。我以另一个主题为例,希望可以引导您朝正确的方向发展。这使用1个父类和2个子类。每个子代都有一个唯一的函数,并且都替换了父代已声明的1个或多个函数。
class gamePlatforms:
played = False
name = "blank"
location = "blank"
total_games = "0"
# play function that will be inherited by Console and PC classes
def play(self):
if self.played:
print("You've already played games on it.")
else:
print("First time gamer! Welcome!")
self.played = True
# print_nerdiness function that will be inherited by Console and PC classes, but will be replaced by their own
# functions (polymorphism)
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("Name of console: " + self.name)
print("Location of console: " + self.location)
print("Has it been played: " + was_played)
# set functions, good practice to create get functions as well, but I skipped that.
def set_name(self):
self.name = input("What is the name of the console: ")
def set_location(self):
self.location = input("Which room is the console in: ")
def set_game_total(self):
self.total_games = input("How many games do you have: ")
# Console: child class of gamePlatforms
class Console(gamePlatforms):
controllers = "0"
# can take 0 to 2 arguments
def __init__(self, name=None, total_games=None):
self.name = name
self.total_games = total_games
# This is a unique function for this child class
def set_controllers(self):
self.controllers = input("How many controllers does the console have: ")
# This function replaces the one from the parent class
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("-" * 20)
print("Name of console: " + self.name)
print("Amount of controllers: " + self.controllers)
print("Location of console: " + self.location)
print("Has it been played: " + was_played)
print("Amount of games: " + str(self.total_games))
# PC: child class of gamePlatforms
class PC(gamePlatforms):
OS = "blank"
# can take 0 to 2 arguments
def __init__(self, name=None, total_games=None):
self.name = name
self.total_games = total_games
# this is a unique function to this child class
def set_OS(self):
self.OS = input("What operating system does the computer have: ")
# this function replaces the parent function
def set_name(self):
self.name = input("Enter the model of the pc: ")
# this function replaces the parent function
def print_nerdiness(self):
if self.played:
was_played = "Yep!"
else:
was_played = "No... it's sad."
print("-" * 20)
print("\nModel of PC: " + self.name)
print("Operating system: " + self.OS)
print("Location of pc: " + self.location)
print("Has it been played: " + was_played)
print("Amount of games: " + self.total_games)
# creating a PC object, but only passing the model
myPC = PC("Dell 2000")
# creating a Console object passing Atari as the console name and total_games to 5
myConsole = Console("Atari", 5)
# Calling PC class functions to fill information, will not directly call variables outside the class object.
myPC.set_location()
myPC.set_game_total()
myPC.set_OS()
# Calling Console class functions to fill information, will not directly call variables outside the class object.
myConsole.set_location()
myConsole.set_controllers()
# Look at activity first
myPC.print_nerdiness()
myConsole.print_nerdiness()
# Time to play! This will be like your visit command
myPC.play()
myConsole.play()
# Look at activity again, it's different
myPC.print_nerdiness()
myConsole.print_nerdiness()