Java内部类与匿名内部类

- 2 分钟前

内部类

在一个类中定义另一个类,这个类就称作内部类。

内部类与外嵌类的关系

例子

public class RedCowForm {
    static String formName;
    RedCow cow;//内部类对象
    RedCowForm(){}
    RedCowForm(String s){
        cow = new RedCow(150,112,5000);
        formName = s;
    }
    public void showCowMess(){
        cow.speak();
    }
    class RedCow {
        String cowName = "RedCow";
        int height,weight,price;
        RedCow(int height,int weight,int price){
            this.height = height;
            this.weight = weight;
            this.price = price;
        }
        void speak(){
            System.out.println("I'm " + cowName + ", My height is "
                    + height + ", and my weight is" + weight + "kg,live in " + formName);
        }
    }//内部类结束
}//外嵌类结束

public class InnerTest {
    public static void main(String[] args){
        RedCowForm redCowForm = new RedCowForm("RedCow Farm");
        redCowForm.showCowMess();
        redCowForm.cow.speak();
    }
}
/*运行结果*/
//I'm RedCow, My height is 150, and my weight is112kg,live in RedCow Farm
//I'm RedCow, My height is 150, and my weight is112kg,live in RedCow Farm

注:内部类对应的字节码文件的名字格式为外嵌类名$内部类名

匿名内部类

一个子类或接口去掉类声明之后的类体,称作匿名类。

注:匿名类是一个内部类,不能用匿名类声明对象,但是可以直接用匿名类创建一个对象。

使用场景

特点

使用格式

//假设Bank是类,下面就是用Bank的一个子类创建对象
new Bank(){
//匿名类的类体
};
//假设Computable是一个接口,下面就是用实现了Computable接口的类创建对象

new Computable(){
//实现接口的匿名类的类体
}

例子

/*抽象类OutputAlphabet方法*/
abstract class OutputAlphabet {
    public abstract void output();
}
/**
 * OutputEnglish是OutputAlphabet的一个子类
 */
public class OutputEnglish extends OutputAlphabet{//输出英文字母的子类
    @Override
    public void output() {
        for(char c='a' ;c <= 'z' ;c++){
            System.out.printf("%2c",c);
        }
    }
}

public class ShowBoard {
    void showMess(OutputAlphabet show){
        show.output();//参数show是OutputAlphabet类型的对象
    }
}

public class AnonymousTest {
    public static void main(String[] args){
        ShowBoard showBoard = new ShowBoard();
        showBoard.showMess(new OutputEnglish());//向参数传递OuputAlphabet的子类OutputEnglish对象
        showBoard.showMess(new OutputAlphabet() {//向参数传递OuputAlphabet的匿名子类的对象
            @Override
            public void output() {
                for(char c = 'α' ; c <= 'ω' ;c++){
                    System.out.printf("%2c",c);
                }
            }
        });
    }
}
//运行结果: a b c d e f g h i j k l m n o p q r s t u v w x y z α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ ς σ τ υ φ χ ψ ω
/**
 * 继承自接口的匿名类
 */
public interface SpeakHello {
    void speak();
}
class HelloMachine{
    public void turnOn(SpeakHello speakHello){
        speakHello.speak();
    }
}

public class InterfaceTest {
    public static void main(String[] args){
        HelloMachine helloMachine = new HelloMachine();
        helloMachine.turnOn(new SpeakHello() {//和接口SpeakHello有关的匿名类
            @Override
            public void speak() {
                System.out.println("Hello Machine!");
            }
        });
    }
}
//运行结果:Hello Machine!
Inger Notes © 2024
rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora qq quora wechat