java反射 构造方法_Java反射笔记1——构造函数的反射实现

整理了一下java的反射用法,这是第一部分,针对构造函数的反射。

package com.xxx;

import java.lang.reflect.Constructor;

import java.lang.reflect.InvocationTargetException;

import org.junit.Test;

public class Demo1 {

@Test

public void test1() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException

{

Class c1=Class.forName("com.xxx.SwordBean");

Constructor con= c1.getConstructor();

SwordBean sw=(SwordBean) con.newInstance();

System.out.println(sw.getAttack());

}

@Test

public void test2() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException

{

Class c1=Class.forName("com.xxx.SwordBean");

Constructor con=c1.getConstructor(float.class);

SwordBean sw=(SwordBean)con.newInstance(1200);

System.out.println("Price of this Sword is "+ sw.getPrice());

}

@Test

public void test3() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException

{

Class c1=Class.forName("com.xxx.SwordBean");

Constructor con=c1.getConstructor(float.class,float.class);

SwordBean sw=(SwordBean)con.newInstance(1200,60);

System.out.println("This Sword costs "+ sw.getPrice()+", and attack power is "+sw.getAttack());

}

@Test

public void test4() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException

{

Class c1=Class.forName("com.xxx.SwordBean");

Constructor con=c1.getConstructor(PropertyBean.class);

SwordBean sw=(SwordBean)con.newInstance(new PropertyBean());

PropertyBean pb=sw.getPropertyOfSword();

System.out.println(

"file: "+pb.getFire()+"\n"+

"water: "+pb.getWater()+"\n"+

"wind: "+pb.getWind()+"\n"+

"rock: "+pb.getRock()+"\n"+

"thunder: "+pb.getThunder()+"\n"

);

}

@Test

public void test5() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException

{

Class c1=Class.forName("com.xxx.SwordBean");

Constructor con=c1.getDeclaredConstructor(String.class);

con.setAccessible(true);

SwordBean sw=(SwordBean)con.newInstance("White Slip");

System.out.println("Name of this Sword is "+ sw.getSwordName());

}

}

其中test5是针对的私有构造函数的反射,这里需要注意的是在实例化之前需要设置访问权限。

package com.xxx;

public class SwordBean {

String swordName;

float price;

float Attack;

int sid;

PropertyBean propertyOfSword;

public String getSwordName() {

return swordName;

}

public void setSwordName(String swordName) {

this.swordName = swordName;

}

public float getPrice() {

return price;

}

public void setPrice(float price) {

this.price = price;

}

public float getAttack() {

return Attack;

}

public void setAttack(float attack) {

Attack = attack;

}

private int getSid() {

return sid;

}

private void setSid(int sid) {

this.sid = sid;

}

public PropertyBean getPropertyOfSword() {

return propertyOfSword;

}

public void setPropertyOfSword(PropertyBean propertyOfSword) {

this.propertyOfSword = propertyOfSword;

}

public SwordBean() {

setAttack(1);

}

public SwordBean(float p) {

setPrice(p);

}

public SwordBean(float p, float a) {

setAttack(a);

setPrice(p);

}

public SwordBean(PropertyBean pro) {

setPropertyOfSword(pro);

}

private SwordBean(String Name)

{

setSwordName(Name);

}

}

package com.xxx;

public class PropertyBean {

public int getFire() {

return fire;

}

public void setFire(int fire) {

this.fire = fire;

}

public int getWater() {

return water;

}

public void setWater(int water) {

this.water = water;

}

public int getWind() {

return wind;

}

public void setWind(int wind) {

this.wind = wind;

}

public int getRock() {

return rock;

}

public void setRock(int rock) {

this.rock = rock;

}

public int getThunder() {

return thunder;

}

public void setThunder(int thunder) {

this.thunder = thunder;

}

int fire;

int water;

int wind;

int rock;

int thunder;

public PropertyBean()

{

setFire(0);

setWater(0);

setWind(0);

setRock(0);

setThunder(0);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值