Class Definition
class defines reference types. Unlike struct, class instances do not copy data when assigned or passed as parameters—two variables will point to the same object. This behavior makes classes suitable for representing data that needs to be shared or mutated.
Classes are defined using the class keyword and can contain member variables, constructors, member functions, etc. Member variables must be initialized by default or assigned in constructors. Use let to declare immutable members and var to declare mutable members.
The Counter class below defines two member variables and one member function. Note that after assigning counter to sameCounter, modifying fields of sameCounter also affects counter because both point to the same object.
All instance member variables of a class must be initialized in the constructor, otherwise the compiler will report an error.
Try modifying the value of sameCounter.count and observe whether counter.count changes synchronously.