• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
何日才能成为大神
博客园    首页    新随笔    联系   管理    订阅  订阅

反射

package cn.itcat.bean;

import java.util.Date;
import java.util.Random;

/**
* 要像对字节码对象进行解剖,必须由字节码文件对象
* 如何获取字节码文件对象?
* @author c_wangmengling-001
*
*/
public class Person {

private static Long randomNumber;
private static Long curIndex = Long.valueOf(0L);


public synchronized static Long getId() throws Exception {
Long index = null;
// 从0到999 curIndex*100 curIndex 100-99900
index = (curIndex = curIndex.longValue() + 1L).longValue() % 1000L;
if (curIndex.longValue() >= 1000L) {
curIndex = 0L;
}
// 随机数 1-10
randomNumber = Long.valueOf(new Random().nextInt(100));
return (new Date()).getTime() * 100000L + index.longValue() * 100L
+ randomNumber.longValue();
}

public static void main(String[] args) throws Exception {
for (int i = 0; i < 10000; i++) {
System.out.println( getId());
}

}

// public static void main(String[] args){
// System.out.println(getStringRandom(18));
// }
//
// public static String getStringRandom(int length) {
//
// String val = "";
// Random random = new Random();
// //参数length,表示生成几位随机数
// for(int i = 0; i < length; i++) {
// String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
// //输出字母还是数字
// if("char".equalsIgnoreCase(charOrNum)){
// //输出是大写字母还是小写字母
// int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
// val += (char)(random.nextInt(26) + temp);
// }else if("num".equalsIgnoreCase(charOrNum)) {
// val += String.valueOf(random.nextInt(10));
// }
// }
// return val;
// }
// private int age;
//
// private String name;
//
// public static String country = "CN";
//
//
//
// public void show() {
// System.out.println(name+"......show run 。。。"+age);
// }
//
// public static void staticMethod() {
// System.out.println("staticMethod......show run 。。。");
// }
//
//
// public Person(int age, String name) {
// super();
// this.age = age;
// this.name = name;
// System.out.println("Person param run... 有参构造函数"+this.age+this.name);
//
// }
//
// public Person() {
// super();
// }
//
// public static void main(String[] args) {
//// Person.staticMethod();
//// Person person = new Person();
//// person.show();
// double s ;
// for (int i = 1; i < 100000; i++) {
// s = 150402198603309000 ;
//
// }
// }
}

 

package cn.itcat.bean;

/**
* 要像对字节码对象进行解剖,必须由字节码文件对象
* 如何获取字节码文件对象?
* @author c_wangmengling-001
*
*/
public class Person2 {

private int age;

private String name;

public int getAge() {
return age;
}

public void show() {
System.out.println(name+"......show run 。。。"+age);
}

private void method() {
System.out.println("method......show run 。。。");
}

public void paraMethod(String str,int num) {
System.out.println("paraMethod......show run 。。。"+str+num);
}

public static void staticMethod() {
System.out.println("staticMethod......show run 。。。");
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Person2(int age, String name) {
super();
this.age = age;
this.name = name;
System.out.println("Person param run... 有参构造函数"+this.age+this.name);

}

// public Person() {
// super();
// System.out.println("Person run... 空参构造函数");
// }

 

}

 

package cn.itcat.bean;

import java.lang.reflect.Constructor;

public class ReflectDemo {

public static void creatNewObject() throws ClassNotFoundException, InstantiationException, IllegalAccessException {

/*
* 早期
* 1、new的时候,现根据被new的类的名称找寻该类的字节码文件,并加载进内存
* 2、并创建该字节码文件对象,并创建该文件的对应的Person对象
*/
// Person person = new Person();

//现在
String name = "cn.itcat.bean.Person";
//找寻该名称类文件,并加载进内存,并产生Class对象
Class clazz = Class.forName(name);
//产生该类的空参对象
Object object = clazz.newInstance();
}


/*
* 当获取指定名称对应类中所体现的对象时,
* 而该对象初始化不使用空参构造怎么办?
* 构造函数加了造数,是说明我们要指定哪个构造函数,而一般空参本来就有,带参是我们自己指定的
* 既然是获取指定的构造函数进行对象的初始化,先获取到该构造函数。
* 如何先获取构造函数?通过字节码文件对象即可完成
* 该方法是:getConstructor (Class ...paramType)
* 因为是可变参数 。。。 就不用new 数组了,可以直接传,任何数据类型都可以被class文件描述
* 返回的是构造对象
* 字节码文件是对象,字节码对象包括了构造函数,构造函数很复杂,他有名字参数列表,修饰符字段,类型名陈,值方法等,封装成对象
* 能去解析类成员的对象,构造器,字段,方法,数组
*/
public static void creatNewObject(String name) throws Exception {
//找寻该名称类文件,并加载进内存,并产生Class对象
Class clazz = Class.forName(name);

Constructor constructor = clazz.getConstructor(String.class,int.class);

Object object = constructor.newInstance("小强",35);
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
//类名不写全 ClassNotFoundException
//没有空参构造函数 InstantiationException 初始化异常
//空参构造函数私有化 IllegalAccessException 无效访问异常
creatNewObject();
}
}

posted @ 2021-02-02 17:02  何日才能成为大神  阅读(66)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3