首页 > Java > java教程 > 正文

static关键字:重载、重写以及this和super的作用

Barbara Streisand
发布: 2024-10-26 07:33:30
原创
615 人浏览过

Static Keyword: Overloading, Overriding, and the Role of this and super

这篇文章扩展了我们之前对 static 关键字的讨论,重点关注静态上下文中的方法重载方法覆盖的概念方法。我们还将探讨 this 和 super 关键字在静态上下文中如何表现(或不表现)。

如果您不熟悉 static 关键字,首先了解静态变量和静态方法可能会有所帮助。

由于本文涉及 this 和 super 在静态上下文中的行为,因此您可能还想了解一下 This 关键字和 Super 关键字。


本文的关键主题:

  1. 静态方法的重载

  2. 为什么静态方法不能被覆盖

  3. 在静态上下文中使用 this 和 super 关键字

  4. 演示关键概念的示例


1. 重载静态方法

在Java中,重载允许同名的方法存在不同的参数列表。静态方法可以像实例方法一样被重载。但是,请记住,重载发生在编译时

代码示例:

package keywords.static_keyword;

public class StaticVariables {

    static int idStatic = 1;

    public StaticVariables(String name) {
        this.id = ++idStatic;
        this.name = name;
    }

    int id;
    String name;

    static void displayText() {
        System.out.println("DisplayText called. ID: " + idStatic);
    }

    // Overloaded static method with a parameter
    static void displayText(String name) {
        System.out.println("Overloaded DisplayText called. Name: " + name);
    }

    public static void main(String[] args) {
        StaticVariables.displayText();
        StaticVariables.displayText("Static Overload Example");
    }
}
登录后复制
登录后复制

解释:

  • 方法重载:displayText方法重载有两个版本——一个不带参数,一个带字符串参数。

  • 这是合法,因为Java可以在编译时期间根据参数列表区分这两个方法。


2. 为什么静态方法不能被重写

Java 不允许重写静态方法。由于静态方法绑定到类而不是对象实例,因此它们不参与运行时多态性,这是方法重写的基础。

但是,静态变量是继承的并且可以被子类访问或修改。

代码示例:

package keywords.static_keyword;

public class StaticVariables {

    static int idStatic = 1;

    public StaticVariables(String name) {
        this.id = ++idStatic;
        this.name = name;
    }

    int id;
    String name;

    static void displayText() {
        System.out.println("DisplayText called. ID: " + idStatic);
    }

    // Overloaded static method with a parameter
    static void displayText(String name) {
        System.out.println("Overloaded DisplayText called. Name: " + name);
    }

    public static void main(String[] args) {
        StaticVariables.displayText();
        StaticVariables.displayText("Static Overload Example");
    }
}
登录后复制
登录后复制

解释:

  • 无重写:注释掉的@Override方法演示了重写静态方法的尝试,这会导致编译时错误,因为重写仅适用于实例方法。
  • 静态变量继承:即使静态方法不能被重写,静态变量也是可以继承的,并且可以在子类中访问或修改。
  • 调用静态方法:静态方法必须使用类名调用,如果它们在同一个类中,则必须直接调用。

3. 在静态上下文中使用 this 和 super 关键字

  • this 关键字:指类的当前实例。由于静态方法不对任何实例进行操作,在静态上下文中使用它会导致编译时错误

例子:

package keywords.static_keyword;

public class CannotOverrideStaticMethod extends StaticVariables {

    public CannotOverrideStaticMethod(String name) {
        super(name);
    }

    // Attempting to override the static method 
    // This will cause a compile-time error
    /*
    @Override
    static void displayText() {
        System.out.println("Overridden DisplayText");
    }
    */

    @Override
    void display() {
        // Static variables are inherited from the parent class
        idStatic = 90;  // Access and modify the parent's static variable
        System.out.println("ID: " + idStatic + ", Name: " + name);
        super.display();  // Call the parent class's non-static method
    }

    // Correct way to use static methods from the parent class
    static void displayText() {
        StaticVariables.displayText(); // Call the parent class static method
    }

    public static void main(String[] args) {
        displayText();  // Calls the static method defined in this class
    }
}
登录后复制
  • super 关键字:同样,super 关键字指的是父类的实例。 由于静态方法属于类而不是实例,因此 super 在静态上下文中也是无效的

例子:

static void displayText() {
    // Cannot use 'this' in a static context
    this.display();  // --> Compile-time error
}
登录后复制

附加说明:

  • 概念 1:重写启用依赖于实例的运行时多态性。由于静态方法不与实例关联,因此无法覆盖它们。但是,静态变量是继承的并且可以在子类中访问或修改,如示例所示。
  • 概念2:this 和 super 都引用一个实例——this 引用当前对象的成员,而 super 引用父对象的成员。由于静态方法独立于任何实例运行,因此 this 和 super 都不能在静态方法或静态块中使用。

4. 关键概念总结

  • 静态方法可以重载:可以定义多个名称相同但参数列表不同的静态方法。此问题在编译时期间解决。
  • 静态方法不能被重写:由于静态方法属于类而不是实例,因此它们不支持运行时多态性。
  • 静态变量是继承的:子类中可以访问和修改父类的静态变量。
  • 静态上下文中没有 this 或 super:由于静态方法不对实例进行操作,因此不能使用 this 和 super 关键字。

结论

在这篇文章中,我们介绍了重载和重写静态方法的细微差别,讨论了在静态上下文中使用 this 和 super 的限制,并解释了静态变量在继承中的行为方式。这些概念对于理解 Java 中静态成员与实例成员的区别至关重要。


相关帖子

  • Java 基础

  • 数组面试要点

  • Java 内存基础

  • 集合框架要点

编码快乐!

以上是static关键字:重载、重写以及this和super的作用的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!