|
| 1 | +Classes \emph{AND} Objects? |
| 2 | +What is the difference? |
| 3 | +\\ |
| 4 | + |
| 5 | +Well I am glad you asked. |
| 6 | +A class is the definition or blueprint of an object. |
| 7 | +A class tells a program what to expect when coming across an object of the given class. |
| 8 | +What methods and properties to expect and even how to create and destroy objects. |
| 9 | +\par |
| 10 | + |
| 11 | +\emph{An object refers to a single instance of a class} |
| 12 | +\\ |
| 13 | +Objects are refered to as being instances of a class. |
| 14 | +When you deine a class you are not creating a usable object that you can then call methods on |
| 15 | +or access properties of. |
| 16 | +You must then create an instance of that class (object) to be able to use it throughout your program. |
| 17 | + |
| 18 | +\subsection{Classes} |
| 19 | +Ok, so as I mentioned before we need to first define a class before we can start creating objects |
| 20 | +and using them in our program. |
| 21 | +How do we do this? |
| 22 | +\par |
| 23 | + |
| 24 | +\begin{lstlisting}[caption={Class Definition}] |
| 25 | +class Person |
| 26 | +\end{lstlisting} |
| 27 | + |
| 28 | +Ok...? |
| 29 | +That seems too easy? |
| 30 | +\\ |
| 31 | +Yes creating classes is usually fairly easy, just make sure to check how to create a class in your language of choice. |
| 32 | + |
| 33 | +\subsection{Objects} |
| 34 | +Ok, so we have our class definition from above, but how do we create an instance of this class so we can use it in our program? |
| 35 | +\par |
| 36 | + |
| 37 | +\begin{lstlisting}[caption={Object Declaration}] |
| 38 | +class Person |
| 39 | + |
| 40 | +p = new Person() |
| 41 | +\end{lstlisting} |
| 42 | + |
| 43 | +That is it. |
| 44 | +We can create an instance of our \pigVar{Person} class by using the \pigVar{new} keyword and calling \pigVar{Person()}. |
| 45 | +We can assign this instance to a variable, \pigVar{p}, and then use \pigVar{p} as an alias for our object throughout |
| 46 | +our program. |
| 47 | +\par |
| 48 | + |
| 49 | +Can we only have one object? |
| 50 | +No, you can have as many instances as your would like. |
| 51 | +\par |
| 52 | + |
| 53 | +\begin{lstlisting}[caption={Multiple Object Instances}] |
| 54 | +class Person |
| 55 | + |
| 56 | +p1 = new Person() |
| 57 | +p2 = new Person() |
| 58 | +p3 = new Person() |
| 59 | +\end{lstlisting} |
| 60 | + |
| 61 | +This then allows us to act on each of these instances as though they are separate. |
| 62 | +What does that mean? |
| 63 | +It means that if we were to modify a property of \pigVar{p1} then it would not have any effect on |
| 64 | +the same properties in \pigVar{p2} and \pigVar{p3}. |
| 65 | + |
| 66 | +\subsection{Properties} |
| 67 | +We are able to store variables inside of a class, these are called properties. |
| 68 | +To define a property we must define its name, access modifier and default value (if any). |
| 69 | +\par |
| 70 | + |
| 71 | +An access modifier can either be \emph{public}, \emph{private} or \emph{protected} (some languages do not support |
| 72 | +access modifiers). |
| 73 | +The \emph{public} modifier means that anyone who has access to the object can read and modify that property. |
| 74 | +The \emph{private} modifier means that no one outside of the object can read and modify the property, meaning that |
| 75 | +only the object itself has acess to the given property. |
| 76 | +The \emph{protected} modifier means that the given object and its children (we will get to this later in the chapter) |
| 77 | +will have access to read and modify the property. Lets look at an example. |
| 78 | +\par |
| 79 | + |
| 80 | +\begin{lstlisting}[caption={Class Properties}] |
| 81 | +class Person |
| 82 | + public name |
| 83 | + private age = 22 |
| 84 | + |
| 85 | +p = new Person() |
| 86 | +p.name = ``Brett Langdon'' |
| 87 | + |
| 88 | +p.age = 23 //this will cause an error |
| 89 | + |
| 90 | +\end{lstlisting} |
| 91 | + |
| 92 | +In this example we are creating a class with two properties, one is public (\pigVar{name}) and the other is private (\pigVar{age}). |
| 93 | +We then create a new instance of our class assigning it to the variable \pigVar{p}. |
| 94 | +Then we set the public property \pigVar{name} to \pigVal{``Brett Langdon''}. |
| 95 | +In line 8 there is the comment ``this will cause an error'' this is because the property \pigVar{age} is private and cannot be accessed |
| 96 | +from outside of the class. |
| 97 | + |
| 98 | + |
| 99 | +\subsection{Methods} |
| 100 | +So what is a Method? |
| 101 | +A method, simply put, is a function that belongs to a class. |
| 102 | +We use methods for the same reasons that we use functions for, to provide code reuse within our applications. |
| 103 | +Ok, so we know how to use functions, but how do we use them from within a class? |
| 104 | +\par |
| 105 | + |
| 106 | +\begin{lstlisting}[caption={Class Methods}] |
| 107 | +class Person |
| 108 | + public name |
| 109 | + private age |
| 110 | + |
| 111 | + function printName() |
| 112 | + print this.name |
| 113 | + |
| 114 | +p = new Person() |
| 115 | +p.name = ``brett'' |
| 116 | +p.printName() |
| 117 | +\end{lstlisting} |
| 118 | + |
| 119 | +The output of this code would be \pigOut{brett}. |
| 120 | + |
| 121 | +\subsection{Special Methods} |
| 122 | + |
0 commit comments