2019年6月16日 星期日

Class And Structrue


struct SomeStructure{
    
}

class SomeClass{
    
}

struct Resolution{
    //在struct內用var定義的稱為property
    var width = 0;
    var height = 0;
}
class VideoMode{
    var resolution = Resolution();
    var interlaced = false;
    var frameRate = 0.0;
    var name:String?;
}
//default初始化Resolution,width = 0,height = 0 
let someResolution = Resolution();
print(someResolution.width);
print(someResolution.height);

let someVideoMode = VideoMode();
someVideoMode.resolution.width = 1280
someVideoMode.resolution.height

let vga = Resolution(width: 640, height: 480)
vga.width;
vga.height;

let hd = Resolution(width: 1920, height: 1080);
var cinema = hd
cinema.width = 2048
hd.width

enum CompassPoiont{
    case north, south, east, west
    mutating func turnNorth(){
        self = .north;
    }
}
var currentDirection = CompassPoiont.west
//rememberedDirection = currentDirection = west
let rememberedDirection = currentDirection
currentDirection.turnNorth();

//rememberedDirection.turnNorth()會出錯!因為rememberedDirection設定為let,值不可改!
print(currentDirection);
print(rememberedDirection);

//referenct
let tenEighty = VideoMode();
tenEighty.resolution = hd;
tenEighty.interlaced = true;
tenEighty.name = "1080i";
tenEighty.frameRate = 25.0;

let alsoTenEighty = tenEighty;
alsoTenEighty.frameRate = 30.0;
print(tenEighty.frameRate);
//識別運算子
//===
//!===
//是不是指向同一個實體
if tenEighty === alsoTenEighty {
    print("遙控同一個實體!");
}

沒有留言:

張貼留言