java中outer和inner_JAVA outer(外部)类和Inner(内部)类

JAVA外部类和内部类

纪念一个不规范 操作 易错的书写带来的小小bug

起初以为这个类是很少用到的 后来发现因为自己的变成习惯的不好 还是经常发生的。。。

刚开始写的时候,始终记者一个文件一个公有类  所以类中有类的习惯是经常发生的

然后之前是这样的错误:

31321014cd0127bf1dc3a93fd2ec0bec.png

错误原因:

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();

}

}

44237d306f2a88da30c6f6f110452052.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值