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”
What can be said about String?
**String is just a piece of text.
What are the differences between the var declaration and a property declaration?
- Properties are used within the Class created
- Variables are used within the Method created.
What expression would you use to call a property?
In order to call a property, it follows the same format as the method call.
object.propertyName
What is a SubClass and why is it helpful?
A subclass inherits the properties and methods of another class. It allows you not to duplicate code.
How would on create a subclass? Give one benefit of this subclass.
class Subclass Name : ParentClassName { }
This allows you to build new functionality within the subclass.
What is another name for the parent class?
Super Class.
How would you override a function of the parent class?
override func ParentClass Method( ) { }
How do you call both functions of the Parent and Sub Class?
super.nameofthefunction within the subclass.