亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> java

Java超级

Java 超级

在本教程中,我们将通过示例了解 Java 中的 super 关键字。

super Java中的关键字在子类中用于访问超类成员(属性、构造函数和方法)。

在我们了解super之前 关键字,请务必了解 Java 继承。


超级关键字的使用

  1. 调用在子类中被覆盖的超类的方法。
  2. 如果超类和子类具有相同名称的属性,则访问超类的属性(字段)。
  3. 从子类构造函数显式调用超类无参数(默认)或参数化构造函数。

让我们了解这些用途。


1.访问超类的重写方法

如果在超类和子类中都定义了同名的方法,则子类中的方法会覆盖超类中的方法。这称为方法覆盖。

示例 1:方法覆盖

class Animal {

  // overridden method
  public void display(){
    System.out.println("I am an animal");
  }
}

class Dog extends Animal {

  // overriding method
  @Override
  public void display(){
    System.out.println("I am a dog");
  }

  public void printMessage(){
    display();
  }
}

class Main {
  public static void main(String[] args) {
    Dog dog1 = new Dog();
    dog1.printMessage();
  }
}

输出

I am a dog

在这个例子中,通过制作一个对象 dog1 类,我们可以调用它的方法 printMessage() 然后执行 display() 声明。

display() 在两个类中都有定义,子类 Dog 的方法 覆盖超类 Animal 的方法 .因此,display() 子类的被调用。

如果必须调用超类的重写方法怎么办?

我们使用 super.display() 如果被覆盖的方法 display() 超类 Animal 需要调用。

示例2:super调用超类方法

class Animal {

  // overridden method
  public void display(){
    System.out.println("I am an animal");
  }
}

class Dog extends Animal {

  // overriding method
  @Override
  public void display(){
    System.out.println("I am a dog");
  }

  public void printMessage(){

    // this calls overriding method
    display();

    // this calls overridden method
    super.display();
  }
}

class Main {
  public static void main(String[] args) {
    Dog dog1 = new Dog();
    dog1.printMessage();
  }
}

输出

I am a dog
I am an animal

到这里,上面的程序是如何工作的。


2.超类的访问属性

超类和子类可以具有同名的属性。我们使用 super 关键字来访问超类的属性。

示例3:访问超类属性

class Animal {
  protected String type="animal";
}

class Dog extends Animal {
  public String type="mammal";

  public void printType() {
    System.out.println("I am a " + type);
    System.out.println("I am an " + super.type);
  }
}

class Main {
  public static void main(String[] args) {
    Dog dog1 = new Dog();
    dog1.printType();
  }
}

输出

I am a mammal
I am an animal

在这个例子中,我们定义了相同的实例字段 type 在两个超类 Animal 和子类 Dog .

然后我们创建了一个对象 dog1 班级。然后,printType() 使用该对象调用方法。

printType() 内部 功能,

因此,System.out.println("I am a " + type); 打印 我是哺乳动物 .而且,System.out.println("I am an " + super.type); 打印 我是动物 .


3。使用 super() 访问超类构造函数

我们知道,当一个类的对象被创建时,它的默认构造函数会被自动调用。

要从子类构造函数显式调用超类构造函数,我们使用 super() .它是 super 的一种特殊形式 关键字。

super() 只能在子类构造函数内部使用,并且必须是第一个语句。

示例 4:super() 的使用

class Animal {

  // default or no-arg constructor of class Animal
  Animal() {
    System.out.println("I am an animal");
  }
}

class Dog extends Animal {

  // default or no-arg constructor of class Dog
  Dog() {

    // calling default constructor of the superclass
    super();

    System.out.println("I am a dog");
  }
}

class Main {
  public static void main(String[] args) {
    Dog dog1 = new Dog();
  }
}

输出

I am an animal
I am a dog

在这里,当一个对象 dog1 创建类时,它会自动调用该类的默认或无参数构造函数。

在子类构造函数中,super() 语句调用超类的构造函数并执行其中的语句。因此,我们得到输出 I am an animal .

然后程序流程返回到子类构造函数并执行剩余的语句。因此, 我是一只狗 将被打印出来。

但是,使用 super() 不是强制性的。即使 super() 子类构造函数中不使用,编译器会隐式调用超类的默认构造函数。

那么,如果编译器自动调用 super(),为什么还要使用冗余代码呢?

参数化构造函数(带参数的构造函数) 必须从子类构造函数中调用超类。

参数化的 super() 必须始终是子类构造函数体中的第一条语句,否则会出现编译错误。

示例5:使用super()调用参数化构造函数

class Animal {

  // default or no-arg constructor
  Animal() {
    System.out.println("I am an animal");
  }

  // parameterized constructor
  Animal(String type) {
    System.out.println("Type: "+type);
  }
}

class Dog extends Animal {

  // default constructor
  Dog() {

    // calling parameterized constructor of the superclass
    super("Animal");

    System.out.println("I am a dog");
  }
}

class Main {
  public static void main(String[] args) {
    Dog dog1 = new Dog();
  }
}

输出

Type: Animal
I am a dog

编译器可以自动调用无参数构造函数。但是,它不能调用参数化的构造函数。

如果必须调用参数化构造函数,我们需要在子类构造函数中显式定义它。

请注意,在上面的示例中,我们显式调用了参数化构造函数 super("Animal") .这种情况下编译器不会调用超类的默认构造函数。


java

  1. Java 运算符
  2. Java 评论
  3. Java if...else 语句
  4. Java for-each 循环
  5. Java break 语句
  6. Java 字符串
  7. Java 接口
  8. Java 匿名类
  9. Java try-with-resources
  10. Java 注释
  11. Java 断言
  12. Java 向量