//1.类
//类的写法
class Person {
//类的属性
var name: String?
var age: Int?
func sayHi() {
print("name: \(name),age: \(age)")
}
static var des: String?
static func sayHello() {
print("des: \(des)")
}
class func sayHello2() {
print("hhhahah,des: \(des)")
}
}
class Student: Person {
override func sayHi() {
print("jjjjjjjjjjjj")
}
override class func sayHello2() {
print("rrrrrrrrr")
}
}
class Rect {
var x: Int!
var y: Int!
var width: Int!
var height: Int!
var centerH: Int {
get {
return x + width/2
}
set {
print(newValue)
}
}
var centerW: Int {
get {
return y+height/2
}
set {
self.centerW=123
}
}
init(x: Int,y: Int,width: Int,height: Int) {
self.x=x
self.y=y
self.width=width
self.height=height
}
}
/*
// 1.类
// 类的写法
class Person {
// 声明属性
var name: String?
var age: Int = 0
// 声明类属性
static var country: String?
// 声明方法
func sayHi() {
print("name: \(name) age: \(age)")
}
// 声明类方法:(可以在前面添加static关键字,或者添加class关键字)
static func sayHello() {
print("你好,我是: \(country) 国家的人")
}
class func sayHello2() {
print("sayHello2")
}
// 类拥有一个构造函数,但是不包含参数
init(name: String, age: Int) {
self.name = name
self.age = age
}
}
class Student: Person {
// 重写父类的方法,需要添加 override 关键字
override func sayHi() {
print("Student SayHi: \(self.name)")
}
override class func sayHello2() {
print("Student SayHello2")
}
}
let stu = Student(name: "huahua", age: 123456789)
stu.sayHi()
Person.sayHello2()
Student.sayHello2()
// 创建对象
var person = Person(name: "da hua zi", age: 12)
person.name = "大华子"
print(person.name)
person.sayHi()
Person.country = "鸟不拉屎国"
Person.sayHello()
// 计算属性
class Rect {
var x: Int!
var y: Int!
var width: Int!
var height: Int!
// 计算属性
var centerX: Int {
get {
return x + width / 2
}
set {
print(newValue)
}
}
var centerY: Int {
get {
return y + height / 2
}
set {
print("newValue: \(newValue)")
}
}
// 构造函数
init(x: Int, y: Int, width: Int, height: Int) {
self.x = x
self.y = y
self.width = width
self.height = height
}
}
let rect = Rect(x: 10, y: 20, width: 100, height: 100)
//rect.centerX = 1000
rect.centerY = 1234567
print(rect.centerX)
print(rect.centerY)
*/
//2.值类型和引用类型
struct YanBo1 { //值类型
var price: Double
}
class LuDan1 { //引用类型
var price: Double!
}
var yaBo = YanBo1(price: 10.0)
//结构体是值类型,每赋值一次,都会拷贝一份新的内容,修改原来的,不影响新的
//类是引用类型,赋值就像是oc中的指针指向,无论通过哪一个变量名修改,都是在操作同一个对象
//3.Protocol 协议
protocol MarryProtocol {
//声明协议方法
func test1()
func test2()
}
//声明可选协议方法
@objc protocol MarryProtocol2 {
optional func test3()
func test4()
}
class LaTiao: MarryProtocol,MarryProtocol2 {
func test1() {
}
func test2() {
}
//如果实现可选协议方法,在没有父类的情况下,需要添加@objc关键字
//如果实现可选协议方法,有父类,直接写
@objc func test4() {
}
}
//4.Extension
extension LuDan1 {
func sayHello() {
print("Hello,我是卤蛋")
}
}
//给结构体扩展方法
extension Int {
func test5(a: Int) ->Int {
return 0
}
}
/*
sumClosure2 = { (a: Int, b: Int) -> Int in
return a + b
}
sumClosure2 = { (a, b) -> Int in
return a + b
}
sumClosure2 = { (a, b) in
return a + b
}
sumClosure2 = { a, b in
return a + b
}
sumClosure2 = { a, b in
a + b
}
sumClosure2 = {
$0 + $1
}
*/
//声明闭包变量
var sumClosure: ((a: Int,b: Int) ->Int)?
//可以省略参数名
var sumClosure2: (( Int, Int) ->Int)?
//赋值
sumClosure2 = { (a: Int,b: Int) ->Int in
return a+b
}
sumClosure2 = { (a,b) -> Int in
return a + b
}
sumClosure2 = { (a,b) in
return a+b
}
sumClosure2 = { a,b in
return a+b
}
sumClosure2 = { a,b in
a+b
}
sumClosure2={
$0 + $1
}
//调用
var result=sumClosure2!(12,34)
print(result)
//使用typealias 起别名
typealias Closure=(Int,Int) ->Int