摘要:類(lèi)方法代碼類(lèi)執(zhí)行了重寫(xiě)方法右鍵類(lèi)執(zhí)行了兩個(gè)對(duì)象是相等的兩個(gè)對(duì)象是不相等的重寫(xiě)右鍵年齡狗可以吃東西類(lèi)執(zhí)行了兩個(gè)對(duì)象是相等的兩個(gè)對(duì)象是不相等的
Object類(lèi) 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類(lèi)執(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); } }重寫(xiě)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類(lèi)執(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("兩個(gè)對(duì)象是相等的"); }else { System.out.println("兩個(gè)對(duì)象是不相等的"); } } }
重寫(xiě)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類(lèi)執(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("兩個(gè)對(duì)象是相等的"); }else { System.out.println("兩個(gè)對(duì)象是不相等的"); } } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/73159.html
摘要:中的繼承初始化順序父類(lèi)和子類(lèi)年齡動(dòng)物可以吃東西類(lèi)執(zhí)行了年齡狗可以吃東西類(lèi)執(zhí)行了對(duì)象的屬性和構(gòu)造方法年齡動(dòng)物可以吃東西類(lèi)執(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...
摘要:人類(lèi)說(shuō)話中國(guó)人說(shuō)漢語(yǔ),美國(guó)人說(shuō)英語(yǔ)繼承實(shí)現(xiàn)人類(lèi)具有說(shuō)話的能力中國(guó)人說(shuō)漢語(yǔ)美國(guó)人說(shuō)英語(yǔ)抽象類(lèi)實(shí)現(xiàn)中國(guó)人說(shuō)漢語(yǔ)美國(guó)人說(shuō)英語(yǔ) 人類(lèi)說(shuō)話,中國(guó)人說(shuō)漢語(yǔ),美國(guó)人說(shuō)英語(yǔ) showImg(https://segmentfault.com/img/bVbnFaM?w=305&h=188); 繼承實(shí)現(xiàn) /javaDemo6/src/com/imooc/Person.java package com.im...
java中的方法重寫(xiě) showImg(https://segmentfault.com/img/bVbnBEj?w=1260&h=668); 重寫(xiě)前 showImg(https://segmentfault.com/img/bVbnBEu?w=347&h=158); /javaDemo3/src/com/imooc/Animal.java package com.imooc; public cl...
閱讀 1969·2021-10-25 09:48
閱讀 2798·2021-09-22 14:59
閱讀 1761·2019-08-29 16:52
閱讀 866·2019-08-29 16:07
閱讀 2308·2019-08-29 12:38
閱讀 1761·2019-08-26 13:23
閱讀 883·2019-08-26 11:49
閱讀 3277·2019-08-26 10:56