protected 修辭方法引起的錯誤:The method * from the type * is not visible

一提到訪問控制符protected,即使是初學者一般都會很自信的認爲自己在這方面的理解沒有問題。那好,我們提一個問題出來看看…

問題提出:
   請看下面兩端代碼,其中包B中的貓和鼠都繼承了動物類。

  1. package testa;      
  2. public class Animal {      
  3.     protected void crowl(String c){      
  4.         System.out.println(c);      
  5.     }      
  6. }  

代碼2:

  1. package testb;  
  2. import testa.Animal;  
  3.   
  4. class Cat extends Animal   
  5. {    
  6.       
  7. }    
  8. public class Rat extends Animal{    
  9.     public void crowl(){    
  10.               this.crowl("zhi zhi"); //沒有問題,繼承了Animal中的protected方法——crowl(String)    
  11.               Animal ani=new Animal();  
  12.               ani.crowl("animail jiaojiao"); //wrong, The method crowl(String) from the type Animal is not visible   
  13.               Cat cat=new Cat();    
  14.               cat.crowl("miao miao"); //wrong, The method crowl(String) from the type Animal is not visible    
  15.     }    
  16. }  

既然,貓和鼠都繼承了動物類,那麼在鼠類的作用範圍內,看不到貓所繼承的crowl()方法呢?

癥結所在:
       protected受訪問保護規則是很微妙的。雖然protected域對所有子類都可見。但是有一點很重要,子類只能在自己的作用範圍內訪問自己繼承的那個父類protected,而無法到訪問別的子類(同父類的親兄弟)所繼承的protected域和父類對象的protectedani.crow1()說白了就是:老鼠只能"zhi,zhi"。即使他能看見貓(可以在自己的作用域內創建一個cat對象),也永遠無法學會貓叫
       也就是說,cat所繼承的crowl方法在cat類作用範圍內可見。但在rat類作用範圍內不可見,即使rat,cat是親兄弟也不行。

這就是爲什麼我們在用clone方法的時候不能簡單的直接將對象aObject.clone()出來的原因了。而需要在aObject.bObject=(Bobject)this.bObject.clone();

      總之,當B extends A的時候,在子類B的作用範圍內,只能調用本子類B定義的對象的protected方法(該方法從父類A中繼承而來)。而不能調用其他A類(A 本身和從A繼承)對象的protected方法

另外:同包內均和派生類中可比默認[default|package](只包內能使用) 權限大一點。

如把上面代碼放到同一package 裏就可以訪問了。

  1. package com;  
  2.   
  3. import testa.Ani;  
  4. class Animail {    
  5.     protected void crowl(String c){    
  6.         System.out.println(c);    
  7.     }    
  8. }    
  9. class Cat extends Animail{    
  10.         
  11. }    
  12. public class Rat extends Animail{    
  13.     public void crowl(){    
  14.                 crowl("zhi zhi"); //沒有問題,繼承了Animal中的protected方法——crowl(String)   
  15.                 Animail ani=new Animail();  
  16.                 ani.crowl("animail jiaojiao");  
  17.                 Cat cat=new Cat();    
  18.                 cat.crowl("miao miao"); //wrong, The method crowl(String) from the type Animal is not visible    
  19.                 
  20.     }  
  21.       
  22.     public static void main(String[] args){  
  23.         Rat rat=new Rat();  
  24.         rat.crowl();  
  25.     }  
  26. }   
運行結果:
  1. zhi zhi  
  2. animail jiaojiao  
  3. miao miao 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章