Last active
June 18, 2016 06:36
-
-
Save gitbricho/41abd52f58d1f1a776c1 to your computer and use it in GitHub Desktop.
swift:class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
//## クラスとストラクチャ ##### | |
//## 定義 | |
struct S住所 { | |
// MARK:プロパティ | |
var 郵便番号 = "" | |
var 都道府県 = "" | |
var 市区町村 = "" | |
} | |
class C連絡先 { | |
// MARK:プロパティ | |
var 氏名 = "" | |
var よみ = "" | |
var 住所 = S住所() | |
// MARK:イニシャライザ | |
init (氏名:String, よみ:String) { | |
self.氏名 = 氏名 | |
self.よみ = よみ | |
} | |
init () {} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## インスタンス生成 | |
let l連絡先 = C連絡先() | |
let l会社住所 = S住所(郵便番号:"222-1111", 都道府県:"東京都", 市区町村:"千代田区") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## プロパティにアクセスする | |
print("会社住所の郵便番号は、\(l会社住所.郵便番号)") //会社住所の郵便番号は、222-1111\n | |
l連絡先.氏名 = "山田太郎" | |
print("連絡先の氏名は、\(l連絡先.氏名)") //連絡先の氏名は、山田太郎\n | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## 参照型(Reference Types)と値型(Value Types) | |
//構造体と列挙型は値型 | |
enum E連絡先種別 { | |
case 家族・親戚, 友人, 仕事 | |
} | |
var v連絡先1種別 = E連絡先種別.仕事 | |
let v連絡先2種別 = v連絡先1種別 | |
v連絡先1種別 = .友人 | |
print("連絡先1:\(v連絡先1種別), 連絡先1:\(v連絡先2種別)") //連絡先1:友人, 連絡先1:仕事\n | |
//値型:変数、定数への代入、関数への引数渡しの場合にコピーを作成 | |
//クラスは参照型 | |
let l連絡先1 = C連絡先() | |
l連絡先1.氏名 = "山田花子" | |
l連絡先1.よみ = "やまだはなこ" | |
let l連絡先2 = l連絡先1 | |
l連絡先2.よみ = "ヤマダハナコ" | |
print("参照元のl連絡先1 のよみは: \(l連絡先1.よみ)") //参照元のl連絡先1 のよみは: ヤマダハナコ\n | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## アイデンティティ・オペレータ | |
if l連絡先1 === l連絡先2 { | |
print("l連絡先1とl連絡先2は同じインスタンスを参照") | |
} | |
//結果:l連絡先1とl連絡先2は同じインスタンスを参照\n | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## 継承 | |
// ベースクラス | |
class C図形 { | |
func 描画する() { | |
// | |
} | |
} | |
// 継承クラス:メソッドをオーバーライド | |
class C円 : C図形 { | |
// 円に特化した描画メソッドにオーバーライド | |
override func 描画する() {} | |
} | |
class C四角形: C図形 { | |
// 四角形に特化した描画メソッドにオーバーライド | |
override func 描画する() {} | |
} | |
// ベースクラス | |
class C商品 { | |
// MARK:プロパティ | |
var 商品番号:Int = 0 | |
var 単価:Int = 0 | |
var 通常割引:Int = 0 | |
var 割引価格: Int { | |
return 単価 - 通常割引 | |
} | |
//その他の商品プロパティ | |
//商品関連のメソッド | |
} | |
// 継承クラス:計算プロパティをオーバーライド | |
class C特別割引商品: C商品 { | |
// MARK:プロパティ | |
var 割引期間: Bool = true | |
override var 割引価格: Int { | |
if 割引期間 { | |
return (単価 - 通常割引)/2 | |
} else { | |
return 単価 - 通常割引 | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// as によるキャスト | |
class Cベースクラス { | |
func fメソッド() { print("メソッド0") } | |
} | |
class C継承クラス1:Cベースクラス { | |
func fメソッド1() { print("メソッド1") } | |
} | |
class C継承クラス2:Cベースクラス { | |
func fメソッド2() { print("メソッド2") } | |
} | |
//型が継承クラス1のインスタンスは継承クラス1のメソッドを呼べる | |
let l継承1インスタンス = C継承クラス1() | |
l継承1インスタンス.fメソッド1() //メソッド1 | |
let lベースクラス配列: [Cベースクラス] = [C継承クラス1(), C継承クラス2()] | |
for 配列要素 in lベースクラス配列 { | |
//配列要素の実際の型は、それぞれC継承クラス1, C継承クラス2だが | |
//定数 lベースクラス配列は明示的に [Cベースクラス] と宣言している | |
配列要素.fメソッド() //Cベースクラスのメソッドはそのまま実行できる | |
if 配列要素 is C継承クラス1 { | |
//fメソッド1を呼ぶには as C継承クラス1 でキャストする | |
//要素は nil の可能性があるので as? とする | |
//オプショナル型をアンラップするために ! をつける | |
(配列要素 as? C継承クラス1)!.fメソッド1() | |
} else if 配列要素 is C継承クラス2 { | |
//fメソッド2を呼ぶには as C継承クラス2 でキャストする | |
//要素は nil の可能性があるので as? とする | |
(配列要素 as? C継承クラス2)!.fメソッド2() | |
} | |
} | |
/* 結果: | |
メソッド0 | |
メソッド1 | |
メソッド0 | |
メソッド2 | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## 初期設定 | |
// インスタンスの生成時にイニシャライザが実行される | |
class Cサンプル1 { | |
// Mark: プロパティ | |
let プロパティ1 : String | |
// Mark: イニシャライザ | |
init (プロパティ1: String) { | |
self.プロパティ1 = プロパティ1 | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## 終了設定 | |
// インスタンスが破棄される時にデイニシャライザが実行される | |
class Cサンプル2 { | |
// Mark: イニシャライザ | |
init () { | |
//リソースの割り当てなどの初期設定 | |
} | |
// Mark: デイニシャライザ | |
deinit { | |
//リソースの解放などの終了時設定 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## プロパティ ##### | |
//## 保持プロパティ | |
struct S長さ固定範囲 { | |
// MARK:プロパティ | |
var 開始: Int | |
let 長さ: Int | |
} | |
var v長さ5範囲 = S長さ固定範囲(開始: 0, 長さ: 5) | |
// プロパティの値を変更 | |
//v長さ5範囲.長さ=4 //長さは変更不可:コンパイルエラー | |
v長さ5範囲.開始=2 //開始は変更可能 | |
let l長さ4範囲 = S長さ固定範囲(開始: 0, 長さ: 4) | |
//l長さ4範囲.開始=2 //定数の場合は変更不可:コンパイルエラー | |
//## 遅延保持型 | |
class Cイメージ { | |
// MARK:プロパティ | |
var ファイル名 = "" | |
} | |
class C遅延プロパティクラス { | |
// MARK:プロパティ | |
lazy var イメージ = Cイメージ() | |
} | |
var v遅延プロパティインスタンス = C遅延プロパティクラス() | |
// この時点ではCイメージのインスタンスは作成されていない | |
v遅延プロパティインスタンス.イメージ.ファイル名 = "img.png"; | |
// この時点でCイメージのインスタンスが作成されて、ファイル名を設定できる | |
print(v遅延プロパティインスタンス.イメージ.ファイル名) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## 計算プロパティ | |
struct Sデータ { | |
// MARK:プロパティ | |
var データ配列: [Int] = [] | |
var 最大値: Int { | |
get { | |
let l最大値 = データ配列.reduce(0) { | |
if $0 > $1 { | |
return $0 | |
} else { | |
return $1 | |
} | |
} | |
return l最大値 | |
} | |
} | |
} | |
var vデータ = Sデータ(データ配列: [1,5,7,2,8]) | |
print(vデータ.最大値) //8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## メソッド ##### | |
//## インスタンスメソッド | |
class Cサンプル01 { | |
// MARK:プロパティ | |
var 設定値 = 0 | |
// MARK:インスタンスメソッド | |
func Fプロパティに値を設定(設定値: Int, 乗数: Int) { | |
self.設定値 = 設定値 * 乗数 | |
} | |
} | |
// インスタンス生成 | |
let lサンプルインスタンス01 = Cサンプル01() | |
// インスタンスメソッドを実行 | |
//update : 16/06/15 | |
//lサンプルインスタンス01.Fプロパティに値を設定(5, 乗数: 2) | |
lサンプルインスタンス01.Fプロパティに値を設定(設定値: 5, 乗数: 2) | |
print(lサンプルインスタンス01.設定値) //結果:10 | |
// 設定値はローカルパラメータ名、乗数はローカル/外部パラメータ名 | |
// 外部パラメータ名を使いたくない場合: | |
class Cサンプル02 { | |
// MARK:プロパティ | |
var vプロパティ = 0 | |
// MARK:インスタンスメソッド | |
//update: 16/06/15 | |
//func Fプロパティに値を設定(設定値: Int, _ 乗数: Int) { | |
func Fプロパティに値を設定(_ 設定値: Int, _ 乗数: Int) { | |
vプロパティ = 設定値 * 乗数 | |
} | |
} | |
// インスタンス生成 | |
let lサンプルインスタンス02 = Cサンプル02() | |
// インスタンスメソッドを実行 | |
lサンプルインスタンス02.Fプロパティに値を設定(5, 2) | |
print(lサンプルインスタンス01.設定値) //結果:10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## 値型(ストラクチャと列挙型)のプロパティの修正 | |
// デフォルトでは値型のプロパティはインスタンスメソッドでは修正できない | |
struct Sサンプル { | |
// MARK:プロパティ | |
var v変更したい値 = 0 | |
mutating func F値を変更できる(設定値: Int) { | |
v変更したい値 = 設定値 | |
} | |
/* mutating をつけないとコンパイルエラーになる | |
func F値を変更できない(設定値: Int) { | |
v変更したい値 = 設定値 | |
} | |
*/ | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## タイプメソッド(クラスメソッド) | |
class Cサンプル03 { | |
class func Fタイプメソッド() { | |
// | |
} | |
} | |
Cサンプル03.Fタイプメソッド() | |
// struct や enum には class は使えない | |
struct Sサンプル01 { | |
// static を使う | |
static func Fタイプメソッド() { | |
// | |
} | |
} | |
Sサンプル01.Fタイプメソッド() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment