Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

swift - How does Class not inherit some variables?

class Model {
    let userId: Int
    let username: String
    private let description: String?

    init(userId: Int, username: String, description: String?) {
        self.userId = userId
        self.username = username
        self.description = description
    }
}

class ModelDetail: Model {
    let url: String
    let detail: String
    
    init(url: String, detail: String, userId: Int, username: String, description: String?) {
        self.url = url
        self.detail = detail
        
        super.init(userId: userId, username: username, description: description)
    }
}

I prepared this sample code to copy here. I don't want to inherit "description" value from Model class. When I try to something I am getting this error: Missing argument for parameter 'description' in call

Is it possible not to inherit some variable?

question from:https://stackoverflow.com/questions/65874886/how-does-class-not-inherit-some-variables

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot not inherit description, but you can assign a value nil to it, and simply ignore it.

If you can modify Model, you can change its init to

class Model {
    // ...

    init(userId: Int, username: String, description: String? = nil) 
    // ...
}

Since description is optional, we set it to nil by default, so ModelDetail doesn't need to set it at all:

class ModelDetail: Model {
    let url: String
    let detail: String

    init(url: String, detail: String, userId: Int, username: String) {
        self.url = url
        self.detail = detail

        super.init(userId: userId, username: username)
    }
}

If you cannot change Model, then ModelDetail can invoke Model's init with nil for description:

class ModelDetail: Model {
    // ...

    init(url: String, detail: String, userId: Int, username: String) {
        // ...

        super.init(userId: userId, username: username, description: nil)
    }
}

If you want to strictly adhere to the inheritance rules (which do not allow for "unused" fields), then you need to separate description from the model ModelDetail will be inheriting from. I.e.:

  • Have a base model, which only has userId and username
  • Both Model and ModelDetail will inherit from that base model, rather than each other:
class BasicModel {
    let userId: Int
    let username: String

    init(userId: Int, username: String) {
        self.userId = userId
        self.username = username
    }
}

class Model: BasicModel {

    private let description: String?

    init(userId: Int, username: String, description: String?) {
        self.description = description
        super.init(userId: userId, username: username)
    }
}

class ModelDetail: BasicModel {
    let url: String
    let detail: String

    init(url: String, detail: String, userId: Int, username: String) {
        self.url = url
        self.detail = detail

        super.init(userId: userId, username: username)
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...