java的一个问题..
来源:百度知道 编辑:UC知道 时间:2024/06/25 13:01:01
接口里,定义的常量,能否被实现它的类里面的其它方法所调用.
句子有些长,...嘻嘻
ps:我还有其它问题,欢迎大家来得分啊
how??
举个例子嘛
接口名. 常量 的形式可以吧?
句子有些长,...嘻嘻
ps:我还有其它问题,欢迎大家来得分啊
how??
举个例子嘛
接口名. 常量 的形式可以吧?
楼上是用实现类实例化一个接口,用接口的名义去做事情...接口是不允许被实例化的
interface Animal
{
void eat();
void sleep();
}
class Zoo
{
private class Tiger implements Animal
{
public void eat(){
System.out.println("Tiger eat");
}
public void sleep(){
System.out.println("Tiger sleep");
}
};
Animal getAnimal(){
return new Tiger();
}
};
class TestAnimal
{
public static void main(String[] args)
{
/*Zoo zoo=new Zoo();
Zoo.Tiger tiger=zoo.new Tiger();
tiger.eat();
tiger.sleep();*/
/*Zoo zoo=new Zoo();
Zoo.Tiger tiger=zoo.getTiger();
tiger.eat();
tiger.sleep();*/
/*Zoo zoo=new Zoo();
Animal an=zoo.getAnimal();
an.eat();
an.sleep();*/
}
}
三种注释方式都可以运行,是访问接口的三种方式
能
明显可以啊,也可以被其它与这个接口并不存在依赖关系的类所调用。
但是因为接口中的常量一般定义为fin