I have a SuperClass with several Subclasses.
The SuperClass will never be instantiated. It just contains code common to all 
subclasses.

Here an example:
I really want “onlyKnownBySubclass” to be a constant (i.e. let instead of var). 
But cannot figure out  how to do this.

class SuperClass
{
        let knownBySuperclass: Int
        var onlyKnownBySubclass: Int    //      this is a constant, only set in 
SubClass.init
        
        init(some: Int)
        {
                knownBySuperclass = some * 2
                onlyKnownBySubclass = 0 //      this value will never be used
        }
        
        final func someFunction() -> Void
        {
                print("Should never be zero: \(onlyKnownBySubclass)")
        }
}

final class SubClass: SuperClass
{
        override init(some: Int)
        {
                super.init(some: some)
                
                //      depends on knownBySuperclass:
                onlyKnownBySubclass = knownBySuperclass + 5     //      
constant - will never change after this
        }
}

let a = SubClass(some:11)
a.someFunction()        //      prints: “Should never be zero: 27”

Gerriet.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to