java中的重载与重写的理解
1.重载必须在同一各类中吗?
Overloading in Java can occur when two or more methods in the same class share the same name
or even if a child class shares a method with the same name as one of it’s parent classes.
2.重写必须在不同类中吗?
public class Test{
public static void main(String[] args){
System.out.println(new Test());
System.out.println(new Test(){
public String toString(){
return "manual override";
}
});
System.out.println(new Test(){
public String gm(){
return "manual gm";
}
}.gm());
} //end of main method
public String gm(){
return "gm";
}
}