前端之家收集整理的这篇文章主要介绍了
swift学习笔记之类和对象,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
class newClass {
let value1=20
var value2:Int=0
}
let newclass=newClass()
print( "\(newclass.value1)")
newclass.value2=100
class DataImportor {
var fileName=123456
}
class DataManager {
lazy var importor=DataImportor()
var data:String = ""
}
let manager = DataManager()
manager.data += "more data"
print(manager.data)
print(manager.importor.fileName)
class WalletClass {
var dollor=0.0
var cal:Double {
get{
let RMB=dollor*6.2
return RMB
}
set(RMB){
dollor=RMB/6.2
}
}
}
var mywallet=WalletClass()
mywallet.cal=20
print(mywallet.cal)
print(mywallet.dollor)
class WalletClass1 {
var money=0.0
var cal:Double {
get{
let RMB=money*6.2
return RMB
}
set {
money=newValue/6.2
}
}
}
var mywallet1=WalletClass1()
mywallet1.cal=200
print(mywallet1.cal)
print(mywallet1.money)
class personName {
var name :String=""
var returnName:String {
if(name.isEmpty){
return "NULL"
}
else {
return name
}
}
}
var person = personName()
print(person.name)
person.name="TOM"
print(person.name)
class Cls {
var count=100
class var newValue:String {
return "hello"
}
}
print(Cls.newValue)
for index in Cls.newValue.characters {
print(index)
}
var cls=Cls()
var value:Int=0
class newClass1 {
class var count:Int {
get{
let newValue=value
return newValue
}
set {
value=newValue
}
}
}
print("修改前:\(newClass1.count)")
newClass1.count=200
print("修改后:\(newClass1.count)")
print("--------------测试属性监控器------------")
class StepCounter {
var totalSteps:Int=0 {
willSet(newTotalSteps) {
print("新的值:\(newTotalSteps)")
}
didSet(old) {
if (totalSteps > old) {
print("与原来比增减了\(totalSteps - old)个值")
}
}
}
}
let stepCounter = StepCounter()
stepCounter.totalSteps=0
stepCounter.totalSteps=200
stepCounter.totalSteps=400
stepCounter.totalSteps=800
class ClassA {
var str="hello"
func printHello(){
print(str)
}
}
let clsA=ClassA()
clsA.printHello()
class AddClass {
var count:Int=0
func add(amount1:Int,amount2:Int){
count=amount1+amount2
print(count)
}
}
let counter=AddClass()
counter.add(100,amount2: 200)
class MutiplyClass {
var number:Int=0
class func Mutiply(amount1:Int,amount2:Int){
var count:Int=0
count=amount1*amount2
print(count)
}
}
class ClassB {
var count:Int=100
func printCount() {
self.count=50
print(self.count)
}
}
let clsB=ClassB()
clsB.printCount()
class ClassC {
var english:Int=0
var chinese:Int=0
var math:Int=0
subscript(index:Int)->Int {
get {
switch index {
case 0:
return english
case 1:
return chinese
case 2:
return math
default:
return 0
}
}
set{
english=newValue
chinese=newValue
math=newValue
}
}
}
var clsC=ClassC()
var i:Int=0
var sum:Int=0
for i=0;i<3;i++ {
sum += clsC[i]
}
print(sum)
clsC[0]=100
clsC[1]=200
clsC[2]=300
for i=0;i<3;i++ {
sum += clsC[i]
}
print(sum)
class Sorce {
var english:Int=130
var chinese:Int=50
var math:Int=90
subscript(index:Int)->Int {
get {
switch index {
case 0:
return english
case 1:
return chinese
case 2:
return math
default:
return 0
}
}
}
}
var myscore=Sorce()
for j in 0...2 {
print(myscore[j])
}
class mutiArrayClass {
var rows:Int=0
var columns=0
var grid=[Double]()
init(rows:Int,columns:Int) {
self.rows = rows
self.columns = columns
grid=Array(count: rows*columns,repeatedValue: 0.0)
}
func indexIsValid(row:Int,column:Int)->Bool {
return row >= 0 && row < rows && column >= 0 && column < columns
}
subscript(row:Int,column:Int)->Double {
get {
assert(indexIsValid(row,column: column),"index out of range")
return grid[(row*columns)+column]
}
set {
assert(indexIsValid(row,"index out of range")
return grid[(row*columns)+column]=newValue
}
}
}
var mutiArr=mutiArrayClass(rows: 2,columns: 2)
print("没有赋值前")
print(mutiArr[0,0])
print(mutiArr[0,1])
print(mutiArr[1,0])
print(mutiArr[1,1])
mutiArr[0,0]=1.4
mutiArr[0,1]=53.234
mutiArr[1,0]=42.55
mutiArr[1,1]=1000.345532
print("赋值后")
print(mutiArr[0,1])
class ClassD {
var count1:Int=10
subscript(index:Int)->Int {
get{
let count=index*count1
return count
}
set {
count1 = newValue
}
}
}
let clsD=ClassD()
print(clsD.count1)
print(clsD[6])
class Residence {
class var numberOfRooms :Int? {
return 100
}
var number:Int?
}
let reClass=Residence()
if let a=Residence.numberOfRooms {
print("目标值")
}
else {
print("目标值为空")
}
if let num=reClass.number {
print("目标值")
}
else {
print("目标值为空")
}
class Person {
var residence:ResidenceA?
}
class ResidenceA {
var numberOfRooms = 10
subscript(i:Int)->Int {
return i
}
func printRoomNumber() {
print("the number of room is \(numberOfRooms)")
}
}
let tom = Person()
if let roomCount = tom.residence?.numberOfRooms {
print("tom的房子有\(roomCount)个房间")
} else {
print("无法检索房间数")
}
let john = Person()
let johnResidence=ResidenceA()
john.residence=johnResidence
if let roomCount = john.residence?.numberOfRooms {
print("jhon的房子有\(roomCount)个房间")
} else {
print("无法检索房间数")
}
let Lily=Person()
if let firstRoomName = Lily.residence?[5]{
print("Lily的房子有\(firstRoomName)个房间")
} else {
print("无法检索房间数")
}
if let firstRoomName = john.residence?[5]{
print("john的房子有\(firstRoomName)个房间")
} else {
print("无法检索房间数")
}
let LinMeimei = Person()
if let f: ()=LinMeimei.residence?.printRoomNumber() {
print("打印房间数")
} else {
print("无法打印房间数")
}
if let f: ()=john.residence?.printRoomNumber() {
print("打印房间数")
} else {
print("无法打印房间数")
}
class PersonB {
var residence:ResidenceB?
}
class ResidenceB {
var address:Address?
}
class Address {
var street:String?
}
let Libai=PersonB()
let LibaiHouse = ResidenceB()
Libai.residence=LibaiHouse
let libAddress=Address()
LibaiHouse.address=libAddress
libAddress.street="china street"
if let libStreet = Libai.residence?.address?.street {
print("libai的地址为\(libStreet)")
} else {
print("无法检索住址")
}
原文链接:https://www.f2er.com/swift/323653.html