0
0

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 1 year has passed since last update.

Swift 11 Swift SubClassingとExtension概要

Posted at

inherit : receive (money, property, or a title) as an heir at the death of the previous holder.
~ appoint as heir’, from Latin in- ‘in’ + heres, hered- ‘heir’.

heir : a person legally entitled to the property or rank of another on that person's death
~ Old French from Latin 'heres' .

their : belonging to or associated with the people or things previously mentioned or easily identified.
~ Old Norse their(r)a ‘of them’,

前の章には例題で作ったClass{}はどのクラスも相続していなかった。
今度の章にはSubClassingと相続そしてExtensionの概念を見ていく

十一・壱 相続、Class{}そして下位Class{}

? hierarch : a chief priest, archbishop, or other leader.
~ Latin from Greek hierarkhēs, from hieros ‘sacred’ + arkhēs ‘ruler’.
? Hierachy: a system or organization in which people or groups are ranked one above the other according to status or authority.
~ The earliest sense was ‘system of orders of angels and heavenly beings’;

Class相続通じてClassHierachyを作ります。
Hierachyの最上位であるクラスをroot classだっと言いて、相続されたクラスをsub classだと言います。これが相続くれたクラスをsuper classだと言います。
下位クラスをいくらでも持たすことができますけど、上位クラスは一つだけです。
真っ直ぐ
上のクラスを超えて、root classを受けることもできます。

十一・弐 Overriding

Super Classで今私が必要な機能だけを取るために、
受け継いだMethod()OverRidingしてsubClass内で新しいMethod()を作ることです。

弐・甲 規則

まず、{OverRide{Method()}}SuperClass{}(Para,meter)の個数と:Type一致するべきです。
あとは、新しくてOverRide{}する{Method()}は必ずSuperClass{Method()}↑ returnする:Typeと一致するべき。
Screenshot 2023-10-18 at 4.09.23 PM.png
また、SubClass{}{OverRide{}}されたSuperClass{}Method()呼び出して重複を消えます。
Untitled.jpg

十一・参 SubClass{init{}}

SavingsAccount Class{}は金利についてvar が追加に必要です。
従って、{Instance{}}が生成する時にinterestRate Property = ...
{init{}}される様にするべきです。

 init(number: Int, balance: Float, rate: Float){
        interesRate = rate
        super.init(number: number, balance: balance)
    }

{init{}}課程で起こられる問題を避けるためにSuperClass{init{}}は後でcall.します。

十一・肆 SubClass{}使用する

let savings1 = SavingsAccount(number: 12311, balance:600.00, rate:0.07)
print(savaing1.calculateInterest())
savings1.displayBalance()

Screenshot 2023-10-18 at 5.06.46 PM.png
Screenshot 2023-10-18 at 5.13.12 PM.png

十一・伍 extension Class {}

此れは、SubClass{}を作るとか↓Referenceしなくても、既存のClass{}{method()}init(){}, ComputedProperty: {},subScript(:) ->{}などと追加するために使用できます。
標準Double Class{}で二乗値を返すプロパティと立方体値を返すプロパティを追加すると仮定します。

extension Double {
    var squared: Double {
        return self * self
    }
    var cubed: Double {
        return self * self
    }
}
let myValue: Double = 3.0
print(myValue.squared) // 9.0

注目する点はmyValue宣言する時  :TypeDoubleになる事に宣言すると
extension Propertyを使用する事です。
此れはSubClass{}を使うのではなくExtensionで追加されるもので、
Double valueで直接に接近ができます。

print(3.0.squared)
print(6.0.cubed)

ExtensionはsubClass{}を使用せずに、 Class{}の機能を拡張できる便利な方法を提供します。だがsubClass{}もExtensionに比べで{Override{}}とか保存Property を生成する事ができます。 

要約

相続は新しいClass{}が既存の Class{}で派生して新しい機能を追加する様な方法でObjectProgrammingObject再利用の概念を拡大します。
既存の Class{}が使用者が欲しいな一部の機能を提供している場合、
相続は、既存のClass{}を新しいSubClass{}の基本形みたいに使えるようにしてくれます。
新しいsubClass{}SUPERClass{}の全ての機能をinheritされるし、足りない機能を追加するため拡張されます。
SwiftのExtensionはSubClass{}を生成しなくても既存の Class{}で機能を追加する事ができる有用な方法を提供します。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?