📜  Java中不用super关键字访问超类方法和实例变量

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

Java中不用super关键字访问超类方法和实例变量

类描述了对象的属性和属性。一个类主要包含以下组件。

  1. 修饰符: Java中的关键字,提供对类及其成员和方法的一组限制。
  2. Class关键字:类的初始化由类保留字和类名完成。
  3. 类名:在Java中创建变量时必须遵循以下规则的类名。

超类:可以从中创建许多子类的父类。所有子类都具有父类的所有属性和属性。  

继承是面向对象语言的一种机制,通过这种机制,任何类(子类)都可以继承其他类的父类的所有属性和行为。

Parent class: GFG
Child  class: GFGChild

简单的继承格式:

Java
// Access Super Class Methods and Instance
// Variables Without Super Keyword in Java
  
import java.io.*;
  
class GFG {
    // super class (parent class)
    // instance variables
    // class methods
}
  
class GFGChild extends GFG {
    // GFGChild child class of GFG class
    // instance variables of GFGChild class
    // as well as GFG class
    // class methods of GFGChild class as well as GFG class
}


Java
// Access Super Class Methods and Instance
// Variables With Super Keyword in Java
import java.io.*;
  
// super class
class helloworld {
    
    // instance variable
    String name = "helloworld is the name";
    void print()
    {
        System.out.println("This is the helloworld class");
    }
}
// derived class
class GFG1 extends helloworld {
    
    // invoking the instance variable of parent class
    String name = super.name;
    void print()
    {
        // calling the overriden method
        super.print();
        System.out.println("This is the GFG1 class");
        
        // printing the name
        System.out.println(name);
    }
}
class GFG {
    public static void main(String[] args)
    {
        // instance of the derived class
        GFG1 ob = new GFG1();
        
        // calling the unoverriden method print
        ob.print();
    }
}


Java
// Access Super Class Methods and Instance
// Variables Without Super Keyword in Java
class Arraylist extends GFG {
    void hello()
    {
        System.out.println("This is the Main class");
    }
    public static void main(String args[])
    {
        // calling the constructor
        Arraylist ob = new Arraylist();
        // calling the inherited name method
        ob.name();
    }
}
class GFG {
    GFG()
    { // constructor of the parent  class
        System.out.println("This is the constructor");
    }
  
    void name() { System.out.println("Hello world"); }
}


子类中有两种方法可以调用超类(父类)的实例变量和方法。

1.第一种方法: super关键字是Java中的保留字之一。 Super 是指父类的一个对象。 (参考这篇文章)。

用途:

  • 我们可以在 super 关键字的帮助下调用父类的重写方法。
  • super() 用于执行父类的构造函数,应该在派生类构造函数的第一行使用。

执行:

Java

// Access Super Class Methods and Instance
// Variables With Super Keyword in Java
import java.io.*;
  
// super class
class helloworld {
    
    // instance variable
    String name = "helloworld is the name";
    void print()
    {
        System.out.println("This is the helloworld class");
    }
}
// derived class
class GFG1 extends helloworld {
    
    // invoking the instance variable of parent class
    String name = super.name;
    void print()
    {
        // calling the overriden method
        super.print();
        System.out.println("This is the GFG1 class");
        
        // printing the name
        System.out.println(name);
    }
}
class GFG {
    public static void main(String[] args)
    {
        // instance of the derived class
        GFG1 ob = new GFG1();
        
        // calling the unoverriden method print
        ob.print();
    }
}
输出
This is the helloworld class
This is the GFG1 class
helloworld is the name

2.第二种方法:继承后不使用super关键字,父类的所有方法和实例变量都被子类继承。所以我们可以在子类中引导它们。

GFG class: parent class
Arraylist class: Derived class

执行:

Java

// Access Super Class Methods and Instance
// Variables Without Super Keyword in Java
class Arraylist extends GFG {
    void hello()
    {
        System.out.println("This is the Main class");
    }
    public static void main(String args[])
    {
        // calling the constructor
        Arraylist ob = new Arraylist();
        // calling the inherited name method
        ob.name();
    }
}
class GFG {
    GFG()
    { // constructor of the parent  class
        System.out.println("This is the constructor");
    }
  
    void name() { System.out.println("Hello world"); }
}
输出
This is the constructor
Hello world