Class, Objects, Methods & Properties Flashcards
What is a Class?
A blueprint that we create to describe a component of our app.
How do you create a Class? 2 Steps.
- ) Type “Class” and give it a name.
2. ) Open { } and type: init ( ) { }
What is init?
It’s an initializer. It’s a special type of method.
What is init used for?
All Classes must have an initializer. Any code you put in-between the curly brackets within init, gets executed when a new object of a certain class is created.
What is an object?
Objects are always created from the Class.
How do you create an object?
Class name followed by ( )
What is the easiest way to reference an object? Please give an example.
Turn the object into a variable.
Example: var a = Person( )
What does the print line statement provide? Give it’s form.
Print Line statements are made to call out a newly created object.
Form: println (“This is what will print”)
What is a method and what is it used for?
A named set of instructions that will be executed when called. They are created using the “func” keyword.
Expression:
func GiveMethodName ( ) { }
What is written in the curly brackets of the method expression?
Inside the curly braces you write the code to execute when the method is called.
The code inside the curly braces of the method expression would yield:
Println(“CalledMethod”) This line of code will only be executed if the func method is called.
Once a method is created, how would you call it? Give an example.
Object.MethodName( ) The var is the object you created from the Class.
Example: Since I created the var to easily reference the object, it would be called by:
a.Walk( )
Where are Properties created within a class? First, Second or Last?
Properties are always
How do you call a method from the initializer?
Go up to inti ( ) { } and type:
self.MethodName( ) - self means call a method you own yourself.
Give the expression for a Property. And give an example.
- var PropertyName: The data the property is going to store/track = an initial value
var Name:String = “Initial Name”