The "confusing" world of OOP

What is OOP?

OOP stands for object oriented programming**.** It the method of programming of which you break things down into objects for easier access, and cleaner code! Let’s get started by jumping into objects, Method Types, and scopes.

What is a scope?

A scope is basically what you have between two brackets " { } " This you probably already know, I’m simply giving rudimentary examples for those who don’t know so they can understand what I mean when I talk in coder jargen.

Why do I need different types of methods?

Basically if you want to access a method outside of a class you can’t use a private method, and some methods shouldn’t be access by other classes so we make those variables and methods private. Static will give access to your variable across a class.

I know it seems like things are messy and confusing right now, but it’s not that hard when you get into it, so let’s hop into some examples.

  • Okay so say you are making a game engine and you want to create a Player with different types of methods like Player.getName(), Player.getIndex, and so on and so forth, Object Orientation allows you to create a new object called a class. a class is a group of methods and variables. We can define a new class (using C# as an example) by create a new file named newClass.cs. Inside this file the FIRST thing we do, is define the class and its scope.

class Player{

}

simple as that. If you notice there is no method type in front of it. you CAN put a method type in front of a class, but there’s no need to. Why, because of constructors.

Constructors are another object inside the magical world of OOP which allows you to define arguments and give a method type to your class. We do this by adding the constructor inside of the class.


class Player{
   public Player(int arg1){
       //create variables
   }
}

so what did we just do, well we gave an access type to Player so you can access it publicly from another class. But what’s more is the advantage of a constructor. If you have two playrers on a server with 2 different ip addresses you’re going to want to make a way to store those addresses while defining an instance of the class. [an instance is basically the creation of an object. For example if I have Player player, the instance of the Player is player and I can have another instance name player2 by defining Player player2]

So let’s go into defining variables with a constructor


class Player{
  private static string ipaddr;
  public Player(string ip){
      this.ipaddr = ip;
   }
}

Now what we’ve done here is create an ip address string that is stored in the class privately. Why privately? Because we’re going to make a method later that allows you to access that stored variable from a public access.

Let’s do that now


class Player{
   private static string ipaddr;
   public Player(string ip){
      this.ipaddr = ip;
   }

  public string getIP(){
      return this.ipaddr;
  }
}

in here we added a string that can be accessed publicly and it returns the ipaddr variable. Now, we’re going to go to our main class and I’m going to show you how to define this new player

In this example, imagine you have a function that when a player connects it creates a new instance of Player then echoes the IP Address of that player to the console.


class main{

  //some code that defines the event onConnect here…

   public static void onConncet(Socket socket){
   Player player =  new Player(socket.getremotehost.toString());

   tellConsole(player);
   } 

   private static void tellConsole(Player player){
      console.WriteLine("The ip " + player.getIP() + " has connected!");
   }
}

so this is much more simple than it looks. Basically we defined a new instance of the player in the line Player player = new Player(socket.getRemoteHost.toString());

The arguments that we had in this instance of the player class are what we defined earlier in our constructor. in this case it was just a string.

We than called a void (a method that returns nothing) which wrote a line to the console saying that the Ip address of the player instance we defined earlier connected.

I know it looks hard, but it’s really not when you get the hang of it. I feel like I was flying everywhere trying to explain OOP’s functionallity. so there’s probably stuff you didn’t understand, just reply with a question and i’ll be sure to help you out.

~Crest, God of Toothpaste

That new code block looks sick 😉

Pats self on back

It’s offset on the right, JC lol.

Thanks toothpaste. 😄

What are you talking about Richy? lol

umm. What are either of you talking about?

you forgot about the factory pattern, polymorphism, abstract factory pattern, singleton, facade. But other than that good work.

you forgot about the factory pattern, polymorphism, abstract factory pattern, singleton, facade. But other than that good work.

Thanks? it was just a rudimentary run down. lol I was trying to help some people in the chat earlier and made a post to give them a hand

Log in to reply