static方法能够继承吗,求解答,谢谢
可以继承 但是假如本类是final类,则不能被继承
可以啊。在一般的类里面可以,在static类中不行
不可以继承。
static方法和实例无关,只和对象相关,不能被继承。
static方法的调用只需要使用类限定名即可,如Integer.parseInt()。
非static方法,如果调用需要先new 对象,然后通过对象调用。
[code="java"]
class A
{
public static void f()
{
System.out.println("A.f()");
}
}
class B extends A
{
public static void f()
{
System.out.println("B.f()");
}
public static void main(String[] args)
{
A a = new B();
B b = new B();
B c = (B)a;
A.f();
B.f();
a.f();
b.f();
c.f();
}
}
[/code]
结果:
[code="java"]
A.f()
B.f()
A.f()
B.f()
B.f()
[/code]
[quote]
You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
[/quote]
静态方法是不会继承的,静态方法属于类方法,和类紧密关联的,如果子类定义了同名的静态方法,将会隐藏。但还是可以通过类名来访问的。
static方法可以被继承,如父类定义一个方法,子类的实例也可以调用该方法
但是,需要注意的是,静态方法不能被重写,静态方法没有多态的概念,如果子类定义了跟父类一样签名的静态方法,那么只是对父类方法的隐藏
[code="java"]class Parent {
public static void say() {
System.out.println("hello");
}
}
class Son extends Parent {
}
public class Test {
public static void main(String[] args) {
Son.say();
}
}[/code]
[code="java"]class Parent {
public static void say() {
System.out.println("parent");
}
}
class Son extends Parent {
public static void say() {
System.out.println("son");
}
}
public class Test {
public static void main(String[] args) {
Parent p = new Son();
p.say();
}
}[/code]
如上代码,表明static方法不能被重写