我们一般使用的java内部类有4种形式:一般内部类、局部内部类、匿名内部类、静态内部类。以下是我作的一个测试,以说明各种内部类的特性。
有关内部类的特性,代码中有详细说明,如下。
public class InterObj {
private int i=8;
private InterA ia=null;
public InterObj(){
ia=new InterA();
}
private int getI(){
return i;
}
public void p(){
pi();
pp();
ppp();
pppp();
}
class InterA{
private int ia=9;
private void pA(){
System.out.println("this is InterA.pA: ia="+ia+",InterObj.i="+getI());
}
public void pA1(){
System.out.println("this is InterA.pA1: ia="+ia+",InterObj.i="+getI());
}
}
public void pi(){
final int s=5;
final Test test=new Test();
class InterB{
private int ib=7;
private void pB(){
System.out.println("this is InterB.pB: ib="+ib+
",(Method)pi.s="+s+",Test.t="+test.getT());
}
}
InterB ib=new InterB();
//此处改变了被局部内部类引用了的Test test的内部状态。
//结果调用ib.pB()时,输出的就是改变后的值100
test.setT(100);
ib.pB();
}
static class InterC{
private int ic=6;
private void pC(){
System.out.println("this is InterC.pC: ic="+ic);
}
}
public void pp(){
InterA ia=new InterA();
ia.pA();
ia.pA1();
InterC ic=new InterC();
ic.pC();
//局部内部类,只在方法内部可见
//InterB ib=new InterB();
}
public static void ppp(){
//InterA ia=new InterA();
//但是可以如下构造:
InterObj iobj=new InterObj();
InterA ia=iobj.new InterA();
ia.pA();
ia.pA1();
InterC ic=new InterC();
ic.pC();
//局部内部类,只在方法内部可见
//InterB ib=new InterB();
}
public void pppp(){
TestInterface tif=new TestInterface(){
public void pppp() {
System.out.println("TestInterface.noName");
}
}
;
tif.pppp();
}
public static void main(String[] args) {
InterObj io=new InterObj();
io.p();
}
}
interface TestInterface{
public void pppp();
}
class Test{
private int t=9;
public int getT(){
return t;
}
public void setT(int t1){
t=t1;
}
}
再分享一则实例:
public class InnerClass {
static Toy toy = new Toy(){
String name = "老吴";
@Override
public void jump()
{
System.out.println(name+"跳出地球");
go();
}
public void go(){
System.out.println("奔跑");
}
}
;
public static void main(String[] args)
{
Person per = new Person("老陈",18);
Person.Computer pc = per.new Computer("外星人");
Person.Computer pc1 = new Person("简自豪",18).new Computer("外星人");
pc.runGame();
pc1.runGame();
Person.Computer1 pc11 = new Person.Computer1("网吧的电脑");
pc11.runGame();
per.useComputer();
String str = "啦啦啦";
//str = "罗库偶偶";
Computer com = new Computer(){
@Override
public void runGame()
{
// TODO Auto-generated method stub
System.out.println(per.age+"岁的"+per.name+"在玩啦啦啦啦啦德玛西塔");
System.out.println(str);
}
}
;
com.runGame();
//具体类的匿名内部类独享
toy.jump();
toy.jump();
//toy.go();
//System.out.println(toy.);
}
}
class Person {
String name;
int age;
static int age1 = 18;
static String name1 = "全职高手";
public Person(String name,int age)
{
super();
this.name = name;
this.age = age;
}
public void playGame(){
System.out.println(name+"玩游戏");
}
public class Computer{
String name;
public Computer(String name)
{
super();
this.name = name;
}
public void runGame(){
System.out.println(name+"运行游戏");
System.out.println(age+"岁的"+Person.this.name+"玩游戏");
}
}
public static class Computer1{
String name;
public Computer1(String name)
{
super();
this.name = name;
}
public void runGame(){
System.out.println(name+"运行游戏");
System.out.println(Person.age1+"的"+Person.name1+"在玩游戏");
}
}
public void useComputer(){
class Computer{
String name;
public Computer(String name)
{
super();
this.name = name;
}
public void runGame(){
System.out.println(name+"运行游戏");
System.out.println(Person.this.age+"的"+Person.this.name+"正在玩游戏");
}
}
Computer com = new Computer("笔记本");
com.runGame();
}
}
public interface Computer {
void runGame();
}
public class Toy {
public void jump(){
System.out.println("玩具跳一下");
}
}
总结
以上就是本文关于java内部测试类代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!



