挑战Python100题(6)

2023-12-28 17:51:16

100+ Python challenging programming exercises 6

Question 51

Define a class named American and its subclass NewYorker.

Hints: Use class Subclass(ParentClass) to define a subclass.

定义一个名为American的类及其子类NewYorker。

提示:使用class Subclass(ParentClass)来定义子类。

Solution:

class American:  
    def __init__(self, name, age):  
        self.name = name  
        self.age = age  
  
    def introduce(self):  
        return f"Hello, my name is {self.name} and I am {self.age} years old."  
  
class NewYorker(American):  
    def introduce(self):  
        return f"Yo, my name is {self.name} and I am from New York. I'm {self.age} years old."

anAmerican = American('Tom',8)
aN

文章来源:https://blog.csdn.net/boysoft2002/article/details/135219583
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。