Skip to content

Instantly share code, notes, and snippets.

@s-kiriki
Created December 5, 2014 01:36
Show Gist options
  • Save s-kiriki/b73c912d29738a3b91f2 to your computer and use it in GitHub Desktop.
Save s-kiriki/b73c912d29738a3b91f2 to your computer and use it in GitHub Desktop.
Swift勉強会#3 クラス操作
// Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
//クラスの勉強
//クラス宣言
class Player{
var name:String
var position:Point = Point(v:0,h:0)
//外部変数名(position)と内部変数名(p)
init(name:String, position p:Point){
self.position = p
self.name = name
}
func getName() -> String{
return name
}
func move(p:Point){
position = p
}
}
var kiriki = Player(name:"s-kiriki", position:Point(v:1,h:2))
kiriki.getName() // s-kiriki
//演習問題
//BMIを計算し、判定するクラスをつくりたい
//BMI=体重/身長^2
class BMICalclator{
var weight:Double
var height:Double
init(h:Double, w:Double){
self.height = h/100
self.weight = w
}
func calcBMI() -> Double{
return self.weight/(self.height*self.height)
}
func judge() -> String{
switch(self.calcBMI()){
case 0..<18.5:
return "痩せ"
case 18.5..<25:
return "標準"
default:
return "肥満"
}
}
}
var calculator = BMICalclator(h:178.0, w:80.0)
calculator.calcBMI()
calculator.judge()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment