面试的时候只想到了equal() hashcode() tostring()
面试官当时问equal()和 hashcode()区别 我的回答是:equal是否通过比较地址判断对象是否相等,hashcode是通过hash值判断对象是否相等
很长时间没有看过这些基础知识了 突然问还是不会的,
————————————————————————————————
下面是打开studio查的object的方法
public class Object {
public Object() {
throw new RuntimeException("Stub!");
}
@RecentlyNonNull
public final Class<?> getClass() {
throw new RuntimeException("Stub!");
}
public int hashCode() {
throw new RuntimeException("Stub!");
}
public boolean equals(@RecentlyNullable Object obj) {
throw new RuntimeException("Stub!");
}
@RecentlyNonNull
protected Object clone() throws CloneNotSupportedException {
throw new RuntimeException("Stub!");
}
@RecentlyNonNull
public String toString() {
throw new RuntimeException("Stub!");
}
public final native void notify();
public final native void notifyAll();
public final void wait(long timeout) throws InterruptedException {
throw new RuntimeException("Stub!");
}
public final native void wait(long var1, int var3) throws InterruptedException;
public final void wait() throws InterruptedException {
throw new RuntimeException("Stub!");
}
protected void finalize() throws Throwable {
throw new RuntimeException("Stub!");
}
}
复盘面试官的问题:
1.object常用的方法,除了equals、 hashcode 和tostring,剩下的notify()、notifyAll()和wait() 全是线程用的方法
2.getClass是获取对象的字节码类型,得到该对象的真实类型,该方法属于java的反射机制,返回的java的class类型,例如:Class cl=obj.getClass(),通过cl对象,我们可以获取该对象的所有成员方法,每个成员方法都是一个method对象。我们也可以获得该对象的成员变量,每个对象都是一个Filed对象,同样也可以获得该对象的构造函数
写了点关于反射的测试代码
Person类(userName和age采用不同的修饰符是为了查看getFileds,getMethods和getDeclaredFields,etDeclaredMethods的区别 )
public class Person {
public String userName;
private int age;
public Person(String userName, int age) {
this.userName = userName;
this.age = age;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
反射写的代码 我直接在activity中测试的
Class<Person> c = Person.class;//获取person对应的class对象
try {
Log.d("TAG", "CLASS 类名 " + c.getName());
Constructor constructor = c.getConstructor(String.class, int.class);//返回类型的构造方法
Log.d("TAG", "构造方法的名字 " + constructor.getName());
Class[] pt = constructor.getParameterTypes();
for (int i = 0; i < pt.length; i++) {
Log.d("TAG", "构造方法中参数类型 " + pt[i].getName());
}
Field[] fields = c.getFields();//获取所有的成员变量
for (Field field : fields) {
int mod=field.getModifiers();//获取修饰符
Log.d("TAG", "获取所有的成员变量 " + Modifier.toString(mod));
Class type=field.getType();
Log.d("TAG", "获取成员变量名称" + field.getName());//获取成员变量名称
Log.d("TAG", "获取成员变量类型 " + type.getName());//获取成员变量类型
}
Method[] methods=c.getMethods();
for (Method method:methods) {
Log.d("TAG", "方法名" + method.getName());//方法名
Log.d("TAG", "方法中参数的个数" + method.getParameterCount());//方法中参数的个数
Parameter []parameters=method.getParameters();
int index=1;
for (Parameter parameter:parameters){
if (parameter.isNamePresent()){
Log.d("TAG", "第" + (index++)+"个参数");
Log.d("TAG", " 参数名称 " + (parameter.getName()));
Log.d("TAG", " 参数类型 " + (parameter.getType()));
Log.d("TAG", " 泛型类型 " + (parameter.getParameterizedType()));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
结论:getFields和getMethods获取的是修饰符为public的成员变量和方法,还包括了从父类继承得到的成员变量和方法
getDeclaredFields,etDeclaredMethods获取的是类中定义的所以成员变量和方法
打印日志:getFields和getMethods
getDeclaredFields,etDeclaredMethods
3.equals:是用来判断两个对象是否指向了同一块存储单元地址
hashcode :俩个对象也可以根据hashcode来比较
示例代码:
String a = "张三";
String b = new String("张三");
Log.d("TAG","equals"+a.equals(b));
Log.d("TAG","=="+(a==b));
Log.d("TAG","a.hashCode()"+a.hashCode());
Log.d("TAG","b.hashCode()"+b.hashCode());
Log.d("TAG","a.hashCode()==b.hashCode()"+(b.hashCode()==a.hashCode()));
打印结果:
4.clone()
深克隆是引用类型和值类型都可以复制
浅克隆是复制的值类型
5.finalize() 是在垃圾回收时,用于确认该对象是否确认被回收的一个标记,该方法需要重写才会执行,而且只会执行一次,对象可以在该方法中自救,避免被垃圾回收掉,同样自救有只能进行一次(不推荐调用该方法)