What is the difference between Type and Class?

前端 未结 20 2218
梦谈多话
梦谈多话 2020-11-28 01:00

What makes a type different from class and vice versa?

(In the general language-agnostic sense)

相关标签:
20条回答
  • 2020-11-28 01:08

    To add another example of distinction: in C++ you have pointer and reference types which can refer to classes, but are not classes in and of themselves.

    Bar b; // b is of type "class Bar"
    Bar *b2 = &b; // b2 is of type "pointer to Class Bar"
    Bar &b3 = b; // b3 is of type "reference to Class Bar"
    Bar *b4[7]; // b4 is of type "7-element array of pointers to Class Bar"
    Bar ***b5; //b5 is of type "pointer to a pointer to a pointer to Class Bar"
    

    Note that only one class is involved, but a near infinite number of types can be used. In some languages, function are considered "first-class-objects" in which case, the type of a function is a class. In others, the type of a function is merely a pointer. Classes generally have the concepts of being able to hold data, as well as operations on that data.

    0 讨论(0)
  • 2020-11-28 01:09

    Different classes may describe the same type.

    Type consists of these parts:

    1. Operations = syntax
    2. Description of operations = semantics

    Class consists of these parts:

    1. Operations = syntax
    2. Implementation (= various implementations describe same semantics)

    Some notes:

    • Interface (as in Java) is not type, because it does not describe semantics (describes only syntax)

    • Subclass is not subtype, because subclass may change semantics defined in superclass, subtype cannot change supertype semantics (see Liskov Substitution Principle, e.g. this LSP example).

    0 讨论(0)
  • 2020-11-28 01:10

    Inspired by Wikipedia...

    In type theory terms;

    • A type is an abstract interface.
      Types generally represent nouns, such as a person, place or thing, or something nominalized,

    • A class represents an implementation of the type.
      It is a concrete data structure and collection of subroutines

      Different concrete classes can produce objects of the same abstract type (depending on type system).

      *For example, one might implement the type Stack with two classes: SmallStack (fast for small stacks, but scales poorly) and ScalableStack (scales well but high overhead for small stacks).*

      Similarly, a given class may have several different constructors.

    enter image description here

    The banana example.

    • A Banana type would represent the properties and functionality of bananas in general.

    • The ABCBanana and XYZBanana classes would represent ways of producing bananas.
      (Different banana suppliers in real life, or different data structures and functions to represent and draw bananas in a video game).

      The ABCBanana class could then produce particular bananas which are instances of the ABCBanana class, they would be objects of type Banana.

    It is not rare the programmer provide a single and only implementation for a type. In this case the class name is often identical with the type name. But there is still a type (which could be extracted in an interface if required), and an implementation (which would implement the separate interface) which builds instances (objects) of the class.

    0 讨论(0)
  • 2020-11-28 01:10

    Interesting question. I think aku's answer is spot on. Take the java ArrayList class as an example

    public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
    

    An instance of the ArrayList class is said to be of type of every superclass it extends and every interface it implements. Therefore, an instance of the ArrayList class has a type ArrayList, RandomAccess, Cloneable, and so forth. In other words, values (or instances) belong to one or more types, classes define what these types are.

    0 讨论(0)
  • 2020-11-28 01:12

    I always think of a 'type' as an umbrella term for 'classes' and 'primitives'.

    int foo; // Type is int, class is nonexistent.

    MyClass foo; // Type is MyClass, class is MyClass

    0 讨论(0)
  • 2020-11-28 01:12

    Type is the umbrella term for all the available object templates or concepts. A class is one such object template. So is the structure type, the Integer type, the Interface type etc. These are all types

    If you want, you can look at it this way: A type is the parent concept. All the other concepts: Class, Interface, Structure, Integer etc inherit from this concept.i.e They are types

    0 讨论(0)
提交回复
热议问题