口语化理解:
重载overload:同个类中,函数多次实现。(方法名同,参数不同)
重写override:子类覆盖父类方法。(方法名,参数,返回都同)
重载:
public class Animal{
// 仅方法名同,参数和返回都不同
public void eat(String food){
System.out.println(food);
}
public String eat(Object object){
return object.food;
}
}
重载(构造函数重载):
public class Animal{
private String name;
pricate int age;
public Animal(){
}
public Animal(String name, int age){
this.name = name;
this.age = age;
}
}
重写:
//父类
public class Animal{
public String eat(Object object){
// 返回(食物名)
return object.foodname;
}
}
// 子类
public class Dog extend Animal{
public String eat(Object object){
String fn = object.foodname;
String cl = object.color;
// 返回(食物名 + 食物颜色)
return fn + cl;
}
}