How to use swift class inheritance
This article mainly explains "how to use swift class inheritance". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use swift class inheritance.
Base class / / Base class class Vehicle {var currentSpeed = 0.0 var description: String {return "traveling at\ (currentSpeed) miles per hour"} func makeNoise () {} / / prevent rewriting final final func stop () {print ("stoped")}} inheritance / / inheritance class Bicycle: Vehicle {var hasBasket = false} method overrides overrideclass Train: Vehicle {/ / method overrides override func makeNoise () {print ("hu wu! Hu wu! ")}} attribute rewrite overrideclass Car: Vehicle {var gear = 1 / / attribute rewrite override var description: String {return super.description +" in gear\ (gear) "}} let bicycle = Bicycle () bicycle.hasBasket = truebicycle.currentSpeed = 15.0print (bicycle.description) / / traveling at 15.0 miles per hourlet train = Train () train.makeNoise () / / hu wu! Hu Wuqilet car = Car () print (car.description) / / traveling at 0.0 miles per hour in gear 1 Thank you for reading, the above is the content of "how to use swift class inheritance", after the study of this article, I believe you have a deeper understanding of how to use swift class inheritance, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!