首页 > 代码库 > Java注解及反射得到注解

Java注解及反射得到注解

技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

Class类的方法

技术分享

技术分享

技术分享

技术分享

f

 

 

 

技术分享
package com.part1;
/**
 * 
 * @author pc
 *
 */
public class Student {
    public int sid;
    public char sex;
    private String sname;
    private String password;
    private String cardno;
    private double money;
    
    public Student() {
    }
    public Student(int sid, String sname, String password, String cardno,
            double money) {
        this.sid = sid;
        this.sname = sname;
        this.password = password;
        this.cardno = cardno;
        this.money = money;
    }
    public int getSid() {
        return sid;
    }
    public void setSid(int sid) {
        this.sid = sid;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getCardno() {
        return cardno;
    }
    public void setCardno(String cardno) {
        this.cardno = cardno;
    }
    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
    
    public char getSex() {
        return sex;
    }
    public void setSex(char sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Student [cardno=" + cardno + ", money=" + money + ", password="
                + password + ", sex=" + sex + ", sid=" + sid + ", sname="
                + sname + "]";
    }
    
    
    

}
student实体类
技术分享
package com.part1;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws ClassNotFoundException, SecurityException, NoSuchFieldException, NoSuchMethodException {
        //1.获取Class对象
        //1.1 第一种获取Class对象方法:对象名.getClass()
        Student stu=new Student(1, "李震东", "123", "3201121990", 47.5);
        Class<Student> cla1=(Class<Student>) stu.getClass();
        System.out.println(cla1);
        
        //1.2 第二种获取Class对象的方法:类名.class;
        Class<Student> cla2=Student.class;
        System.out.println(cla2);
        
        //1.3 第三种获取Class对象的方法:
        Class<Student> cla3=(Class<Student>) Class.forName("com.part1.Student");
        System.out.println(cla3);
        
        //2.获取属性
        //2.1先获取所有public属性
        System.out.println("--------2.1先获取所有public属性---------");
        Field[] fields1=cla1.getFields();
        for (Field field : fields1) {
            System.out.println(field);
        }
        //2.2 获取某个属性
        System.out.println("---------2.2获取某个属性---------");
        Field field2 =cla2.getField("sex");
        System.out.println(field2);
        
        //3.获取方法
        //3.1 获取所有的public方法
        System.out.println("------3.1 获取所有的public方法------");
        Method[] methods=cla1.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        
        //3.2 获取某个public方法
        System.out.println("---------3.2 获取某个public方法---------");
        Method method2=cla2.getMethod("setSname", String.class);
        System.out.println(method2);
        
        
        
        
        

    }

}
java得到对象属性和方法
技术分享
package com.part1;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws ClassNotFoundException, SecurityException, NoSuchFieldException, NoSuchMethodException {
        //1.获取Class对象
        //1.1 第一种获取Class对象方法:对象名.getClass()
        Student stu=new Student(1, "李震东", "123", "3201121990", 47.5);
        Class<Student> cla1=(Class<Student>) stu.getClass();
        System.out.println(cla1);
        
        //1.2 第二种获取Class对象的方法:类名.class;
        Class<Student> cla2=Student.class;
        System.out.println(cla2);
        
        //1.3 第三种获取Class对象的方法:
        Class<Student> cla3=(Class<Student>) Class.forName("com.part1.Student");
        System.out.println(cla3);
        
        //2.获取属性
        //2.1先获取所有public属性
        System.out.println("--------2.1先获取所有public属性---------");
        Field[] fields1=cla1.getFields();
        for (Field field : fields1) {
            System.out.println(field);
        }
        //2.2 获取某个属性
        System.out.println("---------2.2获取某个属性---------");
        Field field2 =cla2.getField("sex");
        System.out.println(field2);
        
        //3.获取方法
        //3.1 获取所有的public方法
        System.out.println("------3.1 获取所有的public方法------");
        Method[] methods=cla1.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        
        //3.2 获取某个public方法
        System.out.println("---------3.2 获取某个public方法---------");
        Method method2=cla2.getMethod("setSname", String.class);
        System.out.println(method2);
        
        
        
        
        

    }

}
java动态调用赋值

 

Java注解及反射得到注解