Swift2.0クラスの基礎知識

4052 ワード

クラス#クラス#
クラス属性
//
//  SomeClass.swift
//  SwiftClass
//
//  Created by    on 15/6/17.
//  Copyright © 2015    . All rights reserved.
//

import Foundation
class SomeClass{
    //    
    var value1 = 1;
    var value2:Int = 2;
    //    
    var cal:Int{
        get{
            return value2
        }
        set(newValue){
            value2 = newValue
        }
    }
    //   ,       ,        
    class var newValue:Int{
        return 29;
    }
    
    var teachers:Int = 0 {
        //     
        willSet(newTeachers){
            print("     \(newTeachers)  ")
        }
        didSet(old) {
            if teachers > old {
                print("        \(teachers - old)    ")
            } else {
                print("        \(old - teachers)    ")
            }
        }
    }
}
//
//  main.swift
//  SwiftClass
//
//  Created by    on 15/6/17.
//  Copyright © 2015    . All rights reserved.
//

import Foundation

var someClass = SomeClass()
//      
print("      ")
print(someClass.value1)
print(someClass.value2)

//      
print("      ")
print(someClass.cal)
someClass.cal = 20
print(someClass.cal)

//     
print("     ")
print(SomeClass.newValue)

//       
print("       ")
someClass.teachers = 3
      
1
2
      
2
20
     
29
       
     3  
        3    
Program ended with exit code: 0

方法
//
//  ClassMethod.swift
//  SwiftClass
//
//  Created by    on 15/6/17.
//  Copyright © 2015    . All rights reserved.
//

import Foundation

class ClassMethod {
    var score = 0
    //    
    //       
    func additive(){
        score++
        print("\(score)
") } // func subtraction(amount: Int){ score -= amount print("\(score)
") } // class func betterScore() { print("
") } // func printScore() { let score: Int = 50 print(" :\(score)") print(" :\(self.score)
") } } class NewClassMethod: ClassMethod { override class func betterScore() { print(" ,
") } override func additive() { score += 3 print(" ,
") } }
//
//  main.swift
//  SwiftClass
//
//  Created by    on 15/6/17.
//  Copyright © 2015    . All rights reserved.
//

import Foundation

var classMethod = ClassMethod()

//      
//      
print("      ")
classMethod.additive()

//      
print("      ")
classMethod.subtraction(10)

//     
print("     ")
ClassMethod.betterScore()

//                
print("                ")
classMethod.printScore()

//        
var newClassMethod = NewClassMethod()
//        
print("        ")
newClassMethod.subtraction(20)

//          
print("          ")
newClassMethod.additive()

//        
print("        ")
NewClassMethod.betterScore()

      
1

      
-9

     
            

                
    :50
    :-9 

        
-20

          
  ,       

        
  ,      

Program ended with exit code: 0

下付きスクリプト
//
//  ClassSubScript.swift
//  SwiftClass
//
//  Created by    on 15/6/18.
//  Copyright © 2015    . All rights reserved.
//

import Foundation

class Experience {
    
    var age:[Int] = Array(count: 5, repeatedValue: 0)
    //    
    subscript(index:Int) -> Int {
        //  
        get{
            return age[index]
        }
        //  
        set{
            age[index] = newValue
        }
    }
    
    
}
import Foundation

var ex = Experience()

ex[0] = 5
ex[1] = 6

print(ex[0])
print(ex[1])
5
6
Program ended with exit code: 0