swift chapter11 inheritance
TRANSCRIPT
Chapter 11: Inheritance
class Shape{ //No inherited from anything var title="Shape" func area() ->Double{ return 0.0 } func description()->String{ return "I am a \(title). My area is \(area())" }}
class Square:Shape{ var sideLength = 0.0 override func area() -> Double{ return sideLength*sideLength }}
let square=Square()square.sideLength=4square.title = "SQUARE"println(square.area())println(square.description())ACCESSING SUPER FROM A SUBCLAS
class Circle:Shape{ var radius=0.0 final override func area() -> Double{ return M_PI*radius*radius }}class Sphere:Circle{ override func description() -> String{ return super.description()+".My volume is \(volume())" } func volume() ->Double{ return (4.0*super.area()*radius)/3.0 }}
let sphere = Sphere()sphere.title="Sphere"sphere.radius=2.0println(sphere.volume())println(sphere.description())
Preventing OverridesClass Identityclass Shape{ //No inherited from anything var title="Shape" func area() ->Double{ return 0.0 } func description()->String{ return "I am a \(title). My area is \(area())" }}
class Square:Shape{ var sideLength = 0.0 override func area() -> Double{ return sideLength*sideLength }}
let square=Square()square.sideLength=4square.title = "SQUARE"println(square.area())println(square.description())
class Circle:Shape{ var radius=0.0 override func area() -> Double{ return M_PI*radius*radius }}class Sphere:Circle{ override func description() -> String{ return super.description()+".My volume is \(volume())" } func volume() ->Double{ return (4.0*super.area()*radius)/3.0 }}
var sphere1 = Sphere()sphere1.title="Sphere"sphere1.radius=2.0
var square1 = Square()square1.title="Square"square1.sideLength=5.0
// println(sphere.volume())// println(sphere.description())
func printDescriptionForShape(shape:Shape){ println(shape.description())}println(printDescriptionForShape(sphere1))println(printDescriptionForShape(square1))
Exerciseclass Shape{ //No inherited from anything var title="Shape" func area() ->Double{ return 0.0 } func description()->String{ return "I am a \(title). My area is \(area())" }}
class Square:Shape{ var sideLength = 0.0 override func area() -> Double{ return sideLength*sideLength }}
let square=Square()square.sideLength=4square.title = "SQUARE"println(square.area())println(square.description())
class Circle:Shape{ var radius=0.0 override func area() -> Double{ return M_PI*radius*radius }}class Sphere:Circle{ override func description() -> String{ return super.description()+".My volume is \(volume())" } func volume() ->Double{ return (4.0*super.area()*radius)/3.0 }}
class Cylinder:Circle{ var height:Double = 0.0 func volume() ->Double{ return super.area()*height } func surfaceArea() ->Double{ return 2*super.area()+2*M_PI*height } override func description() -> String { return super.description()+". My volume is \(volume())" } }
var cylinder=Cylinder()cylinder.radius=5cylinder.height=10cylinder.title="Cylinder"println(cylinder.volume())println(cylinder.description())
http://swiftstub.com/857054676/