public static void main(String[] args) throws Exception {
System.out.println("main method");
Object obj = 1;
Object obj1 = 1;
System.out.println("1".equals(obj1)); // false
System.out.println(obj.equals(obj1)); // true
System.out.println(obj.toString());
}
查其原因:
/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
关于字符串的equals方法,结果返回true,当且仅当参数非空,并且是一个String对象,代表相同的字节序列。
1.示例
委托类
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package demoGradleOne;
public class Library {
private String name;
public Library() {
super();
}
public Library(String name) {
super();
this.setName(name);
}
public boolean someLibraryMethod() {
return true;
}
@Override
public String toString() {
return "demoGradleOne";
}
private String write() {
return "write method";
}
public static void main(String[] args) throws Exception {
System.out.println("main method");
Object obj = 1;
Object obj1 = 1;
System.out.println("1".equals(obj1));
System.out.println(obj.toString());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试
package demoGradleOne;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
public class App {
public static void main(String[] args) throws Exception {
Library lib = new Library();
Class<? extends Library> cl = lib.getClass();
Class<Library> cl2 = Library.class;
System.out.println(cl.getName());
System.out.println(cl == cl2);
Class cl3;
try {
cl3 = Class.forName("demoGradleOne.Library");
Constructor[] conArray = cl3.getConstructors();
for(Constructor c : conArray){
if (c.getParameterCount() != 1) {
Library obj = (Library)c.newInstance();
System.out.println(obj.toString());
System.out.println(obj.someLibraryMethod());
break;
}
Library obj = (Library)c.newInstance("titi");
System.out.println(obj.toString());
System.out.println(obj.someLibraryMethod());
}
System.out.println(cl == cl3);
System.out.println("===================");
Method[] methodArray = cl3.getMethods();
for(Method m : methodArray){
System.out.println(m);
}
System.out.println("===================");
methodArray = cl3.getDeclaredMethods();
for(Method m : methodArray){
System.out.println(m);
}
System.out.println("===================");
Method methodMain = cl3.getMethod("main", String[].class);
methodMain.invoke(null, (Object)new String[]{"1","2"});
System.out.println("===================");
ArrayList<String> strList = new ArrayList<>();
strList.add("aaa");
strList.add("bbb");
// strList.add(100);
//获取ArrayList的Class对象,反向的调用add()方法,添加数据
Class listClass = strList.getClass(); //得到 strList 对象的字节码 对象
//获取add()方法
Method m = listClass.getMethod("add", Object.class);
//调用add()方法
m.invoke(strList, 100);
System.out.println(strList);
System.out.println("===================");
Field[] fields = cl3.getDeclaredFields();
for(Field f : fields){
System.out.println(f);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InvocationHandler handler = new MyInvocationHandler(lib);
LibraryInterface f = (LibraryInterface) Proxy.newProxyInstance(LibraryInterface.class.getClassLoader(),
new Class[] { LibraryInterface.class },
handler);
System.out.println("===================");
System.out.println(f.someLibraryMethod());
}
}
调度方法调用的调用处理程序
package demoGradleOne;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MyInvocationHandler implements InvocationHandler{
private Library lib;
public MyInvocationHandler(Library lib) {
this.lib = lib;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
long start = System.currentTimeMillis();
Object result = null;
try {
result = method.invoke(lib, args);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("use time = " + (end - start));
return result;
}
}
网友评论