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。package a; public class Father { private int age; public String name; private void play(){ System.out.println("兴趣"); } public void Study(){ System.out.println("学习"); } } package b; import a.Father; public class Son extends Father{ protected int age; public String name; private void play(){ System.out.println("游戏"); } public void Study(){ System.out.println("韩语"); } } import a.Father; public class test { public static void main(String[] args) { Father t=new Father(); Son c=new Son(); //t.play();私有 t.Study(); //c.play();私有 c.Study(); } }
本文共计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。package a; public class Father { private int age; public String name; private void play(){ System.out.println("兴趣"); } public void Study(){ System.out.println("学习"); } } package b; import a.Father; public class Son extends Father{ protected int age; public String name; private void play(){ System.out.println("游戏"); } public void Study(){ System.out.println("韩语"); } } import a.Father; public class test { public static void main(String[] args) { Father t=new Father(); Son c=new Son(); //t.play();私有 t.Study(); //c.play();私有 c.Study(); } }

