implementing instance initializers
This commit is contained in:
14
samples/ch28_initializers.lox
Normal file
14
samples/ch28_initializers.lox
Normal file
@ -0,0 +1,14 @@
|
||||
class CoffeeMaker {
|
||||
init(coffee) {
|
||||
this.coffee = coffee;
|
||||
}
|
||||
brew() {
|
||||
print "Enjoy your cup of " + this.coffee;
|
||||
// No reusing the grounds!
|
||||
this.coffee = nil;
|
||||
}
|
||||
}
|
||||
|
||||
var maker = CoffeeMaker("coffee and chicory");
|
||||
print maker;
|
||||
maker.brew();
|
12
samples/ch28_methods_again.lox
Normal file
12
samples/ch28_methods_again.lox
Normal file
@ -0,0 +1,12 @@
|
||||
class Brunch {
|
||||
bacon() {
|
||||
print "calling bacon()";
|
||||
}
|
||||
eggs() {
|
||||
print "calling eggs()";
|
||||
}
|
||||
}
|
||||
|
||||
var brunch = Brunch();
|
||||
print brunch;
|
||||
brunch.bacon();
|
@ -1,8 +1,4 @@
|
||||
class Brunch {
|
||||
bacon() {}
|
||||
eggs() {}
|
||||
init(food, drink) {}
|
||||
}
|
||||
|
||||
var brunch = Brunch();
|
||||
print brunch;
|
||||
brunch.bacon();
|
||||
Brunch("eggs", "coffee");
|
||||
|
17
samples/ch8_initalizers.lox
Normal file
17
samples/ch8_initalizers.lox
Normal file
@ -0,0 +1,17 @@
|
||||
class Brunch {
|
||||
var food;
|
||||
var drink;
|
||||
|
||||
init(food, drink) {
|
||||
this.food = food;
|
||||
this.drink = drink;
|
||||
}
|
||||
|
||||
fun get_recipe() {
|
||||
return this.food + " " + this.drink;
|
||||
}
|
||||
}
|
||||
|
||||
var instance = Brunch("eggs", "coffee");
|
||||
print instance;
|
||||
print instance.get_recipe();
|
Reference in New Issue
Block a user