我正在创建以下类层次结构:
abstract class Shape{
protected abstract float getArea();
protected abstract float getVolume();
}
abstract class TwoDimentionalShape extends Shape{
public abstract float getArea();
protected float getVolume(){
return 0;
}
}
class Square extends TwoDimentionalShape {
float width, height;
Square(float w, float h){
width = w;
height = h;
}
public float getArea(){
return width*height;
}
}
public class ShapeTest {
public static void main(String args[]){
Shape s = new Square(3, 4);
System.out.println(s.getVolume());
}
}
我想做的是隐藏类的功能,因为它将用于getVolume()
类。TwoDimentionalShape
ThreeDimentionalShape
问题是我已经将该函数声明为受保护的,但是当我从 调用它时main()
,程序正在运行。为什么会这样?