“Initialiseur Swift” Réponses codées

Initialiseur Swift

class Bike {

  // properties with no default values
  var name: String
  var gear: Int

  // assign value using initializer
  init(name: String, gear: Int){
    self.name = name
    self.gear = gear
  }
}

// object of Person with custom initializer 
var bike1 = Bike(name: "BMX Bike", gear: 2)

print("Name: \(bike1.name) and Gear: \(bike1.gear)")
SAMER SAEID

Initialiseur Swift

// declare a class
class  Wall {
  var length: Double

  // initializer to initialize property
  init() {
    length = 5.5
    print("Creating a wall.")
    print("Length = ", length)
  }
}

// create an object
var wall1 = Wall()
SAMER SAEID

Initialiseur Swift

// declare a class
class  Wall {
  var length: Double

  // initializer to initialize property
  init() {
    length = 5.5
    print("Creating a wall.")
    print("Length = ", length)
  }
}

// create an object
var wall1 = Wall()
SAMER SAEID

Initialiseur Swift

class Wall {
  
  var length: Double
  ...
  
  // initializer with parameter
  init(length: Double) {

    self.length = length
  } 
}

// create an object
var wall1 = Wall(length: 10.5)
SAMER SAEID

Réponses similaires à “Initialiseur Swift”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code