2017-01-06 1 views
1

code running in xcode수퍼 클래스의 상속 유형 속성을 상속받을 수 있습니까? 그렇다면 어떻게 재정의할까요?

"당신은 상속 된 인스턴스를 대체 할 수 있습니다"클래스 유형에 대한 계산 된 종류의 호텔이 들어,. 서브 클래스는 슈퍼 클래스의 구현을 재정의 할 수 있도록하는 대신 클래스 키워드를 사용할 수 있습니다 "또는 제공하는 속성을 입력하여 그 B가 어떻게 OV하는 A. 에서 모든 종류의 속성을 상속하지 않습니다처럼 해당 속성 " 에 대한 자신 만의 getter 및 setter ---- 애플 Swift3

//override static 
class A{ 
    var myValue = 0614 
    static var storedTypeProperty = "Some value" 
    class var overrideableComputedTypeProperty: Int { 
     return 1 
    } 
} 
class B: A { 
    storedTypeProperty = "New String" 
} 

이 보인다 Swift3 서적에서 설명한 것처럼 "상속 된 유형 특성"을 제거하십시오.

+0

달성하고자하는 것은 무엇입니까? 'static' 속성을 오버라이드 할 수는 없지만 다른 변수로 할 수 있습니다. – NSDmitry

+0

Swift3에서 언급 한 두 문장의 의미를 이해할 수 없습니다. 상속 된 유형 특성을 대체하여 해당 특성에 대한 사용자 정의 게터 및 설정자를 제공 할 수 있다고 말합니다. – cary

답변

0

문제는 사용자가 static 변수를 사용하고 있기 때문에 발생합니다. static 변수를 덮어 쓸 수 없으며 전체 정지 할 수 없습니다. 여기서 논쟁 할 점이 없다. 당신이 class A {}처럼, 대신 static 키워드의 class 키워드를 사용하고, 클래스 형을 무시할 수하는 class (안 struct)가있는 경우

첫 번째 문장 말한다. 즉, 같은 목적으로 두 개의 키워드를 사용할 수 있으며, 큰 차이점은 static을 덮어 쓸 수 없다는 것을 의미합니다.

class B: A { 
    // Class type computed property can be overwritten 
    override class var overridableClassPropery: String { 
     return "This is class B's overwritten property" 
    } 
} 

편집 :

class A { 
    // overridable computed property 
    class var overridableClassPropery: String { 
     return "This is class A's overwritten property" 
    } 

    // Not overridable `static` computed property 
    // Error will be shown if you try to override this property in class `B` 
    static var notOverridableStaticPropery: String { 
     return "Can not override this property in a subclass" 
    } 
} 

두 번째는 당신이 수퍼 클래스의 속성을 재정의하고, 다음과 같이, 서브 클래스에서 자신의 get 구현을 제공 할 수 있음을 말한다 notOverridableStaticProperyclass A이면 class B에 의해 상속되며, 이는 사용자가 class B을 통해 액세스하거나 호출 할 수 있음을 의미합니다. 하지만은 무시할 수 없으며 항상 class A에 설정된 값을 갖습니다.

print(A.notOverridableStaticPropery) // prints "This is class A's not overridable static property" 
print(B.notOverridableStaticPropery) // prints "This is class A's not overridable static property" 
+0

좋아, 아직 두 번째 문장에서 "상속 된 유형 속성"을 언급했기 때문에 B가 A에서 type 속성 "notOverridableStaticProperty"를 상속해야한다는 것을 이해하지 못한다. 그러나이 유형 속성을 호출 할 수는 없다. B, 내가 뭘 놓친거야? – cary

+0

내 대답을 편집했습니다. – dirtydanee

+0

내 문제가 해결되었습니다. 매우 – cary

관련 문제