`
263229365
  • 浏览: 465305 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

一个关于java继承的题目

阅读更多

复制来的:

一个关于java继承的题目,跟大家分享下。题目如下: 


有三个类,一父二子: 
父类:

Java代码   收藏代码
  1. public class Father {  
  2.     static {  
  3.         System.out.println("Father static Create");  
  4.     }  
  5.     {  
  6.         System.out.println("Father Create");  
  7.     }  
  8.       
  9.     public static void StaticFunction(){  
  10.         System.out.println("Father static Function");  
  11.     }  
  12.   
  13.     public void Function(){  
  14.         System.out.println("Father Function");  
  15.     }  
  16. }  


子类1

Java代码   收藏代码
  1. public class ChildOne extends Father {  
  2.     static {  
  3.         System.out.println("ChildOne static Create");  
  4.     }  
  5.     {  
  6.         System.out.println("ChildOne Create");  
  7.     }  
  8.       
  9.     public static void StaticFunction(){  
  10.         System.out.println("ChildOne static Function");  
  11.     }  
  12.   
  13. }  


子类2

Java代码   收藏代码
  1. public class ChildTwo extends Father {  
  2.     static {  
  3.         System.out.println("ChildTwo static Create");  
  4.     }  
  5.     {  
  6.         System.out.println("ChildTwo Create");  
  7.     }  
  8.   
  9.     public static void StaticFunction() {  
  10.         System.out.println("ChildTwo static Function");  
  11.     }  
  12.   
  13.     public void Function() {  
  14.         System.out.println("ChildTwo Function");  
  15.     }  
  16. }  


调用方法

Java代码   收藏代码
  1. public class Main {  
  2.     public static void main(String[] args) {  
  3.         Father A = new ChildOne();  
  4.         Father B = new ChildTwo();  
  5.         A.StaticFunction();  
  6.         A.Function();  
  7.         B.StaticFunction();  
  8.         B.Function();  
  9.     }  

 


输出结果:

 

 

Father static Create

ChildOne static Create

Father Create

ChildOne Create

ChildTwo static Create

Father Create

ChildTwo Create

Father static Function

Father Function

Father static Function

ChildTwo Function




 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics