java's a pretty easy language to pick up. here i'll describe the basics of OOP:
OOP is Object Oriented Programming. to start, i'd suggest to just learn about classes, objects, methods, instance variables and constructors along with how they interact. a class can be thought of a blueprint to make an object. a class contains methods (subroutines), instance variables (like A-Z+theta, except user-defined) and constructors (a special type of method used to create an object). here's a short example, labeling the parts of a class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Enemy{ private int xPos = 0; // this creates an integer instance variable "xPos" and sets it to 0 private int yPos = 0; // because of the variables are "private", they cannot be accessed outside the class
public Enemy(int x, int y){ //constructor. enemies have an x and y position as parameters xPos = x; yPos = y; }
public int getX(){ //this is a method. it returns a type int, and has no parameters passed. return xPos; } public void setX(int x){ //another method, with a void return type. xPos = x; } }
|
to make and manipulate a new Enemy object, you would do:
1 2 3 4 5 6
| Enemy myEnemy = new Enemy(5, 10); //creates a new Enemy object called myEnemy int x = myEnemy.getX(); //calls the method "getX()" System.out.println(x); //prints 5 myEnemy.setX(200); // calls method "setX()" System.out.println(myEnemy.getX()); // calls getX(), and prints the value (200)
|