java内部类练习题,学习笔记——Java内部类练习题
1.嘗試在方法中編寫一個匿名內部類。
package com.lzw;
public class AnonymityInnerClass {
}
class OuterClass4{
public OutInterface doit(final String s){
return new OutInterface(){
private int i=0;
public int getValue(){
System.out.println(s);
return i;
}
public void f(){
System.out.println("f()");
}
};
}
}
interface OutInterface { // 定義一個接口
}
匿名內部類:new OutInterface(){
.......
}寫在了外部類Outerclass4的方法中。
2.嘗試將主方法編寫在靜態內部類中。
packagecom.lzw;public classStaticInnerClass {int x=100;static classInner{voiddoitInner(){//System.out.println("外部類"+x);
}public static voidmain(String args[]){
System.out.println("a");
}
}
}
注:靜態內部類的最大特征就是不能使用外部類的非靜態成員。
3.嘗試編寫一個靜態內部類,在主方法中創建其內部類的實例。
package test;
public class StaticInnerClass {
static class a{
void f(){
System.out.println("f()");
}
}
public static void main(String args[]){
StaticInnerClass.a a=new a();
a.f();
}
}
原文:http://www.cnblogs.com/tangzhirong/p/4699855.html
總結
以上是生活随笔為你收集整理的java内部类练习题,学习笔记——Java内部类练习题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【程序员必看】如何用Python从0开始
- 下一篇: java整数翻转_Java程序反转数字