Created
December 17, 2019 20:02
-
-
Save shakked/0db52e760e77000f1951ceb3ce7311a9 to your computer and use it in GitHub Desktop.
this doesn't actually executre
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
// This declare how a door works in general | |
Object Door { | |
// isOpen is like state, it's a variable tied to the object, | |
// every time you make a new instance of the object, it has its | |
// own state | |
instance var isOpen: Bool | |
// this is called when you make a door, I set it to be open by default | |
initialize() { | |
this.isOpen = true | |
} | |
// This prints the state of the door | |
function printMessage() { | |
if (this.isOpen) { | |
print("I'm open") | |
} else { | |
print("I'm closed") | |
} | |
} | |
// this opens the door | |
function openDoor() { | |
this.isOpen = true | |
} | |
// this closes the door | |
function closeDoor() { | |
this.isOpen = false; | |
} | |
} | |
// You can make many instances of the same object, each has its own state | |
var blueDoor = Door() | |
var redDoor = Door() | |
blueDoor.printMessage() // prints "I'm open" | |
// I close the blue door, the state variable of isOpen is updated to be false | |
blueDoor.closeDoor(); | |
blueDoor.printMessage() // prints "I'm closed" | |
// Note, nothing has happened to red door | |
redDoor.printMessage() // prints "I'm open" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment