忘羡的Day9!

技术总结:

获取Class对象的方法?

getClass

.class

Class.forName();

ClassLoader

代码如下:
`package com.javaboy.demo1;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

/**

  • 获取Class对象的四种方式:
    /
    public class Demo2 {
    public static void main(String[] args) {
    /
    getClass
    .class
    Class.forName();
    ClassLoader*/
    //1. getClass()
    String s = new String("hello");
    Class clazz1= s.getClass();//引用类型尽可能用.getClass();

    //2. .class
    Class clazz2= String.class;

    //3.Class.forName();
    Class clazz3=null;
    try {
    clazz3= Class.forName("java.lang.String");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }

    //4. ClassLoader
    Class clazz4=null;
    try {
    clazz4= ClassLoader.getSystemClassLoader().loadClass("java.lang.String");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    System.out.println(clazz1clazz2);
    System.out.println(clazz1
    clazz3);
    System.out.println(clazz1clazz4);
    System.out.println(clazz3
    clazz4);`

在JDK中,主要由以下类来实现Java反射机制,这些类都位于java.lang.reflect包中:

Class类:代表一个类。

Field 类:代表类的成员变量(成员变量也称为类的属性)。

Method类:代表类的方法。

Modifier类:代表修饰符。

Constructor 类:代表类的构造方法。

Array类:提供了动态创建数组,以及访问数组的元素的静态方法。

代码如下:

`package com.xjy.demo2;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

public class Demo2 {
public static void main(String[] args) {
Student student=new Student();
Class clzStu=student.getClass();
Field[] fields= clzStu.getDeclaredFields();
System.out.println(Arrays.toString(fields));
//have a try
try {
//通过类对象获得类对象所对应的类里面的属性,通过给定的参数
Field field = clzStu.getDeclaredField("name");
System.out.println("field:"+field);
//通过类对象获得类对象所对应的类里面的属性,获得所有的属性
Field[] fields2 = clzStu.getDeclaredFields();
System.out.println("fields2:"+fields2);
System.out.println(Arrays.toString(fields2));

//Method
try {
Method method=clzStu.getMethod("test",String.class);
System.out.println("method:"+method);

Method[] methods2 = clzStu.getMethods();
System.out.println("methods2:"+methods2);
System.out.println(Arrays.toString(methods2));
System.out.println("************");
//发音 有道词典发音 百度发音
Method[] methods3 =clzStu.getDeclaredMethods();
System.out.println("methods:"+Arrays.toString(methods3));

//4.modifiler
int num= clzStu.getModifiers();
System.out.println("num:"+num);

//5.Constructor
Constructor constructor=clzStu.getConstructor(String.class);
System.out.println("constructor:"+constructor);

//6.Constructers
Constructor[] constructors=clzStu.getConstructors();
System.out.println("constructors:"+constructors);
System.out.println("#############");
System.out.println(Arrays.toString(constructors));

//7.
Constructor[] constructors1 =clzStu.getDeclaredConstructors();
System.out.println("#############");
System.out.println(Arrays.toString(constructors1));
} catch (NoSuchMethodException e) {
e.printStackTrace();
}

} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
}`

心得:

1。我们必须明确一个大方向,向着这个大方向发展,努力!
2。学习是为了用的,是为了让你对社会产生价值!
3。在学习过程中一定要动手做,而不是只听不写 没用 很多东西和体会必须自己动手才能真正属于自己
4.在学习过程中可能会遇到形形色色的问题不容易解决 应多去网上查资料 因为书上的东西有限。
可以去网上查查 加以整理,促进学习的深入和水平的提高!!!

posted @ 2020-08-24 16:26  向忘羡  阅读(185)  评论(0)    收藏  举报