//1.定义一个类和结构体
//定义一个结构体
struct Resolution {
var width = 0
var height = 0
}
//定义一个类
class ViewMode {
var resolution = Resolution()
var name: String?
var frameRate = 0.0
var interlace = false
}
//类和结构体的实例
var someResolution = Resolution()
var someViewMode = ViewMode()
//属性访问
print("The resolution's width is \(someResolution.width)")
print("The resolution's width of viewMode is \(someViewMode.resolution.height)")
someViewMode.resolution.height = 80;
print("Now width is \(someViewMode.resolution.height)")
//结构体:值类型 类:引用类型
let hd = Resolution(width: 1920, height: 1024)
var cinema = hd
print(cinema.width)
cinema.width = 2040
print(cinema.width)
let tenEight = ViewMode()
tenEight.resolution = hd
tenEight.interlace = true
tenEight.name = "1080i"
tenEight.frameRate = 25.0
var alsoTenEight = tenEight
alsoTenEight.frameRate = 30.0
print("alsoTenEight.frameRect = \(tenEight.frameRate)")
if alsoTenEight === tenEight{
print("same")
}
/*
=== 等价于 表示2个类是否引用同一个实列
== 等于 表示实例的值是否相等
*/
//结构体的使用
/*
1.当我们需要封装简单的数据的时候
2.我们需要知道所封装的构造体实例再被赋值的时候 是值类型而不是引用类型
3.不会去继承另外类型的属性
*/
//2.属性
//存储属性:存储特定的类或结构体实例中的变量或者常量
struct Fixed{
var firstValue:Int
let length:Int
}
var rangOfThreeItem = Fixed(firstValue: 0, length: 3)
rangOfThreeItem .firstValue = 6
let rangOfForeItem = Fixed(firstValue: 4, length: 4)
//延迟存储属性:只有在第一次被调用的时候才会计算初始值,用关键字lazy表示
class DataImporter{
var fileName = "data.text"
}
class DataManager {
lazy var importer = DataImporter()
var data = [String]()
}
let manger = DataManager()
manger.data.append("Some data")
manger.importer.fileName = "Some.text"
//计算属性:会有Setter和Getter来间接获取和设置变量或常量的值
struct Point {
var x = 0.0
var y = 0.0
}
struct Size {
var width1 = 0.0
var height1 = 0.0
}
struct Rect{
var orign = Point()
var size = Size()
var center:Point{
get {
let centerX = orign.x + (size.width1)/2
let centerY = orign.y + (size.height1)/2
return Point(x: centerX, y: centerY)
}
set(newPoint){
orign.x = newPoint.x - (size.width1)/2
orign.y = newPoint.y - (size.height1)/2
}
}
}
var square = Rect (orign: Point(x: 0.0, y: 0.0), size: Size(width1:5,height1:5))
let center = square.center
print(center)
square.center = Point(x: 6, y: 6)
//只读属性
struct Cuboid{
var width2 = 0.0
var heigth2 = 0.0
var long = 0.0
var volume :Double{
return width2 * heigth2 * long
}
}
var cuboid = Cuboid(width2: 5, heigth2: 5, long: 5)
print(cuboid.volume)
//添加方法
class Counter{
var count = 0
func addCount(){
++count
}
func increase(acount:Int){
count = acount
}
}
var index = Counter()
index.addCount()
print(index.count)
index.increase(89)
print(index.count)
//3.扩展:为已知类添加功能
//扩展语法
class SomeType{
}
extension SomeType{
}
//计算属性
extension Double{
var km:Double{
return self * 1_000.0
}
}
var long = 24.0
print(long.km)
//方法
extension Int {
func printRepeate(index:Int){
for _ in 0...index {
print("Hello")
}
}
}
var number = 8
number.printRepeate(6)
//可变实例方法
extension Int {
mutating func mutaty (){
self = self * self
}
}
var someInt = 3
someInt.mutaty()
print(someInt)
//定义一个结构体
struct Resolution {
var width = 0
var height = 0
}
//定义一个类
class ViewMode {
var resolution = Resolution()
var name: String?
var frameRate = 0.0
var interlace = false
}
//类和结构体的实例
var someResolution = Resolution()
var someViewMode = ViewMode()
//属性访问
print("The resolution's width is \(someResolution.width)")
print("The resolution's width of viewMode is \(someViewMode.resolution.height)")
someViewMode.resolution.height = 80;
print("Now width is \(someViewMode.resolution.height)")
//结构体:值类型 类:引用类型
let hd = Resolution(width: 1920, height: 1024)
var cinema = hd
print(cinema.width)
cinema.width = 2040
print(cinema.width)
let tenEight = ViewMode()
tenEight.resolution = hd
tenEight.interlace = true
tenEight.name = "1080i"
tenEight.frameRate = 25.0
var alsoTenEight = tenEight
alsoTenEight.frameRate = 30.0
print("alsoTenEight.frameRect = \(tenEight.frameRate)")
if alsoTenEight === tenEight{
print("same")
}
/*
=== 等价于 表示2个类是否引用同一个实列
== 等于 表示实例的值是否相等
*/
//结构体的使用
/*
1.当我们需要封装简单的数据的时候
2.我们需要知道所封装的构造体实例再被赋值的时候 是值类型而不是引用类型
3.不会去继承另外类型的属性
*/
//2.属性
//存储属性:存储特定的类或结构体实例中的变量或者常量
struct Fixed{
var firstValue:Int
let length:Int
}
var rangOfThreeItem = Fixed(firstValue: 0, length: 3)
rangOfThreeItem .firstValue = 6
let rangOfForeItem = Fixed(firstValue: 4, length: 4)
//延迟存储属性:只有在第一次被调用的时候才会计算初始值,用关键字lazy表示
class DataImporter{
var fileName = "data.text"
}
class DataManager {
lazy var importer = DataImporter()
var data = [String]()
}
let manger = DataManager()
manger.data.append("Some data")
manger.importer.fileName = "Some.text"
//计算属性:会有Setter和Getter来间接获取和设置变量或常量的值
struct Point {
var x = 0.0
var y = 0.0
}
struct Size {
var width1 = 0.0
var height1 = 0.0
}
struct Rect{
var orign = Point()
var size = Size()
var center:Point{
get {
let centerX = orign.x + (size.width1)/2
let centerY = orign.y + (size.height1)/2
return Point(x: centerX, y: centerY)
}
set(newPoint){
orign.x = newPoint.x - (size.width1)/2
orign.y = newPoint.y - (size.height1)/2
}
}
}
var square = Rect (orign: Point(x: 0.0, y: 0.0), size: Size(width1:5,height1:5))
let center = square.center
print(center)
square.center = Point(x: 6, y: 6)
//只读属性
struct Cuboid{
var width2 = 0.0
var heigth2 = 0.0
var long = 0.0
var volume :Double{
return width2 * heigth2 * long
}
}
var cuboid = Cuboid(width2: 5, heigth2: 5, long: 5)
print(cuboid.volume)
//添加方法
class Counter{
var count = 0
func addCount(){
++count
}
func increase(acount:Int){
count = acount
}
}
var index = Counter()
index.addCount()
print(index.count)
index.increase(89)
print(index.count)
//3.扩展:为已知类添加功能
//扩展语法
class SomeType{
}
extension SomeType{
}
//计算属性
extension Double{
var km:Double{
return self * 1_000.0
}
}
var long = 24.0
print(long.km)
//方法
extension Int {
func printRepeate(index:Int){
for _ in 0...index {
print("Hello")
}
}
}
var number = 8
number.printRepeate(6)
//可变实例方法
extension Int {
mutating func mutaty (){
self = self * self
}
}
var someInt = 3
someInt.mutaty()
print(someInt)