java内部类

来源:互联网 发布:海南网络小贷牌照 编辑:程序博客网 时间:2024/06/04 23:38
/*internal class:  成员内部类、方法内部类(只能在方法内实例化对象,不能访问方法内非最终变量)、静态内部类(不能访问对象属性、成员,只能访问静态属性、成员),不管是什么内部类,最好在内部使用。继承式匿名内部类(重写继承类中的成员方法)、接口式匿名内部类(实现接口中的抽象方法)、参数式匿名内部类*/ public class Test{public static void main(String[] args){//继承式匿名内部类USBPower usbp = new USBPower(){public int capability = 0;public String toString(){return "your string" + capability;}};System.out.println(usbp.toString());//接口式匿名内部类Power p = new Power(){public void charge(){System.out.println("a charge for the use of the telephone");}};p.charge();//参数式匿名内部类action(new Power(){public void charge(){System.out.println("The charge for a front-row seat is $3.");}});}public static void action(Power p){p.charge();}}interface Power{void charge();}class USBPower{}

0 0
原创粉丝点击