Created
September 28, 2014 14:37
-
-
Save nasser/ede187f06e2b8b9fd044 to your computer and use it in GitHub Desktop.
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
// declare a class called Phone | |
class Phone { | |
/* | |
declare its data (called 'fields' in C#) like you declare varibales | |
1. the access: public, private, protected etc. use public if you want | |
other classes to have access to this field. if you leave this off | |
it defaults to private | |
2. the type: what kind of data will be stored in this variable | |
3. the name: what name you will refer to this data by | |
*/ | |
public float weight; | |
public string owner; | |
/* | |
the difference is that each of these variables exists for EACH INSTANCE of | |
the class. there is not just one 'weight' variable. *EACH* Phone object you | |
create using `new Phone()` will get its *own* weight and owner variables. | |
*/ | |
/* | |
declare its behavior or actions (called 'methods' in C#) like you declare | |
functions normally. The difference is that each function has access to all | |
the variables you declared on the top | |
1. return type: what kind of data the method is producing. if youre calculating | |
a number it would be float for example. if youre not producing any data, but | |
just doing some stuff, then use void | |
2. name: the name of the method. C# convention is to capitalize method names | |
3. arguments: in parentheses, a list of declarations for what kind of data can be | |
passed into the method. the declarations here look a lot like normal variable | |
declarations. they have a type then a name. if you have more than one argument | |
you can separate them with commas | |
*/ | |
void TurnOn() { | |
Debug.Log("Phone is turning on..."); | |
} | |
void CallPerson(string otherPerson) { | |
Debug.Log(owner + " is calling " + otherPerson + "..."); | |
} | |
void CallPhone(Phone otherPhone) { | |
Debug.Log(owner + "'s phone is calling " + otherPhone.owner + "'s phone..."); | |
} | |
} | |
// somewhere else in the program | |
/* | |
to use a class, you have to create instances of it. remember, a class is the *BLUEPRINT* | |
it does not do anything on its own, really, just like you can't live in a blueprint of | |
a house. you have to build the house first to use it, and in C# you have to 'instantiate' | |
an 'instance' of the class to use it (those are the C# terms for this action). | |
it looks like a variable declaration, because it is one. the difference is that the type | |
is the name of your class! instead of float, int, etc, you can use your OWN classes as | |
types, so you can basically make your own types when the ones C# or Unity gives you are not | |
enough. the syntax is | |
1. type: there, the name of your class | |
2. name: the name you will refer to this particular instance. this name is only valid in the | |
current scope, so if you create an instance inside of a method for example, then the name | |
you assigned it will not be visible outside the method | |
3. equals sign: you dont have to, but you an assign the value right away. thats what i am doing here | |
4. new instance syntax: new, followed by the name of the class, followed by parentheses with all the | |
arguments to pass into the constructor (we havent talked about constructors yet, but they can automate | |
the process of setting up an instance). if there are no arguments, then just () | |
*/ | |
Phone ramseysPhone = new Phone(); | |
/* | |
once the instance is created, I can access its fields using the . syntax. the dot means 'inside of', so | |
ramseysPhone.weight = 40 can be read as 'get me ramseysPhone, go inside and get me the weight field, then | |
set it to 40.' again, this ONLY affects the weight of ramseysPhone, not any other Phones instances | |
*/ | |
ramseysPhone.weight = 40; | |
ramseysPhone.owner = "Ramsey"; | |
Phone rainsPhone = new Phone(); | |
rainsPhone.weight = 60; | |
rainsPhone.owner = "Rain"; | |
/* | |
you call the methods using the dot syntax as well. 'go into ramseysPhone, go inside and get me the CallPerson | |
method, thenc all it with the argument "Kaho"'. the execution then jumps to your CallPerson method and stays | |
there until that method completes | |
*/ | |
ramseysPhone.CallPerson("Kaho"); // will print Ramsey is calling Kaho... | |
/* | |
this method and this call demonstrated passing in a Phone object as an argument, and accessing its owner field | |
(see the implementation of CallPhone above). | |
*/ | |
ramseysPhone.CallPhone(rainsPhone); // will print Ramsey's phone is calling Rain's phone... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment