Swift Tutorial - Tutorialspoint

(backadmin) #1

class Maths: Subjects {


var formulae: String


init(physics: String, formulae: String) {


self.formulae = formulae


super.init(physics: physics)


}


}


let sa = [


Chemistry(physics: "solid physics", equations: "Hertz"),


Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz"),


Chemistry(physics: "Thermo physics", equations: "Decibels"),


Maths(physics: "Astro Physics", formulae: "MegaHertz"),


Maths(physics: "Differential Equations", formulae: "Cosine Series")]


let samplechem = Chemistry(physics: "solid physics", equations: "Hertz")


println("Instance physics is: (samplechem.physics)")


println("Instance equation is: (samplechem.equations)")


let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")


println("Instance physics is: (samplemaths.physics)")


println("Instance formulae is: (samplemaths.formulae)")


var chemCount = 0


var mathsCount = 0


for item in sa {


if let print = item as? Chemistry {


println("Chemistry topics are: '(print.physics)', (print.equations)")


} else if let example = item as? Maths {


println("Maths topics are: '(example.physics)', (example.formulae)")


}


}

Free download pdf