有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (2) 个答案

  1. # 1 楼答案

    根据同一作者之前的一篇帖子——removal of function types

    1. There are two basic approaches to typing: nominal and structural. The identity of a nominal is based on its name; the identity of a structural type is based on what it is composed of (such as "tuple of int, int" or "function from int to float".)

    Most languages pick mostly nominal or mostly structural; there are not a lot of languages that successfully mix nominal and structural typing except "around the edges." Java is almost entirely nominal (with a few exceptions: arrays are a structural type, but at the bottom there is always a nominal element type; generics have a mix of nominal and structural too, and this is in fact part of the source of many of people's complaints about generics.)

    所以,数组和泛型的一部分是结构类型

    我认为结构类型可能类似于<T extends A & B><T extends A>的超类型,或者Object[]String[][]的超类型。这些类型的兼容性不仅仅基于它们的名称

  2. # 2 楼答案

    据我所知,Java完全是名义上的类型化。 如果两个对象具有相同的命名类型,则它们是类型兼容的。在Java声明类中:

    class A {
        public int value;
    }
    
    class B {
        public int value;
    }
    

    不提供任何语言构造来利用以相同顺序声明的相等成员

    而在C语言中,你可以利用(在某些情况下)声明A&B作为struct并且它们具有相同的二进制布局,这意味着您可以将它们相互复制,形成一个union并利用这种重叠