Java中的重载与重写

首先定义重载(overloading)和重写(overriding)
重写(overriding):重写是面向对象编程的特性——运行时多态。子类提供了父类方法的具体实现。具体实现是在运行时根据调用对象确定的。注意两方法的签名必须相同。
重载(overloading):重载也是面向对象编程的特性——编译期多态,也称为静态多态。该特性允许不同的方法有相同的名称,但是有不同的签名,特别的,输入参数的个数或者类型不同。注意,不能通过返回类型的不同来区分重载方法。
问题1:可以重载静态方法吗?
答案是肯定的。我们可以有两个或多个有相同名称的静态方法,但是输入参数不同。例如:
// filename Test.java
publicclass Test {
publicstatic void foo() {
System.out.println(“Test.foo() called “);
}
publicstatic void foo(inta) {
System.out.println(“Test.foo(int) called “);
}
publicstatic void main(String args[])
{
Test.foo();
Test.foo(10);
}
}
输出为:
Test.foo() called
Test.foo(int) called
问题2:可以仅通过是否有static关键字来重载方法吗?
答案是否定的。例如:
// filename Test.java
publicclass Test {
publicstatic void foo() {
System.out.println(“Test.foo() called “);
}
publicvoid foo() { // Compiler Error: cannot redefine foo()
System.out.println(“Test.foo(int) called “);
}
publicstatic void main(String args[]) {
Test.foo();
}
}
程序运行会报错:Compiler Error, cannot redefine foo()
问题3:java中可以重写静态方法吗?
我们可以在子类中声明有相同声明的静态方法,但是这不认为是重写因为不会产生任何运行时多态。所以答案是否定的。
如果子类定义了与父类相同的静态方法,子类中的方法将会隐藏在父类中。例如:
/* Java program to show that if static method is redefined by
a derived class, then it is not overriding. */

// Superclass
classBase {

// Static method in base class which will be hidden in subclass 
publicstatic void display() {
    System.out.println("Static or class method from Base");
}

 // Non-static method which will be overridden in derived class 
 publicvoid print()  {
     System.out.println("Non-static or Instance method from Base");
}

}

// Subclass
classDerived extendsBase {

// This method hides display() in Base 
publicstatic void display() {
     System.out.println("Static or class method from Derived");
}

// This method overrides print() in Base 
publicvoid print() {
     System.out.println("Non-static or Instance method from Derived");

}
}

// Driver class
publicclass Test {
publicstatic void main(String args[ ]) {
Base obj1 = newDerived();

   // As per overriding rules this should call to class Derive's static 
   // overridden method. Since static method can not be overridden, it 
   // calls Base's display() 
   obj1.display(); 

   // Here overriding works and Derive's print() is called 
   obj1.print();    
}

}
输出:
Static or class method from Base
Non-static or Instance method from Derived
综上,对于java中方法的重写:
①对于静态方法而言,被调用的方法是在编译期确定的。
②对于实例方法而言,被调用的方法是在运行期确定的。
③实例方法不能重写静态方法,静态方法也无法隐藏实例方法。如下的例子中就有两个编译错误。
/* Java program to show that if static methods are redefined by
a derived class, then it is not overriding but hidding. */

// Superclass
classBase {

// Static method in base class which will be hidden in subclass 
publicstatic void display() {
    System.out.println("Static or class method from Base");
}

 // Non-static method which will be overridden in derived class 
 publicvoid print()  {
     System.out.println("Non-static or Instance method from Base");
}

}

// Subclass
classDerived extendsBase {

// Static is removed here (Causes Compiler Error) 
publicvoid display() {
    System.out.println("Non-static method from Derived");
}

// Static is added here (Causes Compiler Error) 
publicstatic void print() {
    System.out.println("Static method from Derived");

}
}

另外子类中,我们可以重载继承自父类的方法。这不是重写或隐藏,这样的方法是只属于子类的的方法。

翻译自:http://www.geeksforgeeks.org/can-we-overload-or-override-static-methods-in-java/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值