求大神解释
package fuck1;//方法重载与方法重写的区别
class umbrella
{
int a;
int b;
int print()
{
return a+b;
}
int print(int a,int b)//重载方法
{
return a-b;
}
}
class umbrella1 extends umbrella
{
int print()//重写了父类的方法print()
{
return a;
}
double print(double a,double b)
{
return a*b;
}
}
public class root2
{
public static void main(String args[])
{
umbrella a1= new umbrella();
umbrella1 a2 = new umbrella1();
a1.a=13;
a1.b=12;
System.out.println(a1.print());
System.out.println(a1.print(32, 23));
a2.a=4;
a2.b=5;
System.out.println(a2.print());
//System.out.println(a2.print(10, 520));为什么加上这句原有的父类中的print()方法会被覆盖??
System.out.println(a2.print(32.5, 28.3));
}
}
------解决方案--------------------不是覆盖吧
10跟520都是整形数据,当调用a2.print(10, 520)的时会调用父类中定义的对应方法
------解决方案--------------------我怎么都没看明白LZ想问什么
umbrella a1= new umbrella();
umbrella1 a2 = new umbrella1();
a1.a=13;
a1.b=12;
System.out.println(a1.print()); //调用父类print方法
System.out.println(a1.print(32, 23)); //调用父类print(int int)方法
a2.a=4;
a2.b=5;
System.out.println(a2.print()); //调用子类方法print方法
//System.out.println(a2.print(10, 520));//调用父类print(int int)方法
System.out.println(a2.print(32.5, 28.3));//调用子类print(double double)方法
------解决方案--------------------//System.out.println(a2.print(10, 520));为什么加上这句原有的父类中的print()方法会被覆盖??
楼主这个是指什么呢?这行调用的时父类的print方法
------解决方案--------------------