java内部类高级_JAVA高级特性--内部类

内部类概念

public class Outer{

class inner{

}

}

特点

可以很好的实现隐藏,可以使用procted,private

可以直接访问外部类的所有成员 ,包括私有成员.

外部内不能访问内部类的成员,必须首先建立内部类的 对象才可访问

成员内部内及应用

//外部内不能访问内部内属性和方法,必须创建对象

//内部类中不能有静态方法和属性

//内部类可以访问Outer.this.name外部内属性

//静态常量在内部类中是可以的

package com.tanlei.newer;

public class MemberInner {

public static void main(String[] args) {

//创建外部内对象

Outer outer=new Outer();

outer.show();

//创建内部类对象

Outer.Inner inner=outer.new Inner();

inner.show();

}

}

class Outer {

private String name = "tanle";

private int num = 10;

public void show() {

System.out.println(name + num);

//外部内不能访问内部内属性和方法,必须创建对象

Inner inner1 =new Inner();

inner1.show();

}

public class Inner {

private String name = "lisi";

private int num = 20;

public void show() {

//内部类中不能有静态方法和属性

//内部类可以访问Outer.this.name外部内属性

//静态常量在内部类中是可以的

System.out.println(Outer.this.name +" 内部类"+ num);

}

}

}

静态内部类

特点:

使用static修饰的成员内部类叫做静态内部类

外部类名.内部类名  实例名=new 外部类名.内部类名(参数).

限制

静态内部类不能与外部类重名

不能访问外部类的非静态的属性和方法,外部类不能访问内部类的非静态 的属性和方法

匿名内部类

特点

没有名称的内部类,没办法引用他们,必须在创建时作为new语句的一部分来声明并创建他们的实例

必须继承一个类或者实现一个接口

语法

new interface接口的实现类/superclass子类(){

//类体

}

package com.pojo;

public class Anoon {

public static void main(String[] args) {

Person person=new Person();

/*Anamill dog=new DOGG();*/

person.feed(new Anamill() {

@Override

public void eat() {

// TODO Auto-generated method stub

System.out.println();

}

} );

}

}

abstract class Anamill{

public abstract void eat();

}

/*class DOGG extends Anamill{

@Override

public void eat() {

System.out.println("啃骨头");

}

}*/

class Person{

public void feed(Anamill anamill) {

anamill.eat();

}

}

局部内部类

特点

定义在代码块,方法体类的

局部内部类不能加访问修饰符

package com.pojo;

public class localInner {

public static void main(String[] args) {

Outer3 outer3=new Outer3();

outer3.showOuter();

}

}

class Outer3{

private String name="zhangs";

private int agw =20;

private static int num=20;

public void showOuter() {

final int mun44=50;//局部内部类只能访问声明其方法的常量

class Inner3{

private int mun3=30;

private int num4=40;

public void showInner() {

System.out.println(mun3);

System.out.println(num4);

System.out.println(Outer3.this.name);

System.out.println(Outer3.num);

System.out.println(mun44);

}

}

Inner3 inner3=new Inner3();

inner3.showInner();

}

}

案例

package com.pojo;

import java.util.Arrays;

import com.pojo.Arraydemao.Entty;

public class Conter {

public static void main(String[] args) {

Arraydemao arraydemao=new Arraydemao();

arraydemao.put("1", "a");

arraydemao.put("2", "b");

arraydemao.put("3", "c");

arraydemao.put("4", "d");

arraydemao.put("5", "e");

Arraydemao.Entty [] entties1=arraydemao.getentties();

for (int i = 0; i < entties1.length; i++) {

Entty entty=entties1[i];

System.out.println(entty.getKey());

System.out.println(entty.getValue());

}

}

}

class Arraydemao{

//存放entry对象的数组,默认大小为5

private Entty[] entties=new Entty[5];

private int count=0;//下标

//对外提供的一个接口向容器中存放封装好的数据

public void put(String key,String value) {

Entty entry=new Entty();

entry.setKey(key);

entry.setValue(value);

entties[count++]=entry;//存放Entry对象到数组中

//数组扩容

if (count>=entties.length) {

System.out.println("容器满了");

//扩容后的新数组的大小

int newCapacity=entties.length*2;

//把老数组中的数据复制到长度为newCapacity薪数组中

entties=Arrays.copyOf(entties,newCapacity);

}

}

//把容器中的有数据内容返回

public Entty[] getentties() {

return Arrays.copyOfRange(entties, 0, count);

}

//把键值对封装在Entry对象中

public static class Entty{

private String key;

private String value;

public String getKey() {

return key;

}

public void setKey(String key) {

this.key = key;

}

public String getValue() {

return value;

}

public void setValue(String value) {

this.value = value;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值