覆盖功能 [英] Override function

查看:99
本文介绍了覆盖功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一所着名大学提供的在线iOS课程。我不明白为什么以下代码使用覆盖并且它是合法的。

I am taking a iOS course online provided by a famous university. I don't understand why the following code use override and it is legal.


  1. 根据官方定义,我们使用覆盖来覆盖超类的方法。以下代码中的子类和超类在哪里?

  1. According to the official definition, we use override to override superclass' methods. Where is the subclass and superclass in the following code?

什么是覆盖和什么?




public override var description: String {
    return "\(url.absoluteString) (aspect ratio = \(aspectRatio))"
}



推荐答案

以下是一个例子:

您原来的班级:

class Person {
    func walk() {
        //do something
    }
}

您的子类:

class Runner: Person {
    override func walk() {
        //do something that is different from Person's walk
    }
}

Runner 类中,有一个覆盖,函数 walk 。那是因为它是 Person 的子类,它可以覆盖Person的walk函数。所以如果你实例化一个 Runner

In the Runner class, there is an override with the function walk. That is because it is a subclass of Person, and it can override Person's walk function. So If you instantiate a Runner:

var usainBolt = Runner()

你打电话给步行功能:

usainBolt.walk()

然后,它将调用您在 Runner 类中编写的overriden函数。如果你不覆盖它,它将调用你在 Person 中写的 walk 函数。

Then that will call the overriden function that you wrote in the Runner class. If you don't override it, it will call the walk function that you wrote in Person.

这篇关于覆盖功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆