Last active
February 23, 2017 13:09
-
-
Save sekky0905/1b1c8270ae5c2133cb25d7e79c8dea9c to your computer and use it in GitHub Desktop.
Goでコンストラクタ ref: http://qiita.com/Sekky0905/items/10c8ae9cee719d66fa84
This file contains 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
import "fmt" | |
type Language struct { | |
Name string | |
LangType string | |
} | |
// コンストラクタ | |
// 戻り値として返すのは、構造体のポイントであることに注意 | |
func NewLanguage(name string, langType string) *Language { | |
// コンストラクタの関数内で、構造体をnew | |
l := new(Language) | |
// 以下、構造体の各フィールドを引数で受け取った値に設定 | |
l.Name = name | |
l.LangType = langType | |
// 構造体のインスタンスを返す | |
return l | |
} | |
func main() { | |
l := NewLanguage("Go", "Static") | |
fmt.Println("名前" + l.Name) | |
fmt.Println("言語" + l.LangType) | |
} |
This file contains 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 Language { | |
constructor( | |
private _name: string, | |
private _langType: string) {} | |
get name(): string { | |
return this._name; | |
} | |
get langType(): string{ | |
return this._langType; | |
} | |
} | |
var lang = new Language('TypeScript', 'Static'); | |
console.log(`名前:${lang.name}`); | |
console.log(`名前:${lang.langType}`); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment