在Java中,如何从派生类的重写方法调用基类的方法?

时间:2021-05-23 22:22:20

I have two Java classes: B, which extends another class A, as follows :

我有两个Java类:B,它扩展了另一个类A,如下所示:

class A {
    public void myMethod() { /* ... */ }
}

class B extends A {
    public void myMethod() { /* Another code */ }
}

I would like to call the A.myMethod() from B.myMethod(). I am coming from the C++ world, and I don't know how to do this basic thing in Java.

我想从B.myMethod()中调用A.myMethod()。我来自c++世界,我不知道如何在Java中做这个基本的事情。

12 个解决方案

#1


120  

The keyword you're looking for is super. See this guide, for instance.

你要找的关键字是super。例如,请参阅本指南。

#2


111  

Just call it using super.

用super就可以了。

public void myMethod()
{
    // B stuff
    super.myMethod();
    // B stuff
}

#3


16  

Answer is as follows:

回答如下:

super.Mymethod();
super();                // calls base class Superclass constructor.
super(parameter list);          // calls base class parameterized constructor.
super.method();         // calls base class method.

#4


14  

super.MyMethod() should be called inside the MyMethod() of the class B. So it should be as follows

MyMethod()应该在类b的MyMethod()中调用,所以应该如下所示。

class A {
    public void myMethod() { /* ... */ }
}

class B extends A {
    public void myMethod() { 
        super.MyMethod();
        /* Another code */ 
    }
}

#5


7  

I am pretty sure that you can do it using Java Reflection mechanism. It is not as straightforward as using super but it gives you more power.

我非常确定您可以使用Java反射机制来实现它。它不像使用super那么简单,但它给你更多的力量。

class A
{
    public void myMethod()
    { /* ... */ }
}

class B extends A
{
    public void myMethod()
    {
        super.myMethod(); // calling parent method
    }
}

#6


6  

call super.myMethod();

调用super.myMethod();

#7


4  

Use the super keyword.

使用超级关键字。

#8


3  

super.baseMethod(params);

call the base methods with super keyword and pass the respective params.

使用super关键字调用基本方法并传递各自的参数。

#9


3  

See, here you are overriding one of the method of the base class hence if you like to call base class method from inherited class then you have to use super keyword in the same method of the inherited class.

看,这里你重写了基类的一个方法因此,如果你想从继承类调用基类方法,那么你必须在继承类的相同方法中使用super关键字。

#10


2  

class test
{
    void message()
    {
        System.out.println("super class");
    }
}

class demo extends test
{
    int z;
    demo(int y)
    {
        super.message();
        z=y;
        System.out.println("re:"+z);
    }
}
class free{
    public static void main(String ar[]){
        demo d=new demo(6);
    }
}

#11


1  

If u r using these methods for initialization then use constructors of class A and pass super keyword inside the constructor of class B.

如果使用这些方法进行初始化,则使用类A的构造函数,并在类B的构造函数中传递超级关键字。

Or if you want to call a method of super class from the subclass method then you have to use super keyword inside the subclass method like : super.myMethod();

或者如果你想从子类的方法中调用一个超类的方法,那么你必须在子类方法中使用super关键字:super.myMethod();

#12


1  

// Using super keyword access parent class variable
class test {
    int is,xs;

    test(int i,int x) {
        is=i;
        xs=x;
        System.out.println("super class:");
    }
}

class demo extends test {
    int z;

    demo(int i,int x,int y) {
        super(i,x);
        z=y;
        System.out.println("re:"+is);
        System.out.println("re:"+xs);
        System.out.println("re:"+z);
    }
}

class free{
    public static void main(String ar[]){
        demo d=new demo(4,5,6);
    }
}

#1


120  

The keyword you're looking for is super. See this guide, for instance.

你要找的关键字是super。例如,请参阅本指南。

#2


111  

Just call it using super.

用super就可以了。

public void myMethod()
{
    // B stuff
    super.myMethod();
    // B stuff
}

#3


16  

Answer is as follows:

回答如下:

super.Mymethod();
super();                // calls base class Superclass constructor.
super(parameter list);          // calls base class parameterized constructor.
super.method();         // calls base class method.

#4


14  

super.MyMethod() should be called inside the MyMethod() of the class B. So it should be as follows

MyMethod()应该在类b的MyMethod()中调用,所以应该如下所示。

class A {
    public void myMethod() { /* ... */ }
}

class B extends A {
    public void myMethod() { 
        super.MyMethod();
        /* Another code */ 
    }
}

#5


7  

I am pretty sure that you can do it using Java Reflection mechanism. It is not as straightforward as using super but it gives you more power.

我非常确定您可以使用Java反射机制来实现它。它不像使用super那么简单,但它给你更多的力量。

class A
{
    public void myMethod()
    { /* ... */ }
}

class B extends A
{
    public void myMethod()
    {
        super.myMethod(); // calling parent method
    }
}

#6


6  

call super.myMethod();

调用super.myMethod();

#7


4  

Use the super keyword.

使用超级关键字。

#8


3  

super.baseMethod(params);

call the base methods with super keyword and pass the respective params.

使用super关键字调用基本方法并传递各自的参数。

#9


3  

See, here you are overriding one of the method of the base class hence if you like to call base class method from inherited class then you have to use super keyword in the same method of the inherited class.

看,这里你重写了基类的一个方法因此,如果你想从继承类调用基类方法,那么你必须在继承类的相同方法中使用super关键字。

#10


2  

class test
{
    void message()
    {
        System.out.println("super class");
    }
}

class demo extends test
{
    int z;
    demo(int y)
    {
        super.message();
        z=y;
        System.out.println("re:"+z);
    }
}
class free{
    public static void main(String ar[]){
        demo d=new demo(6);
    }
}

#11


1  

If u r using these methods for initialization then use constructors of class A and pass super keyword inside the constructor of class B.

如果使用这些方法进行初始化,则使用类A的构造函数,并在类B的构造函数中传递超级关键字。

Or if you want to call a method of super class from the subclass method then you have to use super keyword inside the subclass method like : super.myMethod();

或者如果你想从子类的方法中调用一个超类的方法,那么你必须在子类方法中使用super关键字:super.myMethod();

#12


1  

// Using super keyword access parent class variable
class test {
    int is,xs;

    test(int i,int x) {
        is=i;
        xs=x;
        System.out.println("super class:");
    }
}

class demo extends test {
    int z;

    demo(int i,int x,int y) {
        super(i,x);
        z=y;
        System.out.println("re:"+is);
        System.out.println("re:"+xs);
        System.out.println("re:"+z);
    }
}

class free{
    public static void main(String ar[]){
        demo d=new demo(4,5,6);
    }
}