swift OOP / Classes

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift OOP / Classes相关的知识,希望对你有一定的参考价值。

import Foundation // Used to import certain features

// Class declaration
class Question {  
    // Properties
    let questionText : String
    let answer : Bool
    
    // Init function that will provide some values for when an object is created
    // More specifically, this is a designated initializer
    // It REQUIRES the following arguments when creating an object
    init(text : String, correctAnswer : Bool) {
        questionText = text
        answer = correctAnswer
    }
    
    // For custom inputs, use the convenience initializer
    // Usually, the init() is kept empty and parameterless
    // needs the self.init() to set the default values!
    convenience init(customerChosenColor : String) {
        self.init()
        color = customerChosenColor
    }
}

// To create an obj:
var question = Question(text: String, correctAnswer: Bool)
// If a SelfDrivingCar inherits from the Car class:

class SelfDrivingCar : Car {
    
}
// Overriding a particular method in an inherited class

class Animal {
  func breathe() {
  }

}

class Fish : Animal {
  // Override the breath function
  override func breathe() {
    // perform everthing your inherited class does 
    super.breathe()
    // plus do some extra stuff unique to this class
  }
}

以上是关于swift OOP / Classes的主要内容,如果未能解决你的问题,请参考以下文章

html JS.Objects.Classes.JavaScript OOP:一个简单的ES6类

Swift -POP( 面向协议编程)与OOP

何时使用Swift Structs和Classes

自动加载对象

PHP OOP - 实现 2 个类的最佳方法

Spring AOP