码迷,mamicode.com
首页 > 其他好文 > 详细

static

时间:2018-07-15 19:46:47      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:style   span   font   错误   void   []   无法   string   分配   

 

 1 /* 
 2 final:
 3     定义: 最终意思,可以修饰类,方法,变量
 4     作用: 在继承过程中,final修饰的方法可以避免被方法重写覆盖掉
 5         
 6  */
 7  
 8  class Fu 
 9  {
10      public final void show(){
11          System.out.println("final修饰的父类方法,不能被继承");
12      }
13  }
14  
15  class Zi extends Fu 
16  {
17      //Zi中的show()无法覆盖Fu中的show()
18      public void show(){
19          System.out.println("final 修饰的父类方法不能被覆盖掉");
20      }
21  }
22  
23 public  class FinalDemo
24 {
25     public static void main(String[] args){
26         Zi zi = new Zi();
27         zi.show();
28     }
29 }

 

 

 

 

 1 /* 
 2 final 修饰类变量方法
 3     1.类: final修饰的类不能被继承
 4     2.方法: final修饰方法不能被重写
 5     3.final修饰成员变量是常量,只能被赋值一次
 6     
 7     
 8 常量:
 9     字面值常量 "hello", 10, true
10     自定义常量: final int x =10;
11  */
12 /* 
13 final class Father
14 {}
15  错误: 无法从最终Father进行继承
16 class Son extends Father 
17 {}
18  */
19  
20 class Father
21 {
22     public int num =10;
23     public final int num2 = 20;
24     /* 
25     public final void show(){
26         System.out.println("final修饰方法不能被重写");
27     }
28      */
29 }
30 
31 class Son extends Father
32 {
33     //final 修饰的方法不能被重写
34     //Son中的show()无法覆盖Father中的show()
35     public void show(){
36         num = 100;
37         System.out.println(num);
38         
39         //无法为最终变量num2分配值
40         //num2 = 2000;
41         System.out.println(num2);
42     }
43 }
44 public class FinalDemo1
45 {
46     public static void main(String[] args){
47         Son son = new Son();
48         son.show();
49     }
50 }

 

static

标签:style   span   font   错误   void   []   无法   string   分配   

原文地址:https://www.cnblogs.com/yu-zhi/p/9314071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!