java_内部类

来源:互联网 发布:淘宝天刀代购 编辑:程序博客网 时间:2024/05/16 01:14
------- android培训、java培训、期待与您交流! ----------

将一个类定义在另一个类的里面,对里面那个类就称为内部类(内置类,嵌套类)。
·访问的特点:
1,内部类可以直接访问外部类中的成员,包括私有成员。之所以你可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,格式:外部类.this。
2,外部类要访问的内部类中的成员,必须要建立内部类的对象。

访问格式:
1,当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中。
可以直接建立内部类对象。
格式
外部类名.内部类名 变量名=new 外部类对象().new 内部类对象();
2,当内部类在成员位置上,就可以被成员修饰符所修饰。
比如 private:将
static:
当内部类被静态修是后只能直接访问外部类中的static成员,出现了访问局限。

在外部其他类中,如何直接访问静态类的非静态成员?
new Outer.Inner().function();

在外部其他类中,如何直接访问静态类的静态成员?
Outer.Inner().function();

注意:当内部类中定义了静态成员该内部类必须是static的。
当外部类中的静态方法访问内部类时,内部类也必须是静态的。

class Outer
{
private int x=3;

class Inner
{
  int x=4;
  void function()
  {
   int x=5;
   System.out.println("inner:"+x);
   System.out.println("inner:"+this.x);
   System.out.println("inner:"+Outer.this.x);
  }
}
void method()
{
  Inner i=new Inner();
  i.function();
}

}


class InnerClassDemo
{
public static void main(String[] args)
{
  Outer out=new Outer();
  out.method();
  System.out.println();
  //直接访问内部类中的成员。
  Outer.Inner in=new Outer().new Inner();
  in.function();
}
}
````````````````````````````````
例:在外部其他类中,直接访问静态类的非静态成员

class Outer
{
static private int x=3;

static class Inner
{
  void function()
  {
   System.out.println("inner:"+x);
  }
}

}

class InnerClassDemo
{
public static void main(String[] args)
{
  new Outer.Inner().function();
}
}


````````````````````````````````
例:在外部其他类中,直接访问静态类的静态成员

class Outer
{
static private int x=3;

static class Inner
{
  static void function()
  {
   System.out.println("inner:"+x);
  }
}

}

class InnerClassDemo
{
public static void main(String[] args)
{
  Outer.Inner().function();
}
}

-------------------------------

内部类定义原则

当描述事物时,事物的内部还有事物,该事物用内部类来描述。
因为内部事物在使用外部事物的内容。


--------------------------------


方法里的内部类不能使用static和private,因为这各类在局部
,不是成员。

内部类定义在局部

内部类定义在局部时,不可以被成员修饰符修饰
2,可以直接外部类中的成员,因为还持有外部类中的引用。
但是不可以访问他所在的局部中的变量。只能访问被final修饰的局部变量。

class Outer{
int x=3;
void method(final int a){
  final int y=4;
  class Inner{
   void function(){
    System.out.println(x);
    System.out.println(y);
    System.out.println(a);
   }
  }
  new Inner().function();
}

}

class InnerClassDemo
{
public static void main(String[] args)
{
  Outer out=new Outer();
  out.method(5);
  System.out.println();
  out.method(7);
}
}


匿名内部类:
1,匿名内部类其实就是内部类的简写格式。
2,定义匿名内部类的前提:
内部类必须是继承一个类或者实现接口。
3,匿名内部类的格式:new 父类或者接口(){定义子类的内容}
4,其实匿名内部类就是一个匿名子类对象。可以理解为带内容的对象。
5,匿名内部类中定义的方法最好不要超过3个。

弊端:
不能调用自己的方法。
不能做强转动作。


abstract class AbsDemo
{
abstract void show();
abstract void show1();
}

class Outer{

int x=3;
/*
class Inner extends AbsDemo
{
  void show()
  {
   System.out.println("x=="+x);
  }
}
*/
public void function()
{
  //new Inner().show();
  /*
  new AbsDemo()
  {
   void show()
   {
    System.out.println("x=="+x);
   }
  }.show();
  */
  AbsDemo abs=new AbsDemo()
  {
   void show()
   {
    System.out.println("x=="+x);
   }
   void show1()
   {
    System.out.println("show1");
   }
   void haha()
   {
    System.out.println("haha");
   }
  };
  abs.show();
  abs.show1();
  //abs.haha();//编译失败,父类中没有这个方法。
 
}
}


class InnerClassDemo
{
public static void main(String[] args)
{
  Outer out=new Outer();
  out.function();
}
}

···············
练习

interface Inter
{
void method();
}
class Test
{
//补足代码
}
class InnerClassTest
{
public static void main(String[] args)
{
  Test.function().method();
  /*这句话的解析:
  Test.:Test类中有静态成员。
  function():Test类中有一个静态的方法function。
  .method():function这个方法运算后的结果是一个对象。而且是一个Inner类型的对象。因为只有是Inter类型的对象,才可以调用method方法。
  完整写法:
   Inter in=Test.function();
   in.method();
  */
}
}


`````````````````````
用内部类实现

interface Inter
{
void method();
}
class Test
{
static class Zi implements Inter
{
  public void method()
  {
   System.out.println("method");
  }
}
static Inter function()
{
  return new Zi();
}
}
class InnerClassTest
{
public static void main(String[] args)
{
  Test.function().method();
}
}

`````````````````````````````````
用匿名内部类实现

interface Inter
{
void method();
}
class Test
{
static Inter function()
{
  return new Inter()
  {
   public void method()
   {
    System.out.println("method");
   }
  };
}
}
class InnerClassTest
{
public static void main(String[] args)
{
  Test.function().method();
}
}

````````````````````
匿名内部类做参数

interface Inter
{
void method();
}
class Test
{
static Inter function()
{
  return new Inter()
  {
   public void method()
   {
    System.out.println("method");
   }
  };
}
}
class InnerClassTest
{
public static void main(String[] args)
{
  show(new Inter()
  {
   public void method()
   {
    System.out.println("method show run");
   }
  });
}
public static void show(Inter in)
{
  in.method();
}
}


class InnerTest
{
public static void main(String[] args)
{
  new Object()
  {
   public void function()
   {
    System.out.println("object");
   }
  }.function();
}
}

------- android培训、java培训、期待与您交流! ----------
原创粉丝点击