标签:类名.class private 属性 open set sys get exce otf
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 + "]"; } }
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); } }
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); } }
标签:类名.class private 属性 open set sys get exce otf
原文地址:http://www.cnblogs.com/ou-pc/p/7168327.html