Why Choose Struct Over Class?

前端 未结 16 2115

Playing around with Swift, coming from a Java background, why would you want to choose a Struct instead of a Class? Seems like they are the same thing, with a Struct offeri

16条回答
  •  一生所求
    2020-11-22 06:27

    • Structure and class are user defied data types

    • By default, structure is a public whereas class is private

    • Class implements the principal of encapsulation

    • Objects of a class are created on the heap memory

    • Class is used for re usability whereas structure is used for grouping the data in the same structure

    • Structure data members cannot be initialized directly but they can be assigned by the outside the structure

    • Class data members can be initialized directly by the parameter less constructor and assigned by the parameterized constructor

提交回复
热议问题