finish classes

This commit is contained in:
Gary Gan 2025-01-27 15:58:43 +08:00
parent 7392459508
commit 109bd67369
3 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,122 @@
import Foundation
class Person {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
func increaseAge() {
self.age += 1
}
}
let foo = Person(name: "Foo", age: 20)
foo.age
foo.increaseAge()
foo.age
let bar = foo
bar.increaseAge()
bar.age
foo.age
if foo === bar {
"Foo and Bar point to the same memory"
} else {
"Foo and Bar don't point to the same memory"
}
class Vehicle {
func goVroom() {
"Vroom Vroom"
}
}
class Car: Vehicle {
}
let car = Car()
car.goVroom()
class Person2 {
private(set) var age: Int
init(age: Int) {
self.age = age
}
func increaseAge() {
self.age += 1
}
}
let baz = Person2(age: 20)
baz.increaseAge()
baz.age
//baz.age += 1
class Tesla {
let manufacturer = "Tesla"
let model: String
let year: Int
init() {
self.model = "X"
self.year = 2025
}
init(model: String, year: Int) {
self.model = model
self.year = year
}
convenience init(
model: String
) {
self.init(
model: model,
year: 2024
)
}
}
class TeslaModelY: Tesla {
override init() {
super.init(model: "Y", year: 2024)
}
}
let modelY = TeslaModelY()
modelY.model
modelY.year
class MyClass {
init() {
"Intialized"
}
func doSomething() {
"Do Something"
}
deinit {
"Deinitialized"
}
}
let myClass = MyClass()
let myClosure = {
let myClass = MyClass()
myClass.doSomething()
}
myClosure()

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='7.0' target-platform='macos' swift-version='6' buildActiveScheme='true' executeOnSourceChanges='true' importAppTypes='true'/>

View File

@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:Classes.playground">
</FileRef>
<FileRef
location = "group:Enumerations.playground">
</FileRef>