static修饰属性:无论一个类生成了多少个对象,所有这些对象共同使用唯一一份静态的成员变量;一个对象对该成员变量进行了修改,其他对象的该静态成员变量的值也会随之发生变化。如果一个成员变量是static的,那么我们可以通过类名.成员变量名的方式来使用它(推荐使用这种方式)
static 修饰的方法也推荐使用类名.方法访问
Overriding and Hiding Methods
Instance Methods
An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.
The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This is called a covariant return type .
When overriding a method, you might want to use the @Override
annotation that instructs the compiler that you intend to override a method in the superclass. If, for some reason, the compiler detects that the method does not exist in one of the superclasses, it will generate an error. For more information on @Override
, see Annotations
.
Class Methods
If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass.
The distinction between hiding and overriding has important implications. The version of the overridden method that gets invoked is the one in the subclass. The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass. Let's look at an example that contains two classes. The first is Animal
, which contains one instance method and one class method:
public class Animal {
public static void testClassMethod() {
System.out.println("The class" + " method in Animal.");
}
public void testInstanceMethod() {
System.out.println("The instance " + " method in Animal.");
}
}
The second class, a subclass of Animal
, is called Cat
:
public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The class method" + " in Cat.");
}
public void testInstanceMethod() {
System.out.println("The instance method" + " in Cat.");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
}
The Cat
class overrides the instance method in Animal
and hides the class method in Animal
. The main
method in this class creates an instance of Cat
and calls testClassMethod()
on the class and testInstanceMethod()
on the instance.
The output from this program is as follows:
The class method in Animal. The instance method in Cat.
As promised, the version of the hidden method that gets invoked is the one in the superclass, and the version of the overridden method that gets invoked is the one in the subclass.
Modifiers
The access specifier for an overriding method can allow more, but not less, access than the overridden method. For example, a protected instance method in the superclass can be made public, but not private, in the subclass.
You will get a compile-time error if you attempt to change an instance method in the superclass to a class method in the subclass, and vice versa.
Summary
The following table summarizes what happens when you define a method with the same signature as a method in a superclass.
Note: In a subclass, you can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass methods—they are new methods, unique to the subclass.
在Java中子类是不能重写父类的静态方法的,即使子类中有一个与父类中一模一样的静态方法,也不能叫做重写。下使用的程序将会说明这一点:
package cn.sisy.staticfinal;
public class MyStatic {
public static void main(String[] args) {
M m = new N();
m.print();
}
}
class M{
static void print(){
System.out.println("M");
}
}
class N extends M {
static void print(){
System.out.println("N");
}
}
这个程序将会输出M而不是N。这个结果正好说明了这句话: The version of the hidden method that gets invoked depend s on whether it is invoked from the superclass or the subclass.
下面再使用另外一种方法说明不是重写的原因 :
@Override注解是用于表明下面的方法是重写了父类的方法。结果编译不通过。提示出错。
综合以下两点可以说明:静态方法是不能被重写的。当然,这个在实际开发中并没有太多的应用。
但是静态方法时可以继承的:
package cn.sisy.staticfinal;
public class MyStatic {
public static void main(String[] args) {
N n = new N();
n.print();
}
}
class M{
static void print(){
System.out.println("M");
}
}
class N extends M {
}
程序的运行结果是:M