JAVA外部类和内部类
纪念一个不规范 操作 易错的书写带来的小小bug
起初以为这个类是很少用到的 后来发现因为自己的变成习惯的不好 还是经常发生的。。。
刚开始写的时候,始终记者一个文件一个公有类 所以类中有类的习惯是经常发生的
然后之前是这样的错误:
错误原因:
No enclosing instance of type fuxi is accessible. Must qualify the allocation with an enclosing instance of type fuxi (e.g. x.new A() where x is an instance of fuxi).
无法访问fuxi类型的封闭实例。必须使用包含fuxi类型的实例(例如x)限定分配。新A(),其中x是fuxi的一个实例)。
后修正为:
package fuxi;
/*
(1) 编写Animal接口,接口中声明run() 方法
(2) 定义Bird类和Fish类实现Animal接口
(3) 编写Bird类和Fish类的测试程序,并调用其中的run()方法
*/
public class fuxi {
public interface Animal{
void run();
}
class Bird implements Animal{
public void run() {
System.out.println("鸟不会游泳");
}
}
class Fish implements Animal{
public void run() {
System.out.println("鱼会游泳");
}
}
public fuxi() {
// TODO Auto-generated constructor stub
System.out.println("生成对象时调用构造函数");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
fuxi Fuxi=new fuxi();
Bird bird=Fuxi.new Bird();
bird.run();
Fish fish=Fuxi.new Fish();
fish.run();
}
}