您好,关于java的问题?public boolean equals(Object obj) ?

class BaseClass{
public boolean equals(Object obj) {
if(getClass() == obj.getClass()) return true
else return false
}
}
class DerivedClass extends BaseClass {}
public class ExerciseCh6_6_2 {
public static void main(String[] args){
BaseClass b1 = new BaseClass();
BaseClass b2 = new BaseClass();
DerivedClass d1 =new DerivedClass();//d1생선
DerivedClass d2 =new DerivedClass();//d2생선
if (b1.equals(d1))System.out.println("derived equals base.");
if (d1.equals(b1))System.out.println("base equals derived.");
if (b1.equals(b2))System.out.println("base equals base.");
if (d1.equals(d2))System.out.println("derived equals derived.");
}
}

结果:
base equals base.
derived equals derived.
怎么得出结果? 理解不了 谁能解释?谢谢

慕的地10843
浏览 342回答 2
2回答

慕丝7291255

public final Class<?> getClass()Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class.The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called. For example, no cast is required in this code fragment:Number n = 0;Class<? extends Number> c = n.getClass();Returns:The Class object that represents the runtime class of this object.See Also:The Java Language Specification, Third Edition (15.8.2 Class Literals)还不理解?看中文翻译public final Class<?> getClass()返回此 Object 的运行时类。返回的 Class 对象是由所表示类的 static synchronized 方法锁定的对象。实际结果类型是 Class<? extends |X|>,其中 |X| 表示清除表达式中的静态类型,该表达式调用 getClass。 例如,以下代码片段中不需要强制转换:Number n = 0;Class<? extends Number> c = n.getClass();返回:表示此对象运行时类的 Class 对象。另请参见:The Java Language Specification, Third Edition (15.8.2 Class Literals)

MMMHUHU

b1和b2是BaseClass 类的两个实例,调用的方法应该是一样的,if语句返回真的话就调用后边的输出语句,后边的d1和d2类同上边的
打开App,查看更多内容
随时随地看视频慕课网APP