Python,面向对象

Python,面向对象,python,python-3.x,Python,Python 3.x,我最近参加了一门关于python的课程的考试。我们被告知创建一个名为Place的类和两个儿童类,City和Home。Place对象具有名称和位置(如果未输入,则无)。城市物体有相同的,但增加了人口和市长。Home objects有一个名称和位置,并添加了一些床位和入住率。每个对象也有一个访问的布尔值,从false开始 例如--> 我应该实现一个visit()方法,该方法将该位置的访问布尔值更改为true,如果它位于某个位置,则将该位置的访问布尔值更改为true,并按如下方式打印 Test cod

我最近参加了一门关于python的课程的考试。我们被告知创建一个名为Place的类和两个儿童类,City和Home。Place对象具有名称和位置(如果未输入,则无)。城市物体有相同的,但增加了人口和市长。Home objects有一个名称和位置,并添加了一些床位和入住率。每个对象也有一个访问的布尔值,从false开始

例如-->

我应该实现一个visit()方法,该方法将该位置的访问布尔值更改为true,如果它位于某个位置,则将该位置的访问布尔值更改为true,并按如下方式打印

Test code: 
library.visit()
indiana.visit()
输出:

您可以访问威尔斯图书馆。
这意味着。。。您将参观IU校园。
这意味着。。。您将访问布卢明顿。
这意味着。。。您将访问印第安纳州。
您已经访问了印第安纳州。

有关于使用python 3实现visit()方法的帮助吗?

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()
输出:

您可以访问威尔斯图书馆。
这意味着。。。您将参观IU校园。
这意味着。。。您将访问布卢明顿。
这意味着。。。您将访问印第安纳州。
你已经去过印第安纳了


我已经创建了一个示例,并使用注释来展示每件作品的工作原理。我用了另一个主题作为例子,希望能引导你走向正确的方向。这将使用1个父类和2个子类。每个子级都有一个唯一的函数,并且都替换了父级中已声明的一个或多个函数

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()

创建一个全局列表(最初为空)。检查您是否已经访问过该地点,如果没有,请将该地点添加到列表中。您尝试了什么?为什么不起作用?
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()