(1)Object:java中所有类的父类
来自于:java.lang.object
java中不允许循环继承
class A extends B
Class B extends A
(2)方法:11个方法
(1)getClass() 返回对象的类对象 常用来比较两个对象是否是同一类型
(2)finalize() 对象被回收时调用,有垃圾回收器自动调用。
垃圾回收:垃圾过多时,会造成内存泄露等问题
垃圾回收器在内存即将耗尽时通过零引用算法认定垃圾对象。
程序猿可以通过System.GC()手动调用资源回收器回收垃圾
(3)toString() 返回对象的字符串形式
(4)equals() 判断两个对象是否内容相同
比较固定的写法:
public boolean equals(Object o){
if(this == o)return true;
if(o==null)return false;
if(this.getClass()!=o.getClass())return false;
本类型 变量名 = (类型)o;//强转
判断内容相同
}
(5)int hashcode()object中默认通过引用的地址计算哈希值, 尽量保证内容不同的对象返回不同的int
(6)这是5个方法 wait()和notify() 两种组合使线程之间能够通信
o.wait(): 必须放在对o加锁的同步代码块中. 线程会释放其所拥有的所有锁标记,进入o的等待队列
o.notify()/notifyAll():必须放在对o加锁的同步代码块中,线程会从o的等待队列中释放一个/全部线程
(7)clone()
(3)hashcode和equals方法
解决的问题:hashSet和ArrayList中,现在要放入很多元素,可能有相同的元素,其中hashSet是不要求放入相同元素的。这是hashcode的算法和equals的实现就是最有效解决这个问题。
hashcode可以根据一个对象的地址计算对象一个哈希值,可以理解为放到一个区域中,如果再有对象也计算出这个哈希值,再比较这个对象和已有对象的equals方法。这两个方法共同决定
两个对象是否相同。
这个情况的应用:尽量保证内容不同的对象比较出来,我们要重载这两个方法。
有个情况可以很好的解释:remove方法的使用。如果我们将一个对象放入集合中,这是应该是有一个对象的,如果我们改变了参与hashCode值计算的属性,这时我们将这个对象通过remove移除可能就无法移除了,但我们不知道会造成内存泄露(这个内存怎么消失了??就是因为一个对象一直没有被释放)。
public class TestReflection {
public void reflectionApp(){
Collection<ReflectionPoint> collections = new ArrayList<ReflectionPoint>();
ReflectionPoint rfctp1 =new ReflectionPoint(3,3);
ReflectionPoint rfctp2 =new ReflectionPoint(4,4);
ReflectionPoint rfctp3 =new ReflectionPoint(5,5);
ReflectionPoint rfctp4 =new ReflectionPoint(3,3);
collections.add(rfctp1);
collections.add(rfctp2);
collections.add(rfctp3);
collections.add(rfctp4);
collections.add(rfctp1);
System.out.println("ArrayList");
for(ReflectionPoint rfctp:collections){
System.out.println(rfctp);
}
collections = new HashSet<ReflectionPoint>();
collections.add(rfctp1);
collections.add(rfctp2);
collections.add(rfctp3);
collections.add(rfctp4);
collections.add(rfctp1);
System.out.println("HashSet");
for(ReflectionPoint rfctp:collections){
System.out.println(rfctp);
}
}
}
这个类写的就不会将rfctp4放进去。public class TestReflection {
public void reflectionApp(){
Collection<ReflectionPoint> collections = new ArrayList<ReflectionPoint>();
ReflectionPoint rfctp1 =new ReflectionPoint(3,3);
ReflectionPoint rfctp2 =new ReflectionPoint(4,4);
ReflectionPoint rfctp3 =new ReflectionPoint(5,5);
ReflectionPoint rfctp4 =new ReflectionPoint(3,3);
collections.add(rfctp1);
collections.add(rfctp2);
collections.add(rfctp3);
collections.add(rfctp4);
collections.add(rfctp1);
System.out.println("ArrayList");
for(ReflectionPoint rfctp:collections){
System.out.println(rfctp);
}
collections = new HashSet<ReflectionPoint>();
collections.add(rfctp1);
collections.add(rfctp2);
collections.add(rfctp3);
collections.add(rfctp4);
collections.add(rfctp1);
System.out.println("HashSet");
for(ReflectionPoint rfctp:collections){
System.out.println(rfctp);
}
}
}
class ReflectionPoint{
//属性
private double x;
private double y;
//构造方法
public ReflectionPoint(){}
public ReflectionPoint(double x,double y){
this.x = x;
this.y = y;
}
//set和get方法
public double getX(){return x;}
public double getY(){return y;}
public void setX(double x){this.x = x;}
public void getY(double y){this.y = y;}
//toString
public String toString(){
return "X = " + x + " Y = " + y;
}
//equals
public boolean equals(Object o){
if(this == o) return true;
if(o == null) return false;
if(this.getClass()!=o.getClass())return false;
ReflectionPoint rf = (ReflectionPoint)o;
if(rf.getX()!=x)return false;
if(rf.getY()!=y)return false;
if(rf.getX()!=x)return false;
if(rf.getY()!=y)return false;
return true;
}
//hashcode
public int hashCode(){
return (int)(x+y);
}
}
ArrayList
X = 3.0 Y = 3.0
X = 3.0 Y = 4.0
X = 3.0 Y = 5.0
X = 3.0 Y = 3.0
X = 3.0 Y = 3.0
equals 3.0+4.0
equals 3.0+5.0
equals 3.0+3.0
HashSet
X = 3.0 Y = 3.0
这个我理解是:无论你是否重载了hashCode系统都会为你对对象的地址进行一次匹配(引用),如果你重载了之后它才会执行hashCode和equals (三步)但移除的时候是直接执行我们自己重载的hashCode。(两步)
public class TestReflection {
public void reflectionApp(){
Collection<ReflectionPoint> collections = new ArrayList<ReflectionPoint>();
ReflectionPoint rfctp1 =new ReflectionPoint(3,3);
ReflectionPoint rfctp2 =new ReflectionPoint(3,4);
ReflectionPoint rfctp3 =new ReflectionPoint(3,5);
ReflectionPoint rfctp4 =new ReflectionPoint(3,3);
collections.add(rfctp1);
collections.add(rfctp2);
collections.add(rfctp3);
collections.add(rfctp4);
collections.add(rfctp1);
System.out.println("ArrayList");
for(ReflectionPoint rfctp:collections){
System.out.println(rfctp);
}
collections = new HashSet<ReflectionPoint>();
collections.add(rfctp1);
collections.add(rfctp2);
collections.add(rfctp3);
collections.add(rfctp4);
collections.add(rfctp1);
System.out.println("HashSet");
for(ReflectionPoint rfctp:collections){
System.out.println(rfctp);
}
}
}
class ReflectionPoint{
//属性
private double x;
private double y;
//构造方法
public ReflectionPoint(){}
public ReflectionPoint(double x,double y){
this.x = x;
this.y = y;
}
//set和get方法
public double getX(){return x;}
public double getY(){return y;}
public void setX(double x){this.x = x;}
public void getY(double y){this.y = y;}
//toString
public String toString(){
return "X = " + x + " Y = " + y;
}
//equals
public boolean equals(Object o){
/*if(this == o) return true;
if(o == null) return false;
if(this.getClass()!=o.getClass())return false;
ReflectionPoint rf = (ReflectionPoint)o;
if(rf.getX()!=x)return false;
if(rf.getY()!=y)return false;
if(rf.getX()!=x)return false;
if(rf.getY()!=y)return false;*/
System.out.println("equals");
return true;
}
//hashcode
public int hashCode(){
return (int)x;
}
}
来自于:java.lang.object
java中不允许循环继承
class A extends B
Class B extends A
(2)方法:11个方法
(1)getClass() 返回对象的类对象 常用来比较两个对象是否是同一类型
(2)finalize() 对象被回收时调用,有垃圾回收器自动调用。
垃圾回收:垃圾过多时,会造成内存泄露等问题
垃圾回收器在内存即将耗尽时通过零引用算法认定垃圾对象。
程序猿可以通过System.GC()手动调用资源回收器回收垃圾
(3)toString() 返回对象的字符串形式
(4)equals() 判断两个对象是否内容相同
比较固定的写法:
public boolean equals(Object o){
if(this == o)return true;
if(o==null)return false;
if(this.getClass()!=o.getClass())return false;
本类型 变量名 = (类型)o;//强转
判断内容相同
}
(5)int hashcode()object中默认通过引用的地址计算哈希值, 尽量保证内容不同的对象返回不同的int
(6)这是5个方法 wait()和notify() 两种组合使线程之间能够通信
o.wait(): 必须放在对o加锁的同步代码块中. 线程会释放其所拥有的所有锁标记,进入o的等待队列
o.notify()/notifyAll():必须放在对o加锁的同步代码块中,线程会从o的等待队列中释放一个/全部线程
(7)clone()
(3)hashcode和equals方法
解决的问题:hashSet和ArrayList中,现在要放入很多元素,可能有相同的元素,其中hashSet是不要求放入相同元素的。这是hashcode的算法和equals的实现就是最有效解决这个问题。
hashcode可以根据一个对象的地址计算对象一个哈希值,可以理解为放到一个区域中,如果再有对象也计算出这个哈希值,再比较这个对象和已有对象的equals方法。这两个方法共同决定
两个对象是否相同。
这个情况的应用:尽量保证内容不同的对象比较出来,我们要重载这两个方法。
有个情况可以很好的解释:remove方法的使用。如果我们将一个对象放入集合中,这是应该是有一个对象的,如果我们改变了参与hashCode值计算的属性,这时我们将这个对象通过remove移除可能就无法移除了,但我们不知道会造成内存泄露(这个内存怎么消失了??就是因为一个对象一直没有被释放)。
public class TestReflection {
public void reflectionApp(){
Collection<ReflectionPoint> collections = new ArrayList<ReflectionPoint>();
ReflectionPoint rfctp1 =new ReflectionPoint(3,3);
ReflectionPoint rfctp2 =new ReflectionPoint(4,4);
ReflectionPoint rfctp3 =new ReflectionPoint(5,5);
ReflectionPoint rfctp4 =new ReflectionPoint(3,3);
collections.add(rfctp1);
collections.add(rfctp2);
collections.add(rfctp3);
collections.add(rfctp4);
collections.add(rfctp1);
System.out.println("ArrayList");
for(ReflectionPoint rfctp:collections){
System.out.println(rfctp);
}
collections = new HashSet<ReflectionPoint>();
collections.add(rfctp1);
collections.add(rfctp2);
collections.add(rfctp3);
collections.add(rfctp4);
collections.add(rfctp1);
System.out.println("HashSet");
for(ReflectionPoint rfctp:collections){
System.out.println(rfctp);
}
}
}
这个类写的就不会将rfctp4放进去。public class TestReflection {
public void reflectionApp(){
Collection<ReflectionPoint> collections = new ArrayList<ReflectionPoint>();
ReflectionPoint rfctp1 =new ReflectionPoint(3,3);
ReflectionPoint rfctp2 =new ReflectionPoint(4,4);
ReflectionPoint rfctp3 =new ReflectionPoint(5,5);
ReflectionPoint rfctp4 =new ReflectionPoint(3,3);
collections.add(rfctp1);
collections.add(rfctp2);
collections.add(rfctp3);
collections.add(rfctp4);
collections.add(rfctp1);
System.out.println("ArrayList");
for(ReflectionPoint rfctp:collections){
System.out.println(rfctp);
}
collections = new HashSet<ReflectionPoint>();
collections.add(rfctp1);
collections.add(rfctp2);
collections.add(rfctp3);
collections.add(rfctp4);
collections.add(rfctp1);
System.out.println("HashSet");
for(ReflectionPoint rfctp:collections){
System.out.println(rfctp);
}
}
}
class ReflectionPoint{
//属性
private double x;
private double y;
//构造方法
public ReflectionPoint(){}
public ReflectionPoint(double x,double y){
this.x = x;
this.y = y;
}
//set和get方法
public double getX(){return x;}
public double getY(){return y;}
public void setX(double x){this.x = x;}
public void getY(double y){this.y = y;}
//toString
public String toString(){
return "X = " + x + " Y = " + y;
}
//equals
public boolean equals(Object o){
if(this == o) return true;
if(o == null) return false;
if(this.getClass()!=o.getClass())return false;
ReflectionPoint rf = (ReflectionPoint)o;
if(rf.getX()!=x)return false;
if(rf.getY()!=y)return false;
if(rf.getX()!=x)return false;
if(rf.getY()!=y)return false;
return true;
}
//hashcode
public int hashCode(){
return (int)(x+y);
}
}
ArrayList
X = 3.0 Y = 3.0
X = 3.0 Y = 4.0
X = 3.0 Y = 5.0
X = 3.0 Y = 3.0
X = 3.0 Y = 3.0
equals 3.0+4.0
equals 3.0+5.0
equals 3.0+3.0
HashSet
X = 3.0 Y = 3.0
这个我理解是:无论你是否重载了hashCode系统都会为你对对象的地址进行一次匹配(引用),如果你重载了之后它才会执行hashCode和equals (三步)但移除的时候是直接执行我们自己重载的hashCode。(两步)
public class TestReflection {
public void reflectionApp(){
Collection<ReflectionPoint> collections = new ArrayList<ReflectionPoint>();
ReflectionPoint rfctp1 =new ReflectionPoint(3,3);
ReflectionPoint rfctp2 =new ReflectionPoint(3,4);
ReflectionPoint rfctp3 =new ReflectionPoint(3,5);
ReflectionPoint rfctp4 =new ReflectionPoint(3,3);
collections.add(rfctp1);
collections.add(rfctp2);
collections.add(rfctp3);
collections.add(rfctp4);
collections.add(rfctp1);
System.out.println("ArrayList");
for(ReflectionPoint rfctp:collections){
System.out.println(rfctp);
}
collections = new HashSet<ReflectionPoint>();
collections.add(rfctp1);
collections.add(rfctp2);
collections.add(rfctp3);
collections.add(rfctp4);
collections.add(rfctp1);
System.out.println("HashSet");
for(ReflectionPoint rfctp:collections){
System.out.println(rfctp);
}
}
}
class ReflectionPoint{
//属性
private double x;
private double y;
//构造方法
public ReflectionPoint(){}
public ReflectionPoint(double x,double y){
this.x = x;
this.y = y;
}
//set和get方法
public double getX(){return x;}
public double getY(){return y;}
public void setX(double x){this.x = x;}
public void getY(double y){this.y = y;}
//toString
public String toString(){
return "X = " + x + " Y = " + y;
}
//equals
public boolean equals(Object o){
/*if(this == o) return true;
if(o == null) return false;
if(this.getClass()!=o.getClass())return false;
ReflectionPoint rf = (ReflectionPoint)o;
if(rf.getX()!=x)return false;
if(rf.getY()!=y)return false;
if(rf.getX()!=x)return false;
if(rf.getY()!=y)return false;*/
System.out.println("equals");
return true;
}
//hashcode
public int hashCode(){
return (int)x;
}
}