閱讀904 返回首頁    go 阿裏雲 go 技術社區[雲棲]


馬士兵J2SE-第三章-麵向對象-Object類:toString方法、equals方法、對象轉型、抽象類抽象方法、final關鍵字、接口

toString方法

public class test {
	public static void main(String[] args) {
		Teacher t=new Teacher();
		System.out.println("1 "+t+" 2 "+t.toString());
	}
}


class Teacher {
	public String toString() {
		return "I am a teacher";
	}
}

輸出:

1 I am a teacher 2 I am a teacher

說明:

public String toString() {return "I am a teacher";}不能寫成大寫的public String ToString!

重寫必須要COPY過來!

toString

public String toString()
返回該對象的字符串表示。通常,toString 方法會返回一個“以文本方式表示”此對象的字符串。結果應是一個簡明但易於讀懂的信息表達式。建議所有子類都重寫此方法。

Object 類的 toString 方法返回一個字符串,該字符串由類名(對象是該類的一個實例)、at 標記符“@”和此對象哈希碼的無符號十六進製表示組成。換句話說,該方法返回一個字符串,它的值等於:

getClass().getName() + '@' + Integer.toHexString(hashCode())
 

返回:
該對象的字符串表示形式。

認識一下toString方法,並且重寫父類方法!

 

 equals方法:

public class test {
	public static void main(String[] args) {
		Cat c1=new Cat(1,2,3);
		Cat c2=new Cat(1,2,3);
		System.out.println(c1==c2);
	}
}

class Cat {
   	int color;
    int height,weight;
    public Cat(int color,int height,int weight) {
    	this.color=color;
    	this.height=height;
    	this.weight=weight;
    }
}

//永遠不等,比較的是地址


輸出:

false

 

 

public class test {
	public static void main(String[] args) {
		Cat c1=new Cat(1,2,3);
		Cat c2=new Cat(1,2,3);
		System.out.println(c1==c2);
		System.out.println (c1.equals(c2));
	}
}

class Cat {
   	int color;
    int height,weight;
    public Cat(int color,int height,int weight) {
    	this.color=color;
    	this.height=height;
    	this.weight=weight;
    }
    
    //重寫方法
    public boolean equals(Object obj) {
    	return true;
    }//永遠都相等,不好!就是寫在這裏需要注意一下重寫要從JDK文檔中COPY一下
}

//永遠不等,比較的是地址


輸出:

 false
true

 

 重寫equals方法正確寫法

public class test {
	public static void main(String[] args) {
		Cat c1=new Cat(1,2,3);
		Cat c2=new Cat(1,2,3);
		System.out.println(c1==c2);
		System.out.println (c1.equals(c2));
	}
}

class Cat {
   	int color;
    int height,weight;
    
    public Cat(int color,int height,int weight) {
    	this.color=color;
    	this.height=height;
    	this.weight=weight;
    }
    
    //重寫方法
    public boolean equals(Object obj) {
    	if(obj==null) return false;
    	else {
    		if(obj instanceof Cat) {//如果obj是貓對象的一個引用
    			Cat c=(Cat)obj;//obj強製轉化為貓類型
    			if(c.color==this.color&&c.height==this.height&&c.weight==this.weight) {
    				return true;
    			}
    		}
    	}
    	return false;
    }
}


輸出:

false
true

 

 

public class test {
	public static void main(String[] args) {
		Cat c1=new Cat(1,2,3);
		Cat c2=new Cat(1,2,3);
		System.out.println(c1==c2);
		System.out.println (c1.equals(c2));
		
		
		String s1=new String("Hello");
		String s2=new String("Hello");
		System.out.println(s1==s2);
		
		//java.lang包下的string重寫了equals方法
		//將此字符串與指定的對象比較。當且僅當該參數不為 null,並且是與此對象表示相同字符序列的 String 對象時,結果才為 true。 
		System.out.println(s1.equals(s2));//s2不為空,是true
	}
}

class Cat {
   	int color;
    int height,weight;
    
    public Cat(int color,int height,int weight) {
    	this.color=color;
    	this.height=height;
    	this.weight=weight;
    }
    
    //重寫方法
    public boolean equals(Object obj) {
    	if(obj==null) return false;
    	else {
    		if(obj instanceof Cat) {//如果obj是貓對象的一個引用
    			Cat c=(Cat)obj;//obj強製轉化為貓類型
    			if(c.color==this.color&&c.height==this.height&&c.weight==this.weight) {
    				return true;
    			}
    		}
    	}
    	return false;
    }
}


輸出:

 false
true
false
true

equals

public boolean equals(Object obj)
指示其他某個對象是否與此對象“相等”。

equals 方法在非空對象引用上實現相等關係:

  • 自反性:對於任何非空引用值 xx.equals(x) 都應返回 true
  • 對稱性:對於任何非空引用值 xy,當且僅當 y.equals(x) 返回true 時,x.equals(y) 才應返回true
  • 傳遞性:對於任何非空引用值 xyz,如果 x.equals(y) 返回 true,並且 y.equals(z) 返回 true,那麼x.equals(z) 應返回true
  • 一致性:對於任何非空引用值 xy,多次調用 x.equals(y) 始終返回true 或始終返回false,前提是對象上equals 比較中所用的信息沒有被修改。
  • 對於任何非空引用值 xx.equals(null) 都應返回 false

Object 類的 equals 方法實現對象上差別可能性最大的相等關係;即,對於任何非空引用值 xy,當且僅當xy 引用同一個對象時,此方法才返回truex == y 具有值true)。

注意:當此方法被重寫時,通常有必要重寫 hashCode 方法,以維護 hashCode 方法的常規協定,該協定聲明相等對象必須具有相等的哈希碼。

參數:
obj - 要與之比較的引用對象。
返回:
如果此對象與 obj 參數相同,則返回 true;否則返回 false
另請參見:
hashCode(), Hashtable

 對象轉型:

class Animal {
	public String name;
	Animal(String name) {
		this.name=name;
	}
}

class Cat extends Animal {
	public String eyesColor;
	Cat(String n,String c) {
		super(n);
		eyesColor=c;
	}
}

class Dog extends Animal {
	public String furcolor;
	Dog(String n,String c) {
		super(n);
		furcolor=c;
	}
}

public class test {
	public static void main(String[] args) {
		Animal a=new Animal("name");
		Cat c=new Cat("name","blue");
		Dog d=new Dog("dogname","black");
		
		System.out.println(a instanceof Animal);//true
		System.out.println(c instanceof Animal);//true
		System.out.println(d instanceof Animal);//true
		System.out.println(a instanceof Cat);//false
		
		a=new Dog("bigyellow","yellow");;
		System.out.println(a.name);//bigyellow
		//System.out.println(a.furname);//error!
		System.out.println(a instanceof Animal);//true
		System.out.println(a instanceof Dog);//true
		Dog d1=(Dog)a;//要加強製你轉換符
		System.out.println(d1.furcolor);//yellow
		
	}
}


輸出:

true
true
true
false
bigyellow
true
true
yellow

 

 

class Animal {
	public String name;
	Animal(String name) {
		this.name=name;
	}
}

class Cat extends Animal {
	public String eyesColor;
	Cat(String n,String c) {
		super(n);
		eyesColor=c;
	}
}

class Dog extends Animal {
	public String furcolor;
	Dog(String n,String c) {
		super(n);
		furcolor=c;
	}
}

public class test {
	public static void main(String[] args) {
		test testdemo=new test();
		Animal a=new Animal("name");
		Cat c=new Cat("catname","blue");
		Dog d=new Dog("dogname","black");
		testdemo.f(a);
		testdemo.f(c);
		testdemo.f(d);
	}
	
	public void f(Animal a) {
		System.out.println("name:"+a.name);
		if(a instanceof Cat) {
			Cat cat=(Cat) a;
			System.out.println(" "+cat.eyesColor+" eyes");
		}
			else if(a instanceof Dog) {
				Dog dog =(Dog) a;
				System.out.println(" "+dog.furcolor+" fur");
			}
	}
}
輸出:

name:name
name:catname
 blue eyes
name:dogname
 black fur


abstract class Animal {//有了抽象方法,這個類必須被聲明為抽象類
  private String name;
  Animal(String name) {this.name = name;}
  /*
  public void enjoy(){
    System.out.println("叫聲......");
  }
  */
  //抽象類的方法沒有寫的必要
  public abstract void enjoy();//隻有;沒有左右大括號定義,相當於C++裏的純虛函數
}

abstract class Cat extends Animal {
  private String eyesColor;
  Cat(String n,String c) {super(n); eyesColor = c;}
  /*
  public void enjoy() {
    System.out.println("貓叫聲......");
  }
  */
  //public abstract void enjoy();
}

class Dog extends Animal {
  private String furColor;
  Dog(String n,String c) {super(n); furColor = c;}
 
  public void enjoy() {
    System.out.println("狗叫聲......");
  }
}

class Bird extends Animal {
	 Bird() {
	 	 super("bird");
	 }
	 public void enjoy() {
    System.out.println("鳥叫聲......");
  }
}

class Lady {
    private String name;
    private Animal pet;
    Lady(String name,Animal pet) {
        this.name = name; this.pet = pet;
    }
    public void myPetEnjoy(){pet.enjoy();}
}

public class test {
    public static void main(String args[]){
        //Cat c = new Cat("catname","blue");//抽象的類是殘缺的類,NEW不出來
        Dog d = new Dog("dogname","black");
        Bird b = new Bird();
        //Lady l1 = new Lady("l1",c);
        Lady l2 = new Lady("l2",d);
        Lady l3 = new Lady("l3",b);
       //l1.myPetEnjoy();
        l2.myPetEnjoy();
        l3.myPetEnjoy();
    }
}


輸出:

狗叫聲......
鳥叫聲......

 


 final關鍵字

public class test {
	public static void main(String[] args) {
		T t=new T();
		System.out.println(t.i);
	}
}

final class T {
	final int i = 8;
	public final void m() {
		//j=9;
	}
}


輸出:

8

說白了,就是隻可以讀不可以寫

 

 

 

 接口:

interface Singer {
	public void sing();
	public void sleep();
}

interface Painter {
	public void paint();
	public void eat();
}

class Student implements Singer {
	private String name;
	
	Student(String name) {
		this.name=name;
	}
	
	public void study() {
		System.out.println("studying");
	}
	
	public String getName() {
		return name;
	}
	
	public void sing() {
		System.out.println("student is singing");
	}
	
	public void sleep() {
		System.out.println("student is sleeping");	
	}
}


class Teacher implements Singer,Painter {
	private String name;
	
	public String getString() {
		return name;
	}
	
	Teacher(String name) {this.name=name;}
	
	public void teach() {
		System.out.println("teacher is teaching");
	}
	
	public void sing() {
		System.out.println("teacher is singing");
	}
	public void sleep() {
		System.out.println("teacher is sleeping");
	}
	public void eat() {
		System.out.println("teacher is eating");
	}

	@Override
	public void paint() {
		// TODO Auto-generated method stub
		System.out.println("teacher is painting");
	}
}

public class test {
	public static void main(String[] args) {
		Singer s1=new Student("tom");
		s1.sing();
		s1.sleep();
		Teacher t1=new Teacher("jack");
		t1.eat();
		t1.sing();
		t1.sleep();
		t1.paint();
		Painter p1=(Painter)t1;
		p1.paint();
		p1.eat();
	}

}


輸出:

student is singing
student is sleeping
teacher is eating
teacher is singing
teacher is sleeping
teacher is painting
teacher is painting
teacher is eating

 

 


 

 

最後更新:2017-04-02 06:52:05

  上一篇:go SQL GETDATE()日期格式化函數
  下一篇:go GestureDetector和SimpleOnGestureListener的使用教程