Java基础练习中,子类如何通过继承改变父类权限?
- 内容介绍
- 文章标签
- 相关推荐
本文共计247个文字,预计阅读时间需要1分钟。
pythonclass Father: def __init__(self, age, name): self.__age=age self.name=name
def work(self): print(f{self.name} is working.)
def drive(self): print(f{self.name} is driving.)
class Son(Father): def __init__(self, age, name): super().__init__(age, name)
def play(self): print(f{self.name} is playing.)
包a中编写一个类Father,具有属性:年龄(私有)、姓名(公有);具有功能:工作(公有)、开车(公有)。在包a中编写一个子类Son,具有属性:年龄(受保护的)、姓名;具有功能:玩(私有)、学习(公有)。最后在包b中编写主类Test,在主类的main方法中测试类Father与类Son。本文共计247个文字,预计阅读时间需要1分钟。
pythonclass Father: def __init__(self, age, name): self.__age=age self.name=name
def work(self): print(f{self.name} is working.)
def drive(self): print(f{self.name} is driving.)
class Son(Father): def __init__(self, age, name): super().__init__(age, name)
def play(self): print(f{self.name} is playing.)
包a中编写一个类Father,具有属性:年龄(私有)、姓名(公有);具有功能:工作(公有)、开车(公有)。在包a中编写一个子类Son,具有属性:年龄(受保护的)、姓名;具有功能:玩(私有)、学习(公有)。最后在包b中编写主类Test,在主类的main方法中测试类Father与类Son。
