继承概念 [英] Inheritance concept

查看:155
本文介绍了继承概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在继承概念中,我在超类中有一个静态方法,我将该类继承到一个子类。在这种情况下,静态方法是否继承到子类?

In Inheritance concept, i have a static method in super class and i am inheriting that class to one sub class. In that case the static method is inherited to sub class or not?

推荐答案

如果方法是公共静态或保护静态的超类它可以在子类中访问。所以在这个意义上它是继承的。下面的代码将编译并运行正常。

If the method is public static or protected static in the superclass it will be accessible in the subclass. So in that sense it is inherited. The code below will compile and run fine.

public class A {
   public static String foo() {
     return "hello world";
   }
}

public class B extends A {
   public void bar() {
      System.out.println(foo());
   }
}

然而,这是一个不好用的术语继承因为它不依赖于特定的实例 - 因此Kaleb的回答。正常的OO设计不会将静态方法视为继承的,并且它会变得非常混乱,尤其是当您开始讨论覆盖它们时。

However, this is a bad use of the term "inherited" as it is not tied to a particular instance - hence Kaleb's answer. Normal OO design does not treat static methods as inherited, and it gets very confusing, especially when you start talking about overriding them.

这篇关于继承概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆