Swift inheritance is a bit confusing

How does inheritance work in Swift? In my opinion, all parents should be replaced by their children. For some reason it does not work. The following is an example:

public class Car {

var model: String

func getModel()-> String?{
   return model
}
}


 public class CompactCar: Car {
  // some codes

 }

public class carRedo{
 var cartyp:Car!
init(carType: Car){
   self.cartyp = carType
}
}

when I pass the CompactCar construct to carRedo, I get a compilation error:

carRedo(CompactCar)// error

This is mistake:

Cannot convert value of type '(CompactCar) .Type' (aka 'CompactCar.Type') to the expected argument type 'Car'

+4
source share
1 answer

You cannot pass CompactCar(or any other class name, for that matter) to the constructor carRedo. You need to pass an object of type CompactCarinstead:

let mini = CompactCar()
let redo = carRedo(mini)

, carRedo , "" :

let redo = carRedo(CompactCar())
//                           ^^
+5

Source: https://habr.com/ru/post/1628890/


All Articles