摘要:类方法代码类执行了重写方法右键类执行了两个对象是相等的两个对象是不相等的重写右键年龄狗可以吃东西类执行了两个对象是相等的两个对象是不相等的
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类执行了" ); } }
/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类执行了" ); } 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类执行了" ); } 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("两个对象是不相等的"); } } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/73159.html
摘要:中的继承初始化顺序父类和子类年龄动物可以吃东西类执行了年龄狗可以吃东西类执行了对象的属性和构造方法年龄动物可以吃东西类执行了的 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...
摘要:人类说话中国人说汉语,美国人说英语继承实现人类具有说话的能力中国人说汉语美国人说英语抽象类实现中国人说汉语美国人说英语 人类说话,中国人说汉语,美国人说英语 showImg(https://segmentfault.com/img/bVbnFaM?w=305&h=188); 继承实现 /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...
阅读 1936·2021-10-25 09:48
阅读 2745·2021-09-22 14:59
阅读 1672·2019-08-29 16:52
阅读 826·2019-08-29 16:07
阅读 2279·2019-08-29 12:38
阅读 1733·2019-08-26 13:23
阅读 858·2019-08-26 11:49
阅读 3186·2019-08-26 10:56