重载
overloading大家都熟悉java的重载以及实现,在一个类中同一个函数以多种不同的形态出现,即函数的参数个数或者类型不一样。
例子 System.out.println();
下面简称
SOPSOP(String str)
SOP(int number)
SOP(char ch)
SOP(double num).................................这就是重载的效果
覆盖(重写) overwritting覆盖也是大家熟悉不过了的,子类继承父类,然后子类中覆盖原父类的方法,从而实例化子类后,调用的是子类的方法。
例子
public class father{
public overwritting()
{
SOP("father method");
}
public class son extends father{
public overwritting(){
SOP("son method");
}
public static void main(String args[]){
father son=new son();
son.overwritting();}
}
结果会调用子类的overwritting方法, son method。这就是覆盖。
隐藏
hide大家知道了覆盖,那么什么情况下,子类实例化后会调用父类的方法而不是子类的方法呢?(当然子类和父类的方法定义需要是一样的)大家知道static 类型的方法是不能被覆盖的,所以java利用这一个特性完成了隐藏的效果。
例子
public class father{
public static overwritting(){
SOP("father method");
}
public class son extends father{
public static overwritting(){
SOP("son method");
}
public static void main(String args[]){
father son=new son();
son.overwritting();
}
}
结果会调用父类的overwritting方法 fathetr method。这就是隐藏。 ━━━━━━━━━━━━━━━━━━━━━
方法的重写、重载及隐藏
首先,重写和隐藏是发生在两个类中的,而重载可以发生在一个类中。重写的概念就是顾名思义了:重新写一遍;方法名、参数及返回值是一模一样的,可能实现的过程不一样,为什么要重写?因为原来的方法不够perfect 或者不够strong,或者达不到开发者的实际应用要求。重载是当多个方法享有相同的名字,但是这些方法的参数不同,或者是参数的个数不同,或者是参数类型不同时。就叫方法的重载要注意的是:返回类型不能用来区分重载的方法,仅仅返回类型不同的两个同名方法是个error,这点比较容易理解了:
如果你写参数一模一样的但是返回类型不一样的方法,当调用的时候,编译器没法决定调哪个好隐藏如下,是华为的一道面试题
public class classA{
public void methodOne(int i){ }
public void methodTwo(int i){ }
public static void methodThree(int i){ }
public static void methodFour(int i){ }
}
public class classB extends classA{
public static void methodOne(int i){ }
public void methodTwo(int i){ }
public void methodThree(int i){ }
public static void methodFour(int i){ }
}
1 问那些方法隐藏了父类的方法?
2 问那些方法覆盖了父类的方法?
其实关于隐藏的概念本人不太了解,因为基本没用过,但是上题中的重写是很明显的,那就是methodTow,methodOne和methodThree就比较明显的是个错误了,子类继承父类,两个相同的方法一个是static的,一个不是,你让编译器怎么实例化啊,那剩下的就是隐藏了,根据网上搜到的概念:static 类型的方法是不能被覆盖的;也就是说子类的methodFour写了也没用,还是调用父类的methodFour方法Sample:
public class father{
public static void overwritting(){
System.out.print("father method");
}
}
public class son extends father{
public static void overwritting(){
System.out.print("son method");
}
public static void main(String args[]){
father son=new son();
son.overwritting();
}
}
以上程序的运行结果就是输出了:father method
━━━━━━━━━━━━━━━━━━━━━
重载,继承,重写和多态的区别: http://www.oioj.net/blog/user2/20387/archives/2005/117656.shtml 重载,继承,重写和多态的区别:继承是子类获得父类的成员,重写是继承后重新实现父类的方法。重载是在一个类里一系列参数不同名字相同的方法。多态则是为了避免在父类里大量重载引起代码臃肿且难于维护。网上看到一个有趣的说法是:继承是子类使用父类的方法,而多态则是父类使用子类的方法。
下面的例子包含了这四种实现:
class Triangle extends Shape {
public int getSides() { return 3;}
}
class Rectangle extends Shape {
public int getSides(int i) { return i;}
}
public class Shape {
public boolean isSharp(){
return true;
}
public int getSides(){
return 0 ;
}
public int getSides(Triangle tri){
return 3 ;
}
public int getSides(Rectangle rec){
return 4 ;
}
public static void main(String[] args) {
Triangle tri = new Triangle();
System.out.println("Triangle is a type of sharp? " + tri.isSharp());
Shape shape = new Triangle();
System.out.println("My shape has " + shape.getSides() + " sides.");
}
}
红色是重载,绿色是重写,蓝色是继承,粉红是多态注意Triangle类的方法是重写,而Rectangle类的方法是重载。比较红色的和粉红的部分就可以发现多态对重载的优点:如果用重载,则在父类里要对应每一个子类都重载一个取得边数的方法;如果用多态,则父类只提供取得边数的接口,至于取得哪个形状的边数,怎样取得,在子类里各自实现(重写)。