内部类概念

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;
} }
}

  

 

最新文章

  1. ionic配置 问题小记
  2. .NET 获取类型中的属性
  3. UPDATE INNER JOIN 两表联合更新
  4. Windows下: RubyMine + Ruby On Rails + mysql 搭建开发环境
  5. php的cookie和session相同主域名共享
  6. Asp.net Mvc4 基于Authorize实现的模块权限验证方式
  7. git学习相关资料
  8. BOOL布尔类型
  9. .Net下实现可扩展的编程方法简述
  10. 微信公共服务平台开发(.Net 的实现)9-------处理二维码
  11. ios蓝牙开发(三)app作为外设被连接的实现
  12. linux下主机用户管理(完整详情)
  13. EventBus InMemory 的实践基于eShopOnContainers (二)
  14. (82)Wangdao.com第十六天_JavaScript 异步操作
  15. Python&#160;&#39;xxx&#39;&#160;codec&#160;can&#39;t&#160;decode&#160;byte&#160;xxx常见编码错
  16. Confluence 6 数据库表-集群(Clustering)
  17. python3获得命令行输入的参数
  18. EXC_BAD_ACCESS错误
  19. day14 多态与抽象
  20. 原生+H5开发之:Android webview配置

热门文章

  1. ubuntu下搜狗输入法待选框中文乱码问题解决(重启搜狗输入法)
  2. BZOJ3339&amp;&amp;3585 Rmq Problem&amp;&amp;mex
  3. Leetcode476.Number Complement数字的补数
  4. spring源码学习之容器的扩展(一)
  5. TZ_02MyBatis_一级缓存和二级缓存
  6. 华为 Mate8 Emui 5.0 安卓 7.0 root 记录
  7. CSS min-height 属性
  8. 利用Microsoft.VisualBasic中TextFieldParser解析器把CSV格式倒入数据库
  9. 前端怎么传一个map给JAVA
  10. 从0开始学习 GitHub 系列之「02.加入 GitHub」