15
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【swift】オーバーライドとオーバーロードの違い

Last updated at Posted at 2018-04-11

オーバーライドとオーバーロード、言葉は似ていますが全く違うものです。

#オーバーライド
継承したスーパークラスのメソッドを上書きすること

class SuperClass{
    func sayHello(){
        print("Hello")
    }
}

class SubClass: SuperClass{
    override func seyHello(){
        print("こんにちは")
    }
}

let superClass = SuperClass()
let subClass = SubClass()
superClass.sayHello() //Hello
subClass.sayHello() //こんにちは

#オーバーロード
同じクラスの中に名前は同じだが、引数や戻り値が違うメソッドを定義すること

class SomeClass{
    func sayHello(){
        print("こんにちは")
    }

    func sayHello(name: String){
        print("こんにちは" + name + "さん")
    }
}

let someClass = SomeClass()
someClass.sayHello() //こんにちは
someClass.sayHello(name: "花子") //こんにちは花子さん
15
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?