内部类,静态内部类,全局内部类
局部内部类
匿名内部类
/**
* 内部类的访问规则:
* 1.内部类可以直接访问外部类中的成员,包括私有
* 之所以可以直接访问外部类中的成员,是因为内部类中持有一个外部类的引用, 格式为 外部类名.this.成员
* 2.外部类要访问内部类,必须建立对象
*
*访问格式:
*当内部类定义在外部类的成员位置上,而且非私有, 可以在外部其他类中可以直接建立内部类对象.
*格式:
* 外部类名.内部类名 变量名=外部类对象.内部类对象;
* Outer.Inner in=new Outer().new Inner();
*2,当外部类在成员位置上,就可以被成员修饰符所修饰
* 比如: private : 将外部类中的内部类进行封装
* static : 内部类就具备static 特性 ,只能访问 外部类的static成员.出现访问局限
* (静态内部类: 一般是 应用的数据全是 静态的数据的时候就可以定了)
*
*3.其他外部类中怎么访问 静态内部类的非静态成员
* new Outer.Inner2().function();
*
*4.其他外部类中怎么访问静态内部类中的静态成员.
* Outer.Inner2.function1();
*
* 注意 : 当内部类中定义了静态成员,该内部类必须是static的
* 当外部类中的静态方法访问内部类时候,该内部类也必须是static的
*
*/
class Outer{
private int x=3;
private static int y=5;
//外部类中 访问内部类中的成员
void method(){
Inner in=new Inner();
in.funtion();
}
//放在一个内的内部
class Inner{
int x=4;
//可以直接访问外部类中的成员
void funtion(){
int x=6;
System.out.println("内部类:"+x); // 6
System.out.println("内部类:"+this.x); // 4
System.out.println("内部类:"+Outer.this.x); // 3
}
/* 非静态内部类中不能有 静态成员
static int y=8;
static void function1(){
System.out.println("内部类:"+x);
}*/
}
private class Inner1{
int x=4;
void function(){
int x=6;
System.out.println("内部类:"+x); // 6
System.out.println("内部类:"+this.x); // 4
System.out.println("内部类:"+Outer.this.x); // 3
}
}
static class Inner2{
//静态内部类的 非静态成员
void function(){
System.out.println("静态内部类:"+y);
}
//静态内部类的静态成员
static void function1(){
System.out.println("静态内部类:"+y);
}
}
//外部类中的静态成员
public static void method1(){
//访问 该外部类中的静态内部类静态方法
Inner2.function1();
//new Inner1().function(); //报错 因为 Inner1 不是静态内部类
}
}
public class InnerClassDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Outer out=new Outer();
out.method();
//如果直接访问内部类中的额function
Outer.Inner in=new Outer().new Inner();// 外部类对象 .new 内部类对象
in.funtion();
//静态内部类中的非静态成员
Outer.Inner2 in2=new Outer.Inner2();
in2.function();
new Outer.Inner2().function();
//静态内部类 中的静态成员
Outer.Inner2.function1();
}
}
/**
* 当描述事物时, 事物的内部还有事物,该事物用内部类来描述
* 因为内部事务在使用外部事务的内容.
* 比如 身体 和 心脏 的关系
*/
class Body{
private class XinZang{ //之所以私有 , 是不让任何人访问
int jump=270; //跳动的次数
int speed=80; //血流速度
void show(){
System.out.println("心跳:"+jump+";血流速度:"+speed+"脉");
}
}
public void seeXinZang(){ // 一个心脏病人 要看医生
//里面可以加入判断 是不医生? 身体是不是有病? 有:就给他们看 没有:就不给 等等
new XinZang().show();
}
//比如还可以 是否 被女朋友踹了? 是: 就伤心 不是: 就开心 哈哈
}
局部内部类
/**
* 内部类定义在局部时候
* 1.不可以被成员修饰符修饰 (private static 等)
* 2.可以直接访问外部类中的成员,因为还持有外部类中的引用
* 但是不可以访问它所在的局部中的变量, 只能访问被final修饰的局部变量.
*/
class Outer{
int x=3;
void method(final int k){
int x=4;
final int y=5;
class Inner{ //局部的内部类
void function(){
System.out.print(Outer.this.x);
System.out.print(y);
System.out.print(k);
}
}
new Inner().function();
}
}
public class InnerClassDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Outer out=new Outer();
out.method(7);
out.method(8);
}
}
匿名内部类
/**
* 匿名内部类
* 1.其实就是一个内部类的简写格式
* 2.定义匿名内部类的前提
* 内部类必须是继承一个类或者实现接口
* 3.匿名内部类的格式
* new 父类或者接口(){定义子类的内容}.成员
*
* 4.其实匿名内部类就是一个匿名子类对象.而且这个对象有点胖.
* 5.匿名内部类中定义方法 最好不要超过3个.
*
*/
abstract class AbsDemo{
abstract void show();
}
class Outer{
int x=3;
class Inner extends AbsDemo{
void show(){
System.out.println("show"+Outer.this.x);
}
}
public void function(){ //不采用匿名内部类
new Inner().show();
}
//采用 匿名内部类实现
public void function1(){
//这就是匿名内部类
new AbsDemo() { //如果只用一次
@Override
void show() { //需要先实现 抽象类中的方法
System.out.println("show"+Outer.this.x);
}
}.show();
new AbsDemo() { //如果只用一次
@Override
void show() { //需要先实现 抽象类中的方法
System.out.println("show"+Outer.this.x);
}
void method(){ //子类特有的
System.out.println("method:"+Outer.this.x);
}
}.method();
//发现 要进行多方法调用的时候 每次都要创建一个对象
}
}
public class AnonymousInner {
public static void main(String[] args) {
new Outer().function();
}
}
/***
* 题目 补足代码 采用匿名内部类完成
*/
interface Inter{
void method();
}
class Test{
//补足代码 采用匿名内部类完成
static Inter function(){
return new Inter(){
public void method(){
System.out.println("method show run");
}
};
}
}
class AnonymousInnerTest{
public static void main(String[] args){
//根据这句话 在上面补足代码
Test.function().method();
//Test.function(): Test类中有一个静态方法 function
//.method():function运算后返回的结果对象是 一个Inter对象,因为只有 Inter采用metho方法
}
}
/**
* 面试题:如果没有接口 没有 父类 请写一个 匿名抽象类调用一个方法
*/
class InnerTest{
public static void main(String [] args){
new Object(){
public void function(){
System.out.println("function run");
}
}.function();
}
}