📜  Java方法重载的 super 关键字

📅  最后修改于: 2022-05-13 01:55:40.518000             🧑  作者: Mango

Java方法重载的 super 关键字

我们使用方法重载来多次使用类似的方法。这可以通过在签名中使用不同的参数来实现。在下面的示例中,可以使用具有三个相似方法的类 GFG,尽管这三个方法被重载,但它们提供了不同的参数。 GFG 类的对象调用带有字符串参数的方法。

Java
// Java program to demonstrate the Method 
// Overloading without super keyword
  
class GFG {
    
    // Method m1 with 0 parameter.
    public void m1()
    {
        System.out.println("No parameter method");
    }
  
    // Method m1 with 1 integer parameter.
    public void m1(int i)
    {
        System.out.println("Int Parameter");
    }
  
    // Method m1 with 1 string parameter.
    public void m1(String s)
    {
        System.out.println("String Parameter");
    }
}
  
// Main Class
public class Main {
    
    public static void main(String[] args)
    {
        // Creating object for GFG class.
        // g is object of GFG class.
        GFG g = new GFG();
  
        // Here, m1 called with string parameter.
        // m1(String s) method will be called.
        g.m1("A");
    }
}


Java
class GFG {
    
    // Method m1 with 0 parameter.
    public void m1()
    {
        System.out.println("No parameter method");
    }
}
  
// tech class extends class GFG
// Using Inheritance concept.
class tech extends GFG {
    
    // Method m1 with 0 parameter.
    public void m1()
    {
        System.out.println("Single Int Parameter");
  
        // Using super keyword to call
        // m1 method from class GFG.
        super.m1();
    }
}
  
// Main Class
public class Main {
    public static void main(String[] args)
    {
        // Creating object for tech class
        // obj is the object for tech class.
        tech obj = new tech();
  
        // tech class method m1
        // will be called here.
        // tech class extends GFG class
  
        // GFG class method m1 will be
        // called inside tech class method m1.
  
        // both method m1 will be called
        // one from tech class and
        // second from GFG class.
        obj.m1();
    }
}


Java
// Java program to demonstrate the use
// of super keyword with Method
// Overloading
  
class DP {
    // Class DP method learn
    // with 0 parameter.
    public void learn() { System.out.println("Dynamic"); }
}
  
// Class CP extends class DP
class CP extends DP {
    
    // Class CP method learn
    // with 0 parameter.
    public void learn()
    {
        // Using super keyword
        // here learn will be
        // called from class DP.
        super.learn();
        System.out.println("Competetive");
    }
}
  
// Class programming extends class
// CP that is extends class DP.
class Programming extends CP {
    
    // Class programming method
    // learn with 0 parameter.
    public void learn()
    {
        // Here, learn method from class CP
        // and learn method from class DP
        // which called inside class CP learn
        // method.
        super.learn();
        System.out.println("Programming");
    }
}
  
// Main Class
public class A {
    public static void main(String[] args)
    {
        // Creating object for class Programming.
        Programming obj = new Programming();
        // here, learn method will be called
        // from Programming class.
  
        // method will be also called
        // from programming class extends
        // class CP that is extends
        // class DP.
        obj.learn();
    }
}


输出 :

String Parameter

使用超级关键字:

如果父类和子类都具有相同的方法,则子类将覆盖其父类中可用的方法。通过使用 super 关键字,我们可以利用两个类(子类和父类)来实现这一点。

  • 我们创建一个子类的对象,因为它可以继承父类的方法。
  • 在子类方法中,我们通过使用 super() 调用其父类中可用的方法。

笔记:

在子类中,可以在任何行给出 super 关键字。

Java

class GFG {
    
    // Method m1 with 0 parameter.
    public void m1()
    {
        System.out.println("No parameter method");
    }
}
  
// tech class extends class GFG
// Using Inheritance concept.
class tech extends GFG {
    
    // Method m1 with 0 parameter.
    public void m1()
    {
        System.out.println("Single Int Parameter");
  
        // Using super keyword to call
        // m1 method from class GFG.
        super.m1();
    }
}
  
// Main Class
public class Main {
    public static void main(String[] args)
    {
        // Creating object for tech class
        // obj is the object for tech class.
        tech obj = new tech();
  
        // tech class method m1
        // will be called here.
        // tech class extends GFG class
  
        // GFG class method m1 will be
        // called inside tech class method m1.
  
        // both method m1 will be called
        // one from tech class and
        // second from GFG class.
        obj.m1();
    }
}

输出 :

Single Int Parameter
No parameter method

在下面的例子中,我们创建了一个 Programming 对象,它继承了两个类 DP、CP。当使用 Programming 对象调用方法学习时,它会转到它自己的类 Programming,在它的类中我们有super.learn();它调用其父类的super.learn () 方法,现在在 CP 类中调用 super.learn() 后,它再次调用其父类的 super.learn(),即来自 DP。

Java

// Java program to demonstrate the use
// of super keyword with Method
// Overloading
  
class DP {
    // Class DP method learn
    // with 0 parameter.
    public void learn() { System.out.println("Dynamic"); }
}
  
// Class CP extends class DP
class CP extends DP {
    
    // Class CP method learn
    // with 0 parameter.
    public void learn()
    {
        // Using super keyword
        // here learn will be
        // called from class DP.
        super.learn();
        System.out.println("Competetive");
    }
}
  
// Class programming extends class
// CP that is extends class DP.
class Programming extends CP {
    
    // Class programming method
    // learn with 0 parameter.
    public void learn()
    {
        // Here, learn method from class CP
        // and learn method from class DP
        // which called inside class CP learn
        // method.
        super.learn();
        System.out.println("Programming");
    }
}
  
// Main Class
public class A {
    public static void main(String[] args)
    {
        // Creating object for class Programming.
        Programming obj = new Programming();
        // here, learn method will be called
        // from Programming class.
  
        // method will be also called
        // from programming class extends
        // class CP that is extends
        // class DP.
        obj.learn();
    }
}

输出:

Dynamic
Competetive
Programming