【基础】Equal方法、面向对象-多态-继承-封装

时间:2024-09-21 16:33:56
package Test;

public class L3_1 {
public static void main(String[] args)
{
C c1=new C(100);
C c2=new C(100);
System.out.println(c1.equals(c2));
}
}
class B
{
private int i; B(int i)
{
this.i=i;
}
public boolean equals(B b2) //面向对象-->多态
{
if(this.i==b2.i)
return true;
else return false;
}
}
class C extends B //面向对象-->继承
{
private int j;
C(int j)
{
super(j); //初始化父类的带参数构造函数->B(int i)
this.j=j;
}
public boolean equals(B b2)
{
C c=(C)b2; //传递的参数是B类,需要强制转换
if (this.j==c.j)
return true;
else return false;
}
}