我有以下两个班级:
class Animal {
    public static void staticmethod(int i) {
        System.out.println("Animal : static -- " + i);
    }

    public void instanceMethod(int i) {
        System.out.println("Animal : instance -- " + i);
    }
}

class Cat extends Animal {
    public static void staticmethod(int i) {
        System.out.println("Cat : static -- " + i);
    }

    public void instanceMethod(int i) {
        System.out.println("Cat : instance -- " + i);
    }

    public static void main(String[] args) {
        Cat myCat = new Cat();
        myCat.staticmethod(1);                       // Cat : static -- 1
        myCat.instanceMethod(2);                     // Cat : instance -- 2
        System.out.println("");

        Animal myAnimal = myCat;
        Animal.staticmethod(3);                      // Animal : static -- 3 
        myAnimal.staticmethod(4);                    // Animal : static -- 4 [ ? ]
        System.out.println("");

        myAnimal.instanceMethod(5);                  // Cat : instance -- 5
    }
}

当我运行Cat时,我得到了以下结果:

Cat : static -- 1
Cat : instance -- 2

Animal : static -- 3
Animal : static -- 4

Cat : instance -- 5

我能理解1,2,3和5,但为什么#4不是:“Cat:static – 4”?
我的理解是这样的:

myAnimal = myCat意味着“myAnimal”现在与“myCat”完全相同,所以任何地方“myAnimal”都会出现,你可以用“myCat”替换它并获得相同的结果,因为myAnimal中的所有内容都与myCat中的所有内容相同,因此“myAnimal.staticmethod(4)”应与“myCat.staticmethod(4)”相同,输出应为:“Cat:static – 4”,类似于上面的“myCat.staticmethod(1)”.

但事实并非如此,为什么呢?

解决方法

从 Oracle docs开始:

8.4.8.2. Hiding (by Class Methods)

If a class C declares or inherits a static method m,then m is said to
hide any method m’,where the signature of m is a subsignature
(§8.4.2) of the signature of m’,in the superclasses and
superinterfaces of C that would otherwise be accessible to code in C.

Example 8.4.8.2-1. Invocation of Hidden Class Methods

A class (static) method that is hidden can be invoked by using a
reference whose type is the class that actually contains the
declaration of the method. In this respect,hiding of static methods
is different from overriding of instance methods. The example:

class Super {
            static String greeting() { return "Goodnight"; }
            String name() { return "Richard"; }
        }
        class Sub extends Super {
            static String greeting() { return "Hello"; }
            String name() { return "Dick"; }
        }
        class Test {
            public static void main(String[] args) {
                Super s = new Sub();
                System.out.println(s.greeting() + "," + s.name());
            }
        }

produces the output:

Goodnight,Dick

because the invocation of greeting uses the type of s,namely Super,to figure out,at compile time,which class method to invoke,whereas the invocation of name uses the class of s,namely Sub,at run time,which instance method to invoke.

Java对象赋值是什么意思?的更多相关文章

  1. 可选链

    是不是很苦恼呀,因此可选链就应运而生了将上面的访问链中的的强制解析的感叹号(!可选链可以处理各类之间的属性、方法、下标等的,在这里我只举了属性,其他的就不一一举例说明了,作为一名优秀的程序员还是要慢慢锻炼有触类旁通的能力。

  2. 如何在swift中检查一个对象是否是一种动态类类型?

    我正在实现一个名为ofType的函数,它过滤掉给定类型的所有元素.这是我的代码:在Objc中,我可以使用isKindOf()来检查对象是类的实例还是子类.在swift和as中有类似的操作,但是它们之后的类型应该是静态类型,而不是动态类型值.我不能使用类型参数T,因为在这个例子中,T等于Animal,这不是我想要的.你对如何实现这个功能有任何想法吗?

  3. ios – Swift中的isMemberOfClass

    关键字is等同于isKindOfClass.但我无法在swift中找到与isMemberOfClass相同的东西.注意:我的问题不是关于isKindOfClass或isMemberofclass之间的区别,而是问题是关于什么是Swift中isMemberofClass的等价物有人请澄清一下解决方法您正在寻找类型(:).例:

  4. 在C#中,如何创建具有不同类型对象的IEnumerable类>

    在C#中,如何使用不同类型的对象创建IEnumerable类例如:我想做一些事情:解决方法另一种方法,如果你想要一个命名集合:执行:在这里,你延伸了Collection的基础,它实现了IEnumerable.

  5. string – 使用JRE库替换StrSubstitutor

    目前我正在使用org.apache.commons.lang.text.StrSubstitutor来做:鉴于我想从我的项目中删除commons-lang依赖项,使用标准JRE库的StrSubstitutor的工作和简约实现是什么?注意:StrSubstitutor的工作方式如下:屈服于resolveString=“快速的棕色狐狸跳过懒狗.”解决方法如果性能不是优先级,则可以使用appendRep

  6. Java对象赋值是什么意思?

    我有以下两个班级:当我运行Cat时,我得到了以下结果:我能理解1,2,3和5,但为什么#4不是:“Cat:static–4”?

随机推荐

  1. 基于EJB技术的商务预订系统的开发

    用EJB结构开发的应用程序是可伸缩的、事务型的、多用户安全的。总的来说,EJB是一个组件事务监控的标准服务器端的组件模型。基于EJB技术的系统结构模型EJB结构是一个服务端组件结构,是一个层次性结构,其结构模型如图1所示。图2:商务预订系统的构架EntityBean是为了现实世界的对象建造的模型,这些对象通常是数据库的一些持久记录。

  2. Java利用POI实现导入导出Excel表格

    这篇文章主要为大家详细介绍了Java利用POI实现导入导出Excel表格,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. Mybatis分页插件PageHelper手写实现示例

    这篇文章主要为大家介绍了Mybatis分页插件PageHelper手写实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  4. (jsp/html)网页上嵌入播放器(常用播放器代码整理)

    网页上嵌入播放器,只要在HTML上添加以上代码就OK了,下面整理了一些常用的播放器代码,总有一款适合你,感兴趣的朋友可以参考下哈,希望对你有所帮助

  5. Java 阻塞队列BlockingQueue详解

    本文详细介绍了BlockingQueue家庭中的所有成员,包括他们各自的功能以及常见使用场景,通过实例代码介绍了Java 阻塞队列BlockingQueue的相关知识,需要的朋友可以参考下

  6. Java异常Exception详细讲解

    异常就是不正常,比如当我们身体出现了异常我们会根据身体情况选择喝开水、吃药、看病、等 异常处理方法。 java异常处理机制是我们java语言使用异常处理机制为程序提供了错误处理的能力,程序出现的错误,程序可以安全的退出,以保证程序正常的运行等

  7. Java Bean 作用域及它的几种类型介绍

    这篇文章主要介绍了Java Bean作用域及它的几种类型介绍,Spring框架作为一个管理Bean的IoC容器,那么Bean自然是Spring中的重要资源了,那Bean的作用域又是什么,接下来我们一起进入文章详细学习吧

  8. 面试突击之跨域问题的解决方案详解

    跨域问题本质是浏览器的一种保护机制,它的初衷是为了保证用户的安全,防止恶意网站窃取数据。那怎么解决这个问题呢?接下来我们一起来看

  9. Mybatis-Plus接口BaseMapper与Services使用详解

    这篇文章主要为大家介绍了Mybatis-Plus接口BaseMapper与Services使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  10. mybatis-plus雪花算法增强idworker的实现

    今天聊聊在mybatis-plus中引入分布式ID生成框架idworker,进一步增强实现生成分布式唯一ID,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部