The method * from the type * is not visible

一提到访问控制符protected,即使是初学者一般都会很自信的认为自己在这方面的理解没有问题。那好,我们提一个问题出来看看…

问题提出:
   请看下面两端代码,其中包B中的猫和鼠都继承了动物类。

package testa;    
public class Animal {    
    protected void crowl(String c){    
        System.out.println(c);    
    }    
}

代码2:

package testb;
import testa.Animal;

class Cat extends Animal 
{  
    
}  
public class Rat extends Animal{  
    public void crowl(){  
              this.crowl("zhi zhi"); //没有问题,继承了Animal中的protected方法——crowl(String)  
              Animal ani=new Animal();
              ani.crowl("animail jiaojiao"); //wrong, The method crowl(String) from the type Animal is not visible 
              Cat cat=new Cat();  
              cat.crowl("miao miao"); //wrong, The method crowl(String) from the type Animal is not visible  
    }  
}

既然,猫和鼠都继承了动物类,那么在鼠类的作用范围内,看不到猫所继承的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 里就可以访问了。

package com;

import testa.Ani;
class Animail {  
    protected void crowl(String c){  
        System.out.println(c);  
    }  
}  
class Cat extends Animail{  
      
}  
public class Rat extends Animail{  
    public void crowl(){  
                crowl("zhi zhi"); //没有问题,继承了Animal中的protected方法——crowl(String) 
                Animail ani=new Animail();
                ani.crowl("animail jiaojiao");
                Cat cat=new Cat();  
                cat.crowl("miao miao"); //wrong, The method crowl(String) from the type Animal is not visible  
              
    }
    
    public static void main(String[] args){
    	Rat rat=new Rat();
    	rat.crowl();
    }
} 
运行结果:
zhi zhi
animail jiaojiao
miao miao


帮我写出以下java代码:Add a class Bubble that extends Shape. The Bubble class has an instance variable called radius of type double that represents the radius of the bubble. The constructor of the Bubble class takes an x and a y as arguments, which represent the position of the new bubble. The radius of a new bubble is always 10 and never changes after that. The isVisible method indicates whether the bubble is currently visible inside a window of width w and height h (position (0, 0) is in the upper-left corner of the window). The bubble is considered visible if at least one pixel of the bubble is visible. Therefore a bubble might be visible even when its center is outside the window, as long as the edge of the bubble is still visible inside the window. The code of the isVisible method is a little bit complex, mostly because of the case where the center of the circle is just outside one of the corners of the window. So here is the code of the isVisible method, which you can directly copy-paste into your assignment: // Find the point (wx, wy) inside the window which is closest to the // center (x, y) of the circle. In other words, find the wx in the // interval [0, w - 1] which is closest to x, and find the wy in the // interval [0, h - 1] which is closest to y. // If the distance between (wx, wy) and (x, y) is less than the radius // of the circle (using Pythagoras's theorem) then at least part of // the circle is visible in the window. // Note: if the center of the circle is inside the window, then (wx, wy) // is the same as (x, y), and the distance is 0. public boolean isVisible(int w, int h) { double x = getX(); double y = getY(); double wx = (x < 0 ? 0 : (x > w - 1 ? w - 1 : x)); double wy = (y < 0 ? 0 : (y > h - 1 ? h - 1 : y)); double dx = wx - x; double dy = wy - y; return dx * dx + dy * dy <= radius * radius; } The isIn method indicates whether the point at coordinates (x, y) (which are the arguments of the method) is currently inside the bubble or not. The edge of the bubble counts as being inside of the bubble. HINT: use Pythagoras's theorem to compute the distance from the center of the bubble to the point (x, y). The draw method uses the graphics object g to draw the bubble. HINT: remember that the color of the graphics object g is changed in the draw method of the superclass of Bubble. Also add a testBubble method to test all your methods (including inherited methods, but excluding the isVisible method, which I provide, and excluding the draw method since it requires as argument a graphics object g that you
05-22
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值