java反射(1),2021年BATJ30套大厂Android经典高频面试题

java反射(1),2021年BATJ30套大厂Android经典高频面试题,第1张

java反射(1),2021年BATJ30套大厂Android经典高频面试题

}

}

Person.java

package com.ldw.bean;

public class Person {

private String name;

private int age;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + age;

result = prime * result + ((name == null) ? 0 : name.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Person other = (Person) obj;

if (age != other.age)

return false;

if (name == null) {

if (other.name != null)

return false;

} else if (!name.equals(other.name))

return false;

return true;

}

}

输出:

====class com.ldw.bean.Person

====class com.ldw.bean.Person

====class com.ldw.bean.Person

true

true

利用反射读取配置文件:

config.properties

com.ldw.test.Apple

refelect_test1.java

package com.ldw.test;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

public class refelect_test1 {

public static void main(String[] args){

JuiceMaker jm = new JuiceMaker();

//jm.run(new Apple());

//jm.run(new Orange());

BufferedReader br;

try {

br = new BufferedReader(new FileReader(“config.properties”));

//利用反射读取配置文件

Class clazz = Class.forName(br.readLine());

//Class类的newInstance()方法是使用该类无参的构造函数创建对象

Fruit apple = (Fruit) clazz.newInstance();

System.out.println(“clazz====” + clazz);

jm.run(apple);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

interface Fruit{

public void squeez();

}

class Apple implements Fruit{

@Override

public void squeez(){

System.out.println(“this is apple juice=====”);

}

}

class Orange implements Fruit{

@Override

public void squeez(){

System.out.println(“this is orange juice=====”);

}

}

class JuiceMaker{

public void run(Fruit fruit){

fruit.squeez();

}

}

输出

clazz====class com.ldw.test.Apple

this is apple juice=====

Class类的newInstance()方法是使用该类无参的构造函数创建对象, 如果一个类没有无参的构造函数, 就不能这样创建了,可以调用Class类的getConstructor(String.class,int.class)方法获取一个指定的构造函数然后再调用Constructor类的newInstance(“张三”,20)方法创建对象

Person.java

package com.ldw.bean;

public class Person {

private String name;

private int age;

public Person() {

super();

}

public Person(String name, int age) {

super();

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + age;

result = prime * result + ((name == null) ? 0 : name.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Person other = (Person) obj;

if (age != other.age)

return false;

if (name == null) {

if (other.name != null)

return false;

} else if (!name.equals(other.name))

return false;

return true;

}

@Override

public String toString() {

return “Person [name=” + name + “, age=” + age + “]”;

}

}

refelect_test2.java

package com.ldw.test;

import java.io.BufferedReader;

import java.io.FileReader;

import java.lang.reflect.Constructor;

import com.ldw.bean.Person;

public class refelect_test2 {

public static void main(String[] args){

try {

Class clazz = Class.forName(“com.ldw.bean.Person”);

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

System.out.println(“clazz====” + clazz);

Person p = (Person) c.newInstance(“zhangsan”, 22);

System.out.println§;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

输出:

clazz====class com.ldw.bean.Person

Person [name=zhangsan, age=22]

利用反射修改参数,调用方法:

Person.java

package com.ldw.bean;

public class Person {

public String name;

private int age;

public Person() {

super();

}

public Person(String name, int age) {

super();

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public voi

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

d setAge(int age) {

this.age = age;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + age;

result = prime * result + ((name == null) ? 0 : name.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Person other = (Person) obj;

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5660660.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存