• 15.3反射的应用------ 取得类的结构(血干JAVA系类)


    在这里插入图片描述

    【例 15.6] Person.java

    package file;
    
    import java.lang.reflect.Constructor;
    interface China
    {
    	public static final String NATIONAL = "China";
    	public static final String AUTHOR = "李华";
    	public void sayChina();
    	public String sayHello(String name,int age);
    }
    
    class Person implements China
    {
    	private int age;
    	private String name;
    	public Person()
    	{
    		
    	}
    	public Person(String name)
    	{
    		this.setName(name);
    	}
    	public Person(String name,int age)
    	{
    		this.setAge(age);
    		this.setName(name);
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return this.age;
    	}
    	public String getName() {
    		return this.name;
    	}
    	public void sayChina()
    	{
    		System.out.println("作者:"+ AUTHOR +",国籍:" + NATIONAL);
    	}
    	public String sayHello() 
    	{
    		return "你好,"+this.name+"!我的年龄是"+this.age;
    	}
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    15.3.1取得所实现的全部接口

    在这里插入图片描述

    【例15.7]取得Person类实现的全部接口

    Class<?> inter[] = c.getInterfaces();
    inter[i].getName()
    
    • 1
    • 2
    public class demo {
    
    	public static void main(String[] args) 
    	{
    		Class<?> c = null;
    		try 
    		{
    			c = Class.forName("file.Person");//实例化Class
    		} 
    		catch (ClassNotFoundException e) 
    		{
    			e.printStackTrace();
    		}
    		Class<?> inter[] = c.getInterfaces();
    		for(int i=0;i<inter.length;i++)
    		{
    			System.out.println("实现的全部接口:"+inter[i].getName());
    		}
    		
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    15.3.2取得父类

    在这里插入图片描述

    【例15.8]取得Person的父类

    Class<?> parentClass = c.getSuperclass();
    System.out.println(parentClass.getName());
    
    • 1
    • 2
    public class demo {
    
    	public static void main(String[] args) 
    	{
    		Class<?> c = null;
    		try 
    		{
    			c = Class.forName("file.Person");//实例化Class
    		} 
    		catch (ClassNotFoundException e) 
    		{
    			e.printStackTrace();
    		}
    		Class<?> parentClass = c.getSuperclass();
    		System.out.println(parentClass.getName());
    		
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    15.3.3取得全部构造方法

    在这里插入图片描述

    【例15.9】取得Person类中的全部构造方法

    
    public class demo {
    
    	public static void main(String[] args) 
    	{
    		Class<?> c = null;
    		try 
    		{
    			c = Class.forName("file.Person");//实例化Class
    		} 
    		catch (ClassNotFoundException e) 
    		{
    			e.printStackTrace();
    		}
    		Constructor<?> cons[] = c.getConstructors();
    		for(int i=0;i<cons.length;i++)
    		{
    			System.out.println("构造方法:"+cons[i]);
    		}
    		
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

    在这里插入图片描述

    【例15.10】取得一个类的全部构造方法

    public class demo {
    
    	public static void main(String[] args) 
    	{
    		Class<?> c = null;
    		try 
    		{
    			c = Class.forName("file.Person");//实例化Class
    		} 
    		catch (ClassNotFoundException e) 
    		{
    			e.printStackTrace();
    		}
    		Constructor<?> cons[] = c.getConstructors();
    		for(int i=0;i<cons.length;i++)
    		{
    			System.out.print("构造方法:"+ cons[i].getModifiers()+" "+cons[i].getName());
    			System.out.print("(");
    			Class<?> params[] = cons[i].getParameterTypes();//参数类型 
    			for(int j=0;j<params.length;j++)
    			{
    				//参数类型的名称
    				System.out.print(params[j].getName()+" args"+i);
    				if(j<params.length-1)
    				{
    					System.out.print(",");
    				}
    			}
    			System.out.println(")");
    		}
    		
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    【例15.11】使用Modifier还原修饰符

    int mo = cons[i].getModifiers();
    Modifier.toString(mo)
    
    • 1
    • 2
    public class demo {
    
    	public static void main(String[] args) 
    	{
    		Class<?> c = null;
    		try 
    		{
    			c = Class.forName("file.Person");//实例化Class
    		} 
    		catch (ClassNotFoundException e) 
    		{
    			e.printStackTrace();
    		}
    		
    		Constructor<?> cons[] = c.getConstructors();
    		for(int i=0;i<cons.length;i++)
    		{
    			int mo = cons[i].getModifiers();
    			System.out.print("构造方法:"+ Modifier.toString(mo)+" "+cons[i].getName());
    			System.out.print("(");
    			Class<?> params[] = cons[i].getParameterTypes();//参数类型 
    			for(int j=0;j<params.length;j++)
    			{
    				//参数类型的名称
    				System.out.print(params[j].getName()+" args"+i);
    				if(j<params.length-1)
    				{
    					System.out.print(",");
    				}
    			}
    			System.out.println(")");
    		}
    		
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    15.3.4取得全部方法

    在这里插入图片描述

    在这里插入图片描述

    【例15.12】取得一个类的全部方法定义

    //		修饰符:			int mo = m[i].getModifiers();
    //						String str = Modifier.toString(mo);
    //		返回类型: 			Class<?> returnType = m[i].getReturnType();
    //		方法名称:			m[i].getName()
    //		参数类型: 			Class<?> params[] = m[i].getParameterTypes();
    //		异常:			Class<?> exception[] = m[i].getExceptionTypes();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    public class demo {
    
    	public static void main(String[] args) 
    	{
    		Class<?> c = null;
    		try 
    		{
    			c = Class.forName("file.Person");//实例化Class
    		} 
    		catch (ClassNotFoundException e) 
    		{
    			e.printStackTrace();
    		}
    		
    		Method m[] = c.getMethods();
    //		修饰符:			int mo = m[i].getModifiers();
    //						String str = Modifier.toString(mo);
    //		返回类型: 			Class<?> returnType = m[i].getReturnType();
    //		方法名称:			m[i].getName()
    //		参数类型: 			Class<?> params[] = m[i].getParameterTypes();
    //		异常:			Class<?> exception[] = m[i].getExceptionTypes();
    		
    		for(int i=0;i<m.length;i++)
    		{
    //			修饰符:public final native
    			int mo = m[i].getModifiers();
    			String str = Modifier.toString(mo);
    			System.out.print(str+" ");
    //			返回类型:void 
    			Class<?> returnType = m[i].getReturnType();
    			System.out.print(returnType.getName()+" ");
    //			方法名称:java.lang.Object.wait
    			System.out.print(m[i].getName());
    //			参数类型:long 
    			System.out.print("(");
    			Class<?> params[] = m[i].getParameterTypes();
    			for(int j=0;j<params.length;j++)
    			{
    				System.out.print(params[j].getName() + " " + "args" + j);
    				if(j<params.length-1) 
    				{
    					System.out.print(",");
    				}
    			}
    //			异常:throws java.lang.InterruptedException			
    			Class<?> exception[] = m[i].getExceptionTypes();
    			if(exception.length>0)System.out.print(") throws ");
    			else System.out.print(")");
    			
    			for(int j=0;j<exception.length;j++)
    			{
    				System.out.print(exception[j].getName());
    				if(j<exception.length-1)System.out.print(",");
    			}
    			System.out.println();
    		}	
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    在这里插入图片描述
    在这里插入图片描述

    15.3.5取得全部属性

    在这里插入图片描述
    在这里插入图片描述

    			//返回类型
    			Class<?> type =  f[i].getType();
    			//修饰符
    			int mo = f[i].getModifiers();
    			String str = Modifier.toString(mo);
    			
    
    			//		父类&&接口
    			Field f[] = c.getFields();	
    			//			本类
    			Field f[] = c.getDeclaredFields();	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    public class demo {
    
    	public static void main(String[] args) throws Exception
    	{
    		Class<?> c = null;
    		try 
    		{
    			c = Class.forName("file.Person");//实例化Class
    		} 
    		catch (ClassNotFoundException e) 
    		{
    			e.printStackTrace();
    		}
    		
    //		##########################################################
    		{
    //		父类&&接口
    		Field f[] = c.getFields();
    		for(int i=0;i<f.length;i++)
    		{
    			//返回类型
    			Class<?> type =  f[i].getType();
    			//修饰符
    			int mo = f[i].getModifiers();
    			String str = Modifier.toString(mo);
    			
    			System.out.println("父类&接口:"+str+" "+type.getName()+" "+f[i].getName());
    		}
    		}
    	
    //		##########################################################		
    		System.out.println("-----------------------------");
    		{
    			//			本类
    			Field f[] = c.getDeclaredFields();
    			for(int i=0;i<f.length;i++)
    			{
    				//				修饰符
    				int mo = f[i].getModifiers();
    				String str = Modifier.toString(mo);
    				//				类型
    				Class<?> type =  f[i].getType();
    				
    				System.out.println("本类:"+ str +" "+type.getName()+" "+f[i].getName());
    			}
    		}
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    在这里插入图片描述

  • 相关阅读:
    MySQL的多表操作-外键约束
    拓展虚拟世界边界,云手机可以做到吗
    Jmeter+Ant+Git+Jenkins持续集成介绍
    27-Openwrt rtc htpdate system
    greenhills compiler 2021.1.4 for x86 Linux
    为什么 glBegin 未被定义 & 未定义的标识符,使用新的 API(LearnOpenGL P2)
    22.0 Pycharm中编写js代码
    第十八章《JDBC》第1节:JDBC简介
    Verilog的时间系统任务----$time、$stime、$realtime
    解决方案 | 法大大电子签助力融资租赁突围数字化
  • 原文地址:https://blog.csdn.net/qq_52384627/article/details/125434336