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
版权声明:本文为博主作者:Hann Yang原创文章,版权归属原作者,如果侵权,请联系我们删除!
原文链接:https://blog.csdn.net/boysoft2002/article/details/135219583