ReflectTest.md 1.2 KB
Newer Older
CSDN-Ada助手's avatar
CSDN-Ada助手 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
# ReflectTest


以下程序是关于反射的一个例子,程序的控制台打印为:
```java
public class Reflect {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class clazz = Student.class;
        Constructor con = clazz.getConstructor(String.class, Integer.class);
        Student student1 = (Student) con.newInstance("小铭", 24);
        Student student2 = new Student("小铭", 24);
        System.out.println(student2 == student1);
        System.out.println(student2.equals(student1));
    }
}
```

## aop

### before

```java
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

class Student {
    private String name;
    private Integer age;

    public Student() {}

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
```

## 答案

```
false
false
```

## 选项

### A

```
false
true
```

### B

```
true
false
```

### C

```
true
true
```