SHARE
TWEET

classes in swift

a guest Sep 22nd, 2019 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //simple class
  2. class Man
  3. {
  4.     //instance variables
  5.     var name:String = "Faiz"
  6.     var age:Int = 2
  7.     var isMarried=false //will be true only with neo
  8.     var moto:String = "Its all about Neo"
  9.  
  10.     //method
  11.     func plee(){
  12.         print ("\(self.self.self.self.self.self.name) says :please, please Neo, marry me")
  13.     }
  14. }
  15.  
  16. //create new instance
  17. var faiz = Man()
  18. faiz.plee()
  19.  
  20.  
  21. var michel = Man()
  22. michel.name="Michel"
  23. michel.plee()
  24.  
  25.  
  26. //class witrh c'tor
  27. class Neo
  28. {
  29.     var name:String
  30.     var moto:String
  31.  
  32.     //c'tor
  33.     init()
  34.     {
  35.         self.name="Neo"
  36.         self.moto="Baby shark, to do do do"
  37.     }
  38.  
  39.     //method
  40.     func sayHi()
  41.     {
  42.         print ("\(self.name) says \(self.moto)")
  43.     }
  44. }
  45. print ("============================================")
  46. var neo = Neo()
  47. neo.sayHi()
  48.  
  49.  
  50. class Elad{
  51.     var name:String
  52.     var moto:String
  53.  
  54.     //c'tor
  55.     init(name:String, moto:String){
  56.         self.name=name
  57.         self.moto=moto
  58.     }
  59.  
  60.     func sayHi()
  61.     {
  62.         print ("\(self.name) says \(self.moto)")
  63.     }
  64. }
  65.  
  66. //var elad = Elad(name:"elad", moto:"please take neo a way from me")
  67. print ("============================================")
  68. //elad.sayHi()
  69.  
  70.  
  71.  
  72.  
  73. //wax on , wax off
  74. class NewMan
  75. {
  76.     var name:String?  //we getting inside a nil, which is null in java
  77. }
  78.  
  79. class Phone{
  80.     var name:String
  81.     var owner:NewMan?  //optinal
  82.  
  83.     init (name:String)
  84.     {
  85.         self.name=name
  86.     }
  87.  
  88.     func setOwner(owner:NewMan){
  89.         self.owner=owner
  90.     }
  91.  
  92.     func callFriend(friend:NewMan){
  93.         print ("\(self.owner!.name!) call \(friend.name!)")
  94.     }
  95. }
  96.  
  97. var iphone = Phone(name: "iphone 11")
  98. var shlomi = NewMan()
  99. shlomi.name = "Shlomi"
  100. var elad = NewMan()
  101. elad.name = "Elad"
  102. iphone.setOwner(owner: shlomi)
  103. iphone.callFriend(friend: elad)
  104.  
  105.  
  106. print (shlomi)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top