- 最简答的类和对象
@H_301_8@import UIKit @H_301_8@
@H_301_8@class Shape{ @H_301_8@ var numberOfSides=0; @H_301_8@ func ReturnDescription() ->String{ @H_301_8@ return "it has \(numberOfSides) sides"; @H_301_8@ } @H_301_8@} @H_301_8@class ViewController: UIViewController { @H_301_8@
@H_301_8@ override func viewDidLoad() { @H_301_8@ super.viewDidLoad() @H_301_8@ // Do any additional setup after loading the view,typically from a nib. @H_301_8@ var MyShape=Shape(); @H_301_8@ MyShape.numberOfSides=3; @H_301_8@ var StringFromClass=MyShape.ReturnDescription(); @H_301_8@ println("\(StringFromClass)"); @H_301_8@ @H_301_8@ } @H_301_8@
@H_301_8@ override func didReceiveMemoryWarning() { @H_301_8@ super.didReceiveMemoryWarning() @H_301_8@ // Dispose of any resources that can be recreated. @H_301_8@ } @H_301_8@
@H_301_8@
@H_301_8@} @H_301_8@
@H_301_8@但是一般都需要有构造器,需要加init方法 @H_301_8@
@H_301_8@import UIKit @H_301_8@
@H_301_8@class ShapeWithInit{ @H_301_8@ var numberOfSide:Int;//类的属性 @H_301_8@ init(numberOfSideGet:Int){//必须得与上面的变量名字不同 @H_301_8@ numberOfSide=numberOfSideGet; @H_301_8@ } @H_301_8@ func GetSimpleDesciption(name:String)->String{ @H_301_8@ return "hello,\(name),it has \(numberOfSide) sides" @H_301_8@ } @H_301_8@} @H_301_8@class ViewController: UIViewController { @H_301_8@
@H_301_8@ override func viewDidLoad() { @H_301_8@ super.viewDidLoad() @H_301_8@ // Do any additional setup after loading the view,typically from a nib. @H_301_8@ var myShape=ShapeWithInit(numberOfSideGet: 3);//必须的加参数,而且前面必须加构造函数的参数名称,不是属性的名称 @H_301_8@ var stringGet=myShape.GetSimpleDesciption("wtt"); @H_301_8@ println(stringGet); @H_301_8@ } @H_301_8@
@H_301_8@ override func didReceiveMemoryWarning() { @H_301_8@ super.didReceiveMemoryWarning() @H_301_8@ // Dispose of any resources that can be recreated. @H_301_8@ } @H_301_8@
@H_301_8@
@H_301_8@} @H_301_8@
@H_301_8@下面在讲下类的继承。下面的代码包括的方法的重写,每一步都有特定的意义在,需要仔细看。 @H_301_8@
@H_301_8@import UIKit @H_301_8@
@H_301_8@class ShapeWithInit{ @H_301_8@ var numberOfSide:Int;//类的属性 @H_301_8@ var shapeAdditional:String;//类的属性 @H_301_8@ init(numberOfSideFromObject:Int){//必须得与上面的变量名字不同,构造对象的时候需要传参进来。但是不一定所有的属性都需要传进来。 @H_301_8@ numberOfSide=22;//所有的属性必须在这里被初始化,但是不一定用传进来的参数。额,当然不建议这样做 @H_301_8@ shapeAdditional="this is class ShapeWhithInit"; @H_301_8@ } @H_301_8@ func GetSimpleDesciption(name:String)->String{ @H_301_8@ return "hello,\(shapeAdditional),it has \(numberOfSide) sides" @H_301_8@ } @H_301_8@} @H_301_8@class Squre:ShapeWithInit{ @H_301_8@ var lengthOfClass:Double; @H_301_8@ init(lengthFromSqureObject:Double,number:Int){//子类的构造方法中,参数多了长度 @H_301_8@ self.lengthOfClass=lengthFromSqureObject; @H_301_8@ super.init(numberOfSideFromObject: number);//父类的构造方法 @H_301_8@ numberOfSide=4; @H_301_8@ } @H_301_8@ override func GetSimpleDesciption(name: String) -> String {//如果需要重写父l类的方法需要在前面加上override关键字 @H_301_8@ return "hello,it is from fulei,additional,\(shapeAdditional).it is from zilei,lengthofclass,\(lengthOfClass),numberofside,\(numberOfSide)"; @H_301_8@ } @H_301_8@} @H_301_8@class ViewController: UIViewController { @H_301_8@
@H_301_8@ override func viewDidLoad() { @H_301_8@ super.viewDidLoad() @H_301_8@ // Do any additional setup after loading the view,typically from a nib. @H_301_8@ @H_301_8@ var mySqure=Squre(lengthFromSqureObject:12.5,number:4); @H_301_8@ var stringGet=mySqure.GetSimpleDesciption("wtt"); @H_301_8@ println(stringGet); @H_301_8@ } @H_301_8@
@H_301_8@ override func didReceiveMemoryWarning() { @H_301_8@ super.didReceiveMemoryWarning() @H_301_8@ // Dispose of any resources that can be recreated. @H_301_8@ } @H_301_8@
@H_301_8@
@H_301_8@} 原文链接:https://www.f2er.com/swift/325828.html