发表于: 2018-02-28 20:29:00
1 628
今日完成
1.integer 的自动装箱和拆箱。
public class IntegerDemo {
public static void main(String[] args) {
// integerDemoTest();
// integerDemoTest2();
// integerDemoTest3();
integerDemoTest4();
}
private static void integerDemoTest4() {
Integer a = new Integer(123);
Integer b = new Integer(123);
System.out.println(a == b);//false ,new了两个对象
Integer a1 = 130;
Integer b1 = 130;
System.out.println(a1 == b1);//false,Java定义在自动装箱时对于值从–128到127之间的值,它们被装箱为Integer对象后,会存在内存中被重用,始终只存在一个对象。
//而如果超过了从–128到127之间的值,被装箱后的Integer对象并不会被重用,即相当于每次装箱时都新建一个 Integer对象。
}
private static void integerDemoTest3() {
int i = 128;
Integer i2 = 128;
Integer i3 = new Integer(128);
//Integer会自动拆箱为int
System.out.println(i == i2);//true
System.out.println(i == i3);//true
System.out.println("**************");
Integer i5 = 127;//java在编译的时候,被翻译成-> Integer i5 = Integer.valueOf(127);
Integer i6 = 127;
System.out.println(i5 == i6);//true
// Integer i5 = 128;
// Integer i6 = 128;
// System.out.println(i5 == i6);//false
Integer ii5 = new Integer(127);
System.out.println(i5 == ii5); //false
Integer i7 = new Integer(128);
Integer i8 = new Integer(123);
System.out.println(i7 == i8); //false
}
private static void integerDemoTest2() {
Integer a2 = new Integer("1234");
System.out.println(Integer.parseInt("-1234"));
System.out.println(a2);
}
private static void integerDemoTest() {
Integer a = new Integer(12);
int b = 12;
Integer a1 = 12;
System.out.println(a == b);
System.out.println(a == a1);
Integer c = new Integer(2134);
Integer c1 = 2134;
int d = 2134;
System.out.println(c == d);
System.out.println(c == c1);
}
}
2.学习反射
/**
* 反射机制,能够在运行状态下回去一个类的所有的属性和方法
* 动态获取
*/
public class FanSheDemo1 {
public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
// getObject1_1();
// getObject1_2();
// getFiled_1();
getMethod();
}
private static void getMethod() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
/*
*获取普通方法
*/
Class a = Class.forName("day8_FanShe.Person");
Object ob = a.newInstance();
Method method = a.getMethod("show", int.class, String.class);
method.invoke(ob, 3, "show method");
}
private static void getFiled_1() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException {
/*
*通过反射获得字段
*/
Class a = Class.forName("day8_FanShe.Person");
//获取本来公有的成员属性
Field field = a.getDeclaredField("age");
Object ob = a.newInstance();
field.set(ob, 34);
//获得其中的成员的值
int age = (int) field.get(ob);
System.out.println(age);
}
private static void getObject1_2() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//如果需要调用带参数的构造函数,需要调用有参数的构造函数
Class a = Class.forName("day8_FanShe.Person");
//1.java中万物都是对象,通过getConstructor(paramterType)的方法,得到对应Class对象的构造函数
Constructor<?> con = a.getConstructor(int.class, String.class);
//2.调用newInstance,并在其中填入参数列表,就能够调用其构造函数了;
Person person = (Person) con.newInstance(4, "baby");
}
private static void getObject1_1() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
//通过反射获取对象实例
//1.获取Person对象的 字节码文件
Class a = Class.forName("day8_FanShe.Person");
//2.调用newInstance 方法
Person person = (Person) a.newInstance();
}
}
明日计划
1.和队员一起写方案
遇到问题
1.暂无
收获
1.学习了自动拆箱与装箱。
2.反射机制的学习。
评论