摘要:類方法代碼類執(zhí)行了重寫方法右鍵類執(zhí)行了兩個對象是相等的兩個對象是不相等的重寫右鍵年齡狗可以吃東西類執(zhí)行了兩個對象是相等的兩個對象是不相等的
Object類 toString()方法
/javaDemo3/src/com/imooc/Animal.java
package com.imooc; public class Animal { public Animal() { } }
/javaDemo3/src/com/imooc/Dog.java
package com.imooc; import java.util.*; public class Dog extends Animal { public Dog() { System.out.println("Dog類執(zhí)行了" ); } }
/javaDemo3/src/com/imooc/Initail.java
package com.imooc; public class Initail { public static void main(String[] args) { // TODO Auto-generated method stub Dog dog = new Dog(); System.out.println(dog); } }重寫toString()方法
/javaDemo3/src/com/imooc/Dog.java
右鍵 -> source -> genenor toString
]
package com.imooc; import java.util.*; public class Dog extends Animal { public Dog() { System.out.println("Dog類執(zhí)行了" ); } public String toString() { return "Dog [age=" + age + "]"; } }equals
/javaDemo3/src/com/imooc/Initail.java
package com.imooc; public class Initail { public static void main(String[] args) { Dog dog = new Dog(); Dog dog2 = new Dog(); if(dog.equals(dog2)) { System.out.println("兩個對象是相等的"); }else { System.out.println("兩個對象是不相等的"); } } }
重寫equals
右鍵-> source-> generate->hascode equals
/javaDemo3/src/com/imooc/Dog.java
package com.imooc; import java.util.*; public class Dog extends Animal { public int age = 20; public void eat() { System.out.println("年齡:"+age+ "狗可以吃東西" ); } public Dog() { System.out.println("Dog類執(zhí)行了" ); } public void method() { System.out.println(age); System.out.println("super" + super.age); eat(); super.eat(); } public String toString() { return "Dog [age=" + age + "]"; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Dog other = (Dog) obj; if (age != other.age) return false; return true; } }
/javaDemo3/src/com/imooc/Initail.java
package com.imooc; public class Initail { public static void main(String[] args) { Dog dog = new Dog(); Dog dog2 = new Dog(); if(dog.equals(dog2)) { System.out.println("兩個對象是相等的"); }else { System.out.println("兩個對象是不相等的"); } } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/73159.html
摘要:中的繼承初始化順序父類和子類年齡動物可以吃東西類執(zhí)行了年齡狗可以吃東西類執(zhí)行了對象的屬性和構(gòu)造方法年齡動物可以吃東西類執(zhí)行了的 java中的繼承初始化順序 showImg(https://segmentfault.com/img/bVbnBI1?w=1277&h=671); showImg(https://segmentfault.com/img/bVbnBKG?w=811&h=427...
final showImg(https://segmentfault.com/img/bVbnBWo?w=1223&h=652); super showImg(https://segmentfault.com/img/bVbnBUf?w=1221&h=646); /javaDemo3/src/com/imooc/Animal.java package com.imooc; public clas...
java中的繼承 showImg(https://segmentfault.com/img/bVbnByS?w=1155&h=726); showImg(https://segmentfault.com/img/bVbnByR?w=1146&h=708); showImg(https://segmentfault.com/img/bVbnByT?w=1152&h=701); showImg(htt...
摘要:人類說話中國人說漢語,美國人說英語繼承實現(xiàn)人類具有說話的能力中國人說漢語美國人說英語抽象類實現(xiàn)中國人說漢語美國人說英語 人類說話,中國人說漢語,美國人說英語 showImg(https://segmentfault.com/img/bVbnFaM?w=305&h=188); 繼承實現(xiàn) /javaDemo6/src/com/imooc/Person.java package com.im...
java中的方法重寫 showImg(https://segmentfault.com/img/bVbnBEj?w=1260&h=668); 重寫前 showImg(https://segmentfault.com/img/bVbnBEu?w=347&h=158); /javaDemo3/src/com/imooc/Animal.java package com.imooc; public cl...
閱讀 1972·2021-10-25 09:48
閱讀 2800·2021-09-22 14:59
閱讀 1763·2019-08-29 16:52
閱讀 870·2019-08-29 16:07
閱讀 2310·2019-08-29 12:38
閱讀 1766·2019-08-26 13:23
閱讀 886·2019-08-26 11:49
閱讀 3282·2019-08-26 10:56