基础的练习代码如下:
// print("hello world");
// let explictFloat : Float = 4;
// print(explictFloat)
// let label = "My age is ";
// let age = 18;
// let labelAge = label + String(age);
// print(labelAge);
// let apples = 3;
// let oranges = 5;
// let appleSummary = "I have \(apples) apples\n";
// print(appleSummary);
// let fruitSummary = "I have \(apples + oranges) pieces of fruit";
// print(fruitSummary);
// let abc = 38.8;
// let label = "My number is \(abc)";
// print(label);
// var shoppingList = ["apple","orange","banana","milk"]; //数组
// print(shoppingList[1]);
// var peopleSex = ["Jack" : "I'm a boy","Lisa" : "I'm a girl"]; //字典
// print(peopleSex["Jack"]);
// let individualscores = [75,43,103,87,12]; //if else 进行判断
// var teamscore = 0;
// for score in individualscores {
//
// if score > 50 {
// teamscore += 3;
// } else {
// teamscore += 1;
// }
//
// }
//
// print(teamscore);
// var optionalName: String? = "Jack"; //用问号来标记某个变量的值是可选的
// optionalName = nil;
// var greeting = "Hello!";
// if let name = optionalName {
// greeting = greeting + String(name);
// print(greeting);
//
// }else{
// greeting = greeting + String(optionalName);//+ "Lisa";
// print(greeting);
// }
// let vegetable = "red pepper"; //switch 的使用
//
// switch vegetable {
// case "celery":
// let vegetableComment = "Hi I'm will go home";
// print(vegetableComment);
// case "cucumber","watercress":
// let vegetableComment = "I'm lisa,come here";
// print(vegetableComment);
// case let x where x.hasSuffix("pepper"):
// let vegetableComment = "Is it a spicy \(x)?";
// print(vegetableComment);
// default:
// let vegetableComment = "EveryThing is taste good in a soup";
// print(vegetableComment);
// }
//
// let interestingNumbers = [ //for-in 实现字典的遍历
// "Prime" : [2,3,5,7,11,13],
// "Fibonacci" : [1,1,2,8],
// "Square" : [1,4,9,16,25],
// ]
//
// var largest = 0;
//
// for (kind,numbers) in interestingNumbers {
//
// for number in numbers {
// if number > largest {
// largest = number;
// }
// }
// print(largest);
//// print(kind,largest);
// }
//
// var n = 2; //while实现循环
// while n < 100 {
// n = n*2;
// }
//
// print(n);
//
// var m = 2; //repeat-while 实现循环
//
// repeat {
// m = m*2;
// }while m < 100
//
// print(m);
// //使用 for 循环
// var firstForLoop = 0;
//
// for var i = 0;i < 3;++i {
//
// firstForLoop += i;
// }
//
// print(firstForLoop);
// //函数和闭包
// func greet(name: String,day: String) ->String{
//
// return "Hello \(name),Today is \(day).";
// }
//
// print(greet("Jack",day: "Tuesday"));
// func getGasPrice() ->(Double,Double,Double){ //使用一个元组来返回多个值。
//
// return (3.59,3.69,3.79);
// }
//
//
// print(getGasPrice());
// //函数的参数是可变的,用一个数组来获取它们
//
// func sumOf(numbers: Int...) ->Int{
//
// var sum = 0;
// for number in numbers {
//
// sum += number;
// }
// print(sum);
// return sum;
// }
//
//// sumOf();
// sumOf(42,597,12);
class nameShape {
var numberOfsides: Int = 0;
var name: String;
init(name: String){
self.name = name;
}
func simpleDescription() ->String{
return "A shape with \(numberOfsides) sides.";
}
}
class Square:nameShape{
var sideLength: Double;
init(sideLength: Double,name: String){
self.sideLength = sideLength;
super.init(name: name);
numberOfsides = 4;
}
func area() ->Double {
return sideLength * sideLength;
}
override func simpleDescription() -> String {
return "A square with sides of length \(sideLength)";
}
}
// let test = Square(sideLength: 5.5,name: "Lili");
//
// let area = test.area();
// print(area);
//
// let description = test.simpleDescription();
// print(description);
// class Circle:nameShape { //圆的类定义
// var radius: Double;
// init(radius: Double,name: String){
// self.radius = radius;
// super.init(name: name);
// }
//
// func area() ->Double{
//
// return M_PI * radius * radius;
//
// }
//
// override func simpleDescription() -> String {
//
// return "A Cicle with radius of \(radius)";
// }
// }
//
// let test = Circle(radius: 4.8,name: "myCircle");
//
// print(test.name);
//
// let area = test.area();
// print(area);
//
// let description = test.simpleDescription();
// print(description);
//
//
class EquilateralTriangle: nameShape{
var sideLength: Double = 0.0;
init(sideLength: Double,name: String){
self.sideLength = sideLength;
super.init(name: name);
numberOfsides = 3;
}
func area() -> Double {
return sideLength * sideLength;
}
var perimeter: Double{ //属性可以有 getter 和 setter 。
get {
return 3.0 * sideLength;
}
set {
sideLength = newValue/3.0; //在 perimeter 的 setter 中,新值的名字是 newValue。你可以在 set 之后显示的设置一个名 字。
}
}
override func simpleDescription() -> String {
return "An equilateral triangle with sides of length \(sideLength)";
}
}
// let triangle = EquilateralTriangle(sideLength: 5,name: "my Triangle");
//
// print(triangle.perimeter);
//
// triangle.perimeter = 9.9;
//
// print(triangle.sideLength);
//
// let description = triangle.simpleDescription();
//
// print(description);
//如果你不需要计算属性但是需要在设置一个新值之前运行一些代码,使用willSet和didSet。 比如,下面的类确保三角形的边长总是和正方形的边长相同。
class TriangleAndSquare {
var triangle: EquilateralTriangle {
willSet {
square.sideLength = newValue.sideLength;
}
}
var square: Square {
willSet {
triangle.sideLength = newValue.sideLength;
}
}
init(size: Double,name: String){
square = Square(sideLength: size,name: name);
triangle = EquilateralTriangle(sideLength: size,name: name);
}
}
let triangleAndSquare = TriangleAndSquare(size: 10,name: "another test shape");
print(triangleAndSquare.square.sideLength);
print(triangleAndSquare.triangle.sideLength);
triangleAndSquare.square = Square(sideLength: 50,name: "larger Square");
print(triangleAndSquare.triangle.sideLength);