AppDelegate.swift 文件代码如下:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// Override point for customization after application launch.
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()
//类属性和方法
//调用无参初始化方法来创建对象
//HoPeople()相当于Objective-C的[[HoPeople alloc] init]
var p: HoPeople = HoPeople();
//调用有参初始化方法来创建对象
p = HoPeople(aName: "张三", aAge: 20);
//访问People类的属性
p.name = "lisi"
//输出和格式化语句
println("name=\(p.name)");
//调用People类的方法
p.setName("王五");
var name = p.getName();
println("获得的名字是:\(name)");
p.setAge(Age: 20);
p.setAge(aAge: 22);
var sum = p.getSum(10, b: 12);
println("sum = \(sum)");
sum = p.getSum2(11, b: 21, c: 31);
println("sum = \(sum)");
var getInt = p.getInt();
var num = getInt();
println("num = \(num)");
var getInfo = p.getInfo(true);
var info = getInfo(20, "张三");
println("获得嵌套2的内容是 = \(info)");
var moreInfo = p.getMoreInfo();
println("\(moreInfo.aName)+\(moreInfo.aAge)");
var average = p.getAverage(1,2,3,4, 5, 6, 7, 8);
println("平均数是:\(average)");
///*
//继承关系
var man = HoMan();
man = HoMan(aName: "姓名", aAge: 30);
man.name = "测试姓名"
println("\(man.name)");
println("\(man.sex)");
//类方法
HoMan.test();
//条件语句
man.ifTest(20);
man.switchTest(1);
//*/
return true
}
}
HoPeople.swift 文件代码如下:
import UIKit
class HoPeople: NSObject {
//变量可以直接在外部打点访问
var name: String?
var age: Int?
var sex = true
//常量
let height = 185.0
//无参初始化方法
override init() {
println("测试结果")
}
//有参初始化方法
init(aName: String, aAge: Int) {
println("测试结果2222")
self.name = aName
self.age = aAge
}
//有参无返回的方法
func setName(aName: String) {
self.name = aName;
}
//无参有返回的方法
func getName() -> String {
return self.name!;
}
//内外部参数名不同的方法
//- (void)setAge:(int)a Age:(int)aAge
func setAge(Age aAge: Int) {
self.age = aAge;
}
//内外部参数名同名的方法
//- (void)setAge:(int)a aAge:(int)aAge
func setAge(#aAge: Int) {
self.age = aAge;
}
//多参有返回
func getSum(a: Int, b: Int) ->Int {
return a + b;
}
//方法的内部调用
func getSum2(a: Int, b: Int, c: Int) ->Int {
var sum = getSum(a, b: b)
return getSum(sum, b: c)
}
//方法的嵌套1
func getInt() ->() ->Int {
func getInt2() ->Int {
return 111;
}
return getInt2
}
//方法的嵌套2
func getInfo(flag: Bool) -> (Int, String) ->String {
func info(aAge: Int, aName: String) ->String {
//return aName + "的年龄为:" + String(aAge)
return "\(aName)的年龄为:\(aAge)"
}
return info
}
//多返回值的方法
func getMoreInfo() ->(aName: String, aAge: Int){
return (name!, age!);
}
//不定长参数的方法
func getAverage(numbers: Double...) ->Double {
var total: Double = 0
for number in numbers {
total += number
}
return total / Double(numbers.count)
}
}
import UIKit
//继承关系 HoMan具有HoPeople的所有属性和方法
class HoMan: HoPeople {
override init() {
println("子类的初始化方法");
super.init() ;
self.sex = true;
}
override init(aName: String, aAge: Int) {
super.init(aName: aName, aAge: aAge)
self.sex = true;
}
class func test() {
println("类方法调用了...");
}
//if条件语句
func ifTest(num: Int) {
if num < 5 {
println("<5");
} else if num < 15 {
println("5~15");
} else if num < 25 {
println("15~25");
}
}
//switch条件语句
func switchTest(index: Int) {
switch index {
case 1,2,3:
println("1,2,3");
case 4:
println("1,2,3");
default:
println("default");
}
var info: String = "abc"
switch info {
case "a":
println("a====");
case "abc":
println("abc====");
default:
println("Default====");
}
forTest()
}
func forTest() {
for var n=0; n<5; ++n {
//println("-----\(n)");
}
//1 2 3 4
for n in 1...5 {
//println("-----\(n)");
}
//1 2 3 4 5
for n in 1...5 {
//println("-----\(n)");
}
doWhileTest()
}
func whileTest() {
var n = 0
while n<10 {
//println("-----\(n)");
n++;
}
}
func doWhileTest() {
var n = 0;
do {
println("-----\(n)");
n++;
}while n<10
}
}