java调用内部类的方法_JavaSE学习 外部调用内部类的方法(一)

Title:内部类(三)内部类的使用

Time:

Author:weir

内部类:

1-在外部类中使用:使用的方式直接使用内部类类名

2-在外部类中使用;

1-条件:内部类不能使用private修饰,使用对应访问修饰符(public protect default)【详细参考访问修饰表】

2-使用方式:

1-非静态内部类实例:

1-创建外部类对象实例

2-通过外部类对象创建内部类

3-内部类的使用

2-静态内部类实例;class Outterclass {

// 定义内部类,位置任意

private int weight;

private int hight;

public void setter(int w, int h) {

this.weight = w;

this.hight = h;

System.out.println("Here is a setter Function,and i am outter class" + "  w:" + weight + "  h:" + hight);

}

public class inner {

private int inner_var=666;

void info() {

System.out.println("this is a inner class!\nI know your input weight:" + weight

+ "  I know your input hight:" + hight);

}

}

public static class Static_inner{

void test(){

System.out.println("这里直接使用在调用静态内部类的方法!");

}

}

}

public class Demo {

public static void main(String[] args) {

System.out.println("_____________________________________________");

System.out.println("!非静态内部类测试实例!");

//1-创建外部类对象实例

Outterclass out=new Outterclass();

//2-通过外部类对象创建内部类

Outterclass.inner in= out.new inner();

//3-内部类的使用

in.info();

System.out.println("!非静态内部类测试实例的简洁形式!");

Outterclass.inner in1=out.new inner();

in1.info();

System.out.println("_____________________________________________");

System.out.println("!静态内部类测试实例!");

new Outterclass.Static_inner().test();

System.out.println("_____________________________________________");

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值