Override function

I take the online iOS course provided by a renowned university. I do not understand why the following code uses override, and it is legal.

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

  • What has been redefined and by what?

public override var description: String {
    return "\(url.absoluteString) (aspect ratio = \(aspectRatio))"
}
+4
source share
2 answers

Here is an example:

Original Grade:

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

Your subclass:

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

The class Runnerexists overridewith a function walk. This is because it is a subclass Person, and it can override the Human function. So, if you create an instance Runner:

var usainBolt = Runner()

walk:

usainBolt.walk()

, Runner. , walk, Person.

+6

.

. - , description. NSObject, , ( ), , NSObject, var description: String.

description - , Swift , description CustomStringConvertible. toString() Java str() Python.

?

- , . , , .

+2

Source: https://habr.com/ru/post/1652638/


All Articles