java - 关于泛型与多态的问题
迷茫
迷茫 2017-04-17 17:45:13
0
4
459

c#中推荐使用list而不用arraylist是为了避免拆箱装箱,但是我有个疑问,比如List这种父类的集合,存进了很多cat,dog这种类型的子类对象,这样效率是不是也不高呢,还是分别放在List和List中????

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all (4)
阿神

Neither of the two people upstairs understand C#.

  1. C# is also a pointer when storing class objects. If it is a non-generic type like ArrayList, or a generic type like List that specifies Object, the struct/primitive (collectively called value type) data stored in it is also boxed. Then get an address and save this address into the collection;

  2. There is no difference in Java because of type erasure. In C#, List<class type> does not have this boxing/unboxing thing.

  3. If cat and dog can be derived from animal, it means that they are not structs. The class object contains a sync block and an RTTI (a specific type descriptor) in the memory layout, see here. The specific type can be determined based on this RTTI, which has nothing to do with boxing/unboxing. Class objects do not require boxing and unboxing.

  4. The reason why value type must be boxed/unboxing is because its semantics are: a. As a whole, it is directly laid out to the current position. For example, a struct local variable is directly placed on the stack. If it is a field in a class, Directly into the memory occupied by this class object, instead of allocating a piece of memory from somewhere else and using a pointer to point to it (this is a class); and b. Therefore, every assignment must be copied, instead of using a pointer One finger is all it takes. Precisely because it lies directly, for a system without generics such as ArrayList, if structs of various sizes are allowed to lie in the ArrayList, then the element sizes will not be unequal, and arrayList[i] cannot pass offset + i * elemSize is calculated and can only be traversed element by element. This becomes a LinkedList with a time complexity of O(N) to find the Nth element, rather than an ArrayList of O(1). This is obviously not in line with your expectations, and it will be very difficult to implement the code. Very strange.

  5. In fact, you don’t need to ask about this kind of thing. There are many ready-made answers on the Internet. Of course, you have to circumvent the firewall. Google it yourself: dotnet memory layout, or even better read ECMA Spec yourself, then use Windbg and then !loadby sos clr to see it yourself.

  6. I have already posted so many links for you. If you continue to reach out, you will have to give up.

    • 大家讲道理

      Are you referring to C++ or C#? These two are different. Lists in C++ do not directly store objects, but object pointers, because only object pointers and references are polymorphic

        洪涛

        There is no difference in Java. When the compiler compiles, the type is uniformly erased to List, that is, the elements are treated as Object type, and are automatically converted to the specified type when getting.

          巴扎黑

          There is no extra overhead when converting a subclass to a parent class

            Latest Downloads
            More>
            Web Effects
            Website Source Code
            Website Materials
            Front End Template
            About us Disclaimer Sitemap
            php.cn:Public welfare online PHP training,Help PHP learners grow quickly!