-
Notifications
You must be signed in to change notification settings - Fork 0
OOP Basics
OOP stands for Object-Oriented Programming.
Because if you know what OOP means, it will help you get into the right mindset when planning your code.
Find car keys
- Move to Bedroom
- Search Room
- if Keys are Found then Go_To_Work and End Find_car_keys
- Move to Living Room
- Search Room
- if Keys are found then Go_To_Work and End Find_car_keys
- Move to Kitchen
- Search Room
- if Keys are Found then Go_To_Work and End Find_car_keys
- Move to Bathroom
- Search Room
- if Keys are Found then Go_To_Work and End Find_car_keys
Pretty simple right? Just follow the steps. But what if we have more rooms? The number of steps could get very high. What if we want to change it to look for something other than keys?
Class FindCarKeys
variable room
variable keysFound
variable searchingMethod Search(parameter room)
- set searching to true
- if the current room is not equal to room then move to room
- search the room
- if keys are found then set keysFound to true
- if keys are not found then set searching to false
Class Main
variable roomsOn Startup
- set rooms to a list of the rooms in the house
- create an instance of the class FindCarKeys
Every 10 seconds
- if FindCarKeys.searching is false and FindCarKeys.keysFound is false. . .
- then run FindCarKeys.Search(a unsearched room in rooms)
Now this looks more complicated. Here we are using two "objects". The Class Main will start automatically (don't question it). Using this logic, it doesn't matter how many rooms we have. And if we wanted to change what we are looking for, we should only need to change a couple lines. And if we wanted to add another task, we would just create another Class and call it.
- Class
- Variable
- Method
- Parameter
I will explain this as if you are God. This seems reasonable and has little chance of offending anyone. As God you decide to create something new. It just starts to exist because you will it. You would also like all of your current things in the universe to be able to see it and interact with it.
public class Something()
If you decided that you wanted it to be secret and hidden from the five senses you would say:
private class Something()
I will get to that later, just know that they're there and they need to be. Pay attention and stop interrupting me.
Fields are variables that are stored in the Class. I am going to assume that you understand basic algebra. It's the same concept. When you say X = 10.1, you are actually saying:
This variable is a decimal with the value of 10.1. I shall name it "X".
Now let's see how that would look in a Class. Instead of a decimal, we are going to make it a "float". A float is just like a decimal. Just don't question it for now. In fact, from now on, when you are thinking of a decimal you are mistaken. You use float.
public class Something()
{
float X;
}
The squiggly brackets just mean that the code within them belongs to the Class.
Yeah, sure. Whatever.
public class Something(){
float X;
}
Well, for one; It doesn't matter. I like to place my curly braces on a new line because it's easier to understand what's going on when you're knee-deep in code. For two; I think what you meant to say was: "I thought it was supposed to look like this". So, how about we learn how to spell first?
Alright Mr. Smart-Ass. It looks like the student has become the teacher. I guess we are done here.