Java self - Object class inheritance and interfaces

Java in the super class Object

Step. 1: Object class is the parent of all classes

Declare a class, the default is inherited Object
public Object class the extends Hero

package charactor;
 
import property.Item;
 
public class Hero extends Object {
        
    String name; //姓名
        
    float hp; //血量
        
    float armor; //护甲
        
    int moveSpeed; //移动速度
     
    public void useItem(Item i){
        System.out.println("hero use item");
        i.effect();
    }   
     
    public Hero(){
        System.out.println("Hero的无参的构造方法 ");
    }
     
    public Hero(String name){
        System.out.println("Hero的有一个参数的构造方法 ");
        this.name = name;
    }
     
    public static void main(String[] args) {
        new Hero();
    }
      
}

Step 2: toString ()

Object class provides a toString method, so that all the classes have toString method
toString () returns the current object means that the character string expressed
by System.out.println print target is to print the object toString () Return value

package charactor;
  
public class Hero {
    public String name;
    protected float hp;
      
    public String toString(){
        return name;
    }
      
    public static void main(String[] args) {
         
        Hero h = new Hero();
        h.name = "盖伦";
        System.out.println(h.toString());
        //直接打印对象就是打印该对象的toString()返回值
        System.out.println(h);
    }
}

Step. 3: Finalize ()

When an object has no references to time, and it satisfies the conditions for garbage collection

When it is recycled garbage, it's finalize () method is called.

finalize () method is not the developers take the initiative to call, but by the virtual machine JVM calls.
finalize()

package charactor;
  
public class Hero {
    public String name;
    protected float hp;
      
    public String toString(){
        return name;
    }
     
    public void finalize(){
        System.out.println("这个英雄正在被回收");
    }
      
    public static void main(String[] args) {
        //只有一引用
        Hero h;
        for (int i = 0; i < 100000; i++) {
            //不断生成新的对象
            //每创建一个对象,前一个对象,就没有引用指向了
            //那些对象,就满足垃圾回收的条件
            //当,垃圾堆积的比较多的时候,就会触发垃圾回收
            //一旦这个对象被回收,它的finalize()方法就会被调用
            h = new Hero();
        }
 
    }
}

Step. 4: the equals ()

equals () for determining the contents of the two objects are the same

Suppose, when the same hp two heroes of the time, we believe that these same two heroes

package charactor;
  
public class Hero {
    public String name;
    protected float hp;
      
    public boolean equals(Object o){
        if(o instanceof Hero){
            Hero h = (Hero) o;
            return this.hp == h.hp;
        }
        return false;
    }
      
    public static void main(String[] args) {
        Hero h1= new Hero();
        h1.hp = 300;
        Hero h2= new Hero();
        h2.hp = 400;
        Hero h3= new Hero();
        h3.hp = 300;
         
        System.out.println(h1.equals(h2));
        System.out.println(h1.equals(h3));
    }
}

Step 5: ==

Object This is not a method, but used to determine whether two identical objects
more accurately say , two references for judging, it points to the same object

package charactor;
  
public class Hero {
    public String name;
    protected float hp;
      
    public boolean equals(Object o){
        if(o instanceof Hero){
            Hero h = (Hero) o;
            return this.hp == h.hp;
        }
        return false;
    }
      
    public static void main(String[] args) {
        Hero h1= new Hero();
        h1.hp = 300;
        Hero h2= new Hero();
        h2.hp = 400;
        Hero h3= new Hero();
        h3.hp = 300;
         
        System.out.println(h1==h2);
        System.out.println(h1==h3);
         
    }
}

Step. 6: the hashCode ()

hashCode method returns a hash value of the object

Step 7: Thread Synchronization related methods

Object also provides relevant thread synchronization method
the wait ()
the Notify ()
notifyAll ()

Step. 8: getClass ()

getClass () returns an object class object

Exercise : Object

Item rewriting the toString (), Finalize () and equals () method
toString () Returns the Item +. Price name
Finalize () output current object being recovered
equals (Object o) o first determines whether the Item type, and then compare the two the price is the same one Item

The answer :

package property;
 
public class Item extends Object {
    String name;
    int price;
 
    public String toString() {
        return "叫做 " + name + " 的物品,其价格是 " + price;
    }
 
    public void finalize() {
        System.out.println("回收叫做 " + name + " 的物品");
    }
 
    public boolean equals(Object o) {
        if (o instanceof Item) {
            Item i = (Item) o;
            if (i.price == this.price)
                return true;
            else
                return false;
        }
        return false;
    }
 
}

Guess you like

Origin www.cnblogs.com/jeddzd/p/11532236.html