fork download
  1. class Car {
  2. var name: String
  3. let model: String
  4. let color: String
  5.  
  6. init(name: String, model: String, color: String) { // In Class, we need to implement `init` which is a constructor but in struct, it is not necessary
  7. self.name = name
  8. self.model = model
  9. self.color = color
  10. }
  11. }
  12.  
  13. let ford = Car(name:"Ford", model:"2010", color:"Red")
  14.  
  15. // Ford instance share his reference to Ferrari i.e Ferrari and Ford both having same copy of data i.e pointing to the same reference
  16. // We declared `ferrari` instance using `let` (immutable now). In class, we can always change it's variable (But variable should be declared as `var` then only it is possible)
  17.  
  18. let ferrari = ford
  19.  
  20. ferrari.name = "Ferrari"
  21.  
  22. print(ford.name) // Output : Ferrari (Because ford referring to same reference point which ferrari has)
  23. print(ferrari.name) // Output : Ferrari
Success #stdin #stdout 0s 57640KB
stdin
Standard input is empty
stdout
Ferrari
Ferrari