您的位置:首页 > 编程语言 > Java开发

Java的内部类和匿名内部类的使用规则

2015-11-16 19:21 429 查看
JAVA 中的内部类:

内部类可以做到一点就是随意访问外部类的成员变量和方法,包括私有的,而不用生成外部类的对象。具体说到,那么内部类是怎样访问外部类的变量呢?内部类在没有同名成员变量和局部变量的情况下,内部类会直接访问外部类的成员变量,而无需指定Out.this.成员变量,创建内部类的对像的时候用

Out.In in = new Out().new In()

静态内部类:

说白了就是内部类对象只能访问外部类中的静态成员变量或是静态方法,有一定的局限性,对象的构架形式是这样的,可以把Out.In 看做是一个整体 Out.In in = new Out.In (); 这样就可以了。

私有内部类:

只有外部类中的方法才能操作内部类中,例如下面的两个例子

public class Piavate_InClass {
private class Inclass{
private int aa = 10;
public void In(){
System.out.println("我是内部类的方法");
}
}
public static void main(String args []){
Priavate_InClass.Inclass inclass =
new Priavate_InClass().new Inclass();
System.out.println(inclass.aa);
inclass.In();
}
}


public class QianTao {
private  final  int SIZE = 15;
private  int [] IntArry  = new int [SIZE];
public  QianTao(){
for(int i = 0;i<IntArry.length;i++){
IntArry [i] = (int) (Math.random() * SIZE);
}

}
public void printOdd(){
IntOrator intOrator  = new  IntOrator();
while(intOrator.hasNext()){
int returna = intOrator.getNext();
if(returna != -1){
System.out.print(returna+" ");
}
}
}
private  class IntOrator{
private int next = 0;
private boolean hasNext(){
return (next < SIZE-1);
}
public int getNext(){
int returnVlue = IntArry[next++];
if(returnVlue % 2 == 1){
return returnVlue;
}
return -1;
}
}
public static void main(String args []){
QianTao qianTao  = new QianTao();
qianTao.printOdd();
}
}


方法内部类:

在外部方法中写入内部类,然后在外部方法中生成一个对象去调用内部方法,如果想往外部方法中传入参数的话就要使用final关键字,final关键字其实也没什么作用,就是一个特殊的类型。

public class FangFaNeiBuLei {
public void print(final int x){
class SubClass{
private int a = 10;
public void Sub()
{
System.out.println("我是内部类的方法");
System.out.println("a的值是  "+ a);
System.out.println("x的值是  "+ x);
}
}
SubClass class1 = new SubClass();
class1.Sub();
}
public static void main(String args []){
FangFaNeiBuLei neibu = new FangFaNeiBuLei();
neibu.print(4);
}
}


匿名内部类也就是没有名字的内部类

正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写

但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口

abstract class Person {
public abstract void eat();
}

class Child extends Person {
public void eat() {
System.out.println("eat something");
}
}

public class Demo {
public static void main(String[] args) {
Person p = new Child();
p.eat();
}
}


运行结果:eat something

可以看到,我们用Child继承了Person类,然后实现了Child的一个实例,将其向上转型为Per
4000
son类的引用

但是,如果此处的Child类只使用一次,那么将其编写为独立的一个类岂不是很麻烦?

这个时候就引入了匿名内部类

abstract class Person {
public abstract void eat();
}

public class Demo {
public static void main(String[] args) {
Person p = new Person() {
public void eat() {
System.out.println("eat something");
}
};
p.eat();
}
}


运行结果:eat something

可以看到,我们直接将抽象类Person中的方法在大括号中实现了

这样便可以省略一个类的书写

并且,匿名内部类还能用于接口上

实例3:在接口上使用匿名内部类

interface Person {
public void eat();
}

public class Demo {
public static void main(String[] args) {
Person p = new Person() {
public void eat() {
System.out.println("eat something");
}
};
p.eat();
}
}


运行结果:eat something

由上面的例子可以看出,只要一个类是抽象的或是一个接口,那么其子类中的方法都可以使用匿名内部类来实现

最常用的情况就是在多线程的实现上,因为要实现多线程必须继承Thread类或是继承Runnable接口

实例4:Thread类的匿名内部类实现

public class Demo {
public static void main(String[] args) {
Thread t = new Thread() {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}
}
};
t.start();
}
}


运行结果:1 2 3 4 5

实例5:Runnable接口的匿名内部类实现

public class Demo {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}
}
};
Thread t = new Thread(r);
t.start();
}
}


运行结果:1 2 3 4 5
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息