swift 학습 노트 의 계승

10752 단어 swift자바 script

//  1      2    
//1     
//2    
//3             ,                  
class StepCounter {
      
    var totalSteps:Int=0 {  //       lazy
        willSet(newTotalSteps) {
            print("   :\(newTotalSteps)")
        }

        didSet(old) {
            if (totalSteps > old) {
                print("       \(totalSteps - old)  ")
            }

        }

    }
}

class stepCounter:StepCounter {
      

}

let counter=stepCounter()
counter.totalSteps=10
counter.totalSteps=100

//4       
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
            }

        }

    }
}

class childSorce: Sorce{
      

}
let myscore=childSorce()
print(myscore[0])
print(myscore[1])

//5     

//6       swift  1>      ,            ,     2>          


//7  
//

class NewClass {
      

    var value:Int {
     // ~~1    

    return 100
    }
    class var classValue:Int {
       //~~~2      

     return 200
    }


}

class NewClass1: NewClass {
      
    override var value:Int {
    return 333
    }

    override class var classValue:Int {
      
    return 666
    }
}
let cls = NewClass1()
print(cls.value)
print(NewClass1.classValue)


// ~~~~3       


class Car {
      
    var speed:Double=0.00
}

class AutomicCar: Car {
      
    var gear = 1
    override var speed:Double {
        didSet {
        gear = Int(speed)
            print(gear)
        }
    }
}

let automic = AutomicCar()
automic.speed=34.00
automic.speed=6886.00

// ~~~~~4      
class childSorce111: Sorce{
      
    var value=0
    override subscript(index:Int)->Int {
    let count = index-value
        return count
    }
}
//~~~~~5    

//       
//@1     override
//@         ,     ,    


//8       
//  1>     super.   
//  2>       suer[index]
//  3>     super.   (   )
//  4>      



//9    
//       ,    ,  ,           final (  )             


//10    
//               
//  1>      is 
//         is    
//       @1,    true
//       @2          

class ClassA {
      

}

class ClassA1:ClassA {
      
    var movie:String="hello"
}

class ClassA2:ClassA {
      
    var movie:String="hello"
}
class ClassA3:ClassA {
      
    var movie:String="hello"
}
class ClassA4:ClassA {
      
    var movie:String="hello"
}

class ClassB:ClassA {
      
    var movie:String="hello"
}
class ClassC {
      

}


let b = ClassB()
if b is ClassB { //  truse
print("true")
}
else {
print("false")
}

let a = ClassA()
if a is ClassC {  //ClassC   
    print("true")
}
else {
    print("false")
}


//  2  as
//                   ,         as            as?
//    as   

// >1       
let bb=ClassB()

let clsbb = bb as ClassB
print("clsbb.movie==\(clsbb.movie)")


//   @1       :                    
//
//let library = [ClassB(),ClassB(),ClassB(),ClassC()]
//
//for item in library {
      
//let cls = item as  ClassB
//    print(cls)
//}


//    @2          


//  >2        as?
//                 ,    as?


//AnyObject  Any   
//            ,swift             ,   AnyObject  Any
//Any AnyObject    ,      


let library1 = [ClassA1(),ClassA2(),ClassA3(),ClassA4()]

for item in library1 {
let cls = item as  ClassA
    print(cls)
}

for item:AnyObject in library1 {
    let cls = item as! ClassA
    print(cls)
}

$(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('
').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); });

좋은 웹페이지 즐겨찾기