⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classtest.java

📁 一些Java写的测试API的源代码
💻 JAVA
字号:
/*获取Class实例的三种方式:
 *(1) 利用对象调用getClass()方法获取该对象的Class实例
 *(2) 使用Class类的静态方法forName(),用类的名字获取一个Class实例
 *(3) 运用.class的方式来获取Class实例,对于基本数据类型的封装类,
 *  还可以采用.TYPE来获取相对应的基本数据类型的Class实例。
 *
 */
import java.lang.reflect.*;
class ClassTest
{
	public static void main(String[] args)
	{
		/*
		//(1)
		Point pt1 = new Point();
		Class c1= pt1.getClass();
		
		System.out.println(c1.getName());
		
		//(2)
		try{
		
			Class c2 = Class.forName("Point");
			System.out.println(c2.getName());
		}catch(ClassNotFoundException e)
		{
			e.printStackTrace();	
		}
		//(3)
		
		Class c3 = Point.class;
		System.out.println(c3.getName());
		
		
		Class c4 = int.class;
		System.out.println(c4.getName());
		
		Class c5 = Integer.TYPE;
		System.out.println(c5.getName());
		
		Class c6 = Integer.class;
		System.out.println(c6.getName());
		*/
		
		
		// 在运行期间,如果我们要产生某个类的对象,
		//Java虚拟机会检查该类型的Class对象是否已被加载。
		//如果没有被加载,JVM会根据类的名称找到.class文件并加载它
		//一旦某个类型的Class对象已被加载到内存,就可以
		//用它来产生该类型的所有对象,
		//
		
		
		System.out.println("before new Point()");
		new Point(1,1);
		System.out.println("after new Point()");
		
		try
		{
			Class.forName("Line");
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
		
		//在不知道类的的名字情况下,创建一个类的实例
		//可以用newInstance();
		if(args.length!=1)
		{
			return;
		}
		try
		{
			Class c=Class.forName(args[0]);
			Point pt = (Point)c.newInstance();
			System.out.println("");
			pt.output();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		System.out.println("");
		
		
		//注意:newInstance()调用类中的缺省的构造方法(即不带参数的)
		//要解决这个问题,可以用反射API
		//Reflection API
		/*1.Determine the class fo an object.
		 *2.Get information about a class's modifiers,fields,methods,
		 *	constructors,and superclasses.
		 *3.Find out what constants and method declarations belong to an 
		 *	interface.
		 *4.Create an instance of a class whose anme is not known until runtime.
		 *5.Get and set the value of an object's field,even if the field name is
		 *	unknown to your program until runtime.
		 *6.Invoke a method on an object,even if the method id not known until runtime.
		 *7.Create a new array,whose size and component type are not known until runtime,
		 *	and then modify the array's components.
		 *
		 */
		 if(args.length != 1)
		 {
		 	return ;
		 }
		 try
		 {
		 	Class c = Class.forName("Point");
		 	Constructor[] cons = c.getDeclaredConstructors();
		 	for (int i=0;i<cons.length;i++)
		 	{
		 		System.out.println(cons[i]);
		 	}
		 	Method[] ms = c.getDeclaredMethods();
		 	for (int i=0;i<ms.length;i++)
		 	{
		 		System.out.println(ms[i]);
		 	}
		 	
		 }
		 catch(Exception e)
		 {
		 	e.printStackTrace();
		 }
	}
}

class Point
{
	Point(int x,int y)
	{
		this.x = x;
		this.y = y;
	}
	static 
	{
		System.out.println("Loading Point....");
	}
	int x,y;
	void output()
	{
		System.out.println("x="+x+","+"y="+y);
	}
}

class Line
{
	static 
	{
		System.out.println("Loading Line....");	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -