栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java内部测试类代码详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java内部测试类代码详解

我们一般使用的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内部测试类代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/142618.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号