If you're familiar with structs in C programming, I actually understood how objects work after messing around with structs for awhile. The way I look at an object, is that its just a struct, with the added functionality of initializing its own instances with an initializer, and functions (methods) inside it that allow you to modify or retrieve its parameters.
To use this object you would probably initialize it first.
Bike ABC = NewBike(blablabla);
When you wanna mod its variables you just do it like you would do a struct.
Bike.SetWheels(600);
Sorry I know its vague, Maybe someone else might be able to come up with a better explanation.. But this is how I understand it. Its basically just a struct with added functionality.
That example actually helps a lot...Let's say if one was programming a game, what would an object really do for that game or instead, what would an object really do for an anti-virus program?
There are practically a limitless amount of uses in which an object helps in the creation of a game. However, basically it mostly boils down to defining an object with the parameters and methods needed to work with or modify that object.
For example, if you were creating a game whose map comprised of tiles, you may want to create a Tile class that includes information such as x and y coordinates, the type of texture that Tile should contain, whether or not that Tile is passable, and any other objects that may be on that particular Tile (i.e. chest, monster, player, building entrance, etc.). Then maybe your Tile class has a paint method, which can be called when the world map needs to be drawn to the screen.
For another example, maybe you want to create a bunch of Items. You would want a class Item for all of your particular items to implement. Your Item class would have info pertaining to what kind of item it is (quest item, consumable, equippable), and how much bonus it grants to what attribute. Then it contains an Activate method which, depending on what Item it is, will perform some action (i.e. a health potion would add x amounts of health to the player, a spell book would perform x spell, a helmet would equip itself to the player and add a bonus x to armor, etc.).
For a non-game object example, you would want some kind of AnimationManager object. While not strictly necessary, it is extremely helpful to create an object whose sole purpose is to animate your game's sprites. Every time the AnimationManager Object's step method is called, it would advance the animations of all animating sprites by one frame, freeing the paint method from that responsibility and increasing your overall FPS.
There are tons of other examples, but these are a few that will hopefully show you a small amount of the power that object-oriented programming truly contains.
Let's say you are working on a 2D sprite based game - so you have a
class: (note this is kinda pseudo language mixed with c++)
structVECTOR2{floatx;floaty;VECTOR2(float_x,float_y){this->x=_x;this->y=_y;}~VECTOR2();}classCObject{public:GameObject()~GameObject();SetSize(VECTOR2size);SetTexture(texturetex);...moresets......somegets...private:VECTOR2pos;//position on game planeVECTOR2size;//size of objecttexture_texture;//actual image/spriteVECTOR2mov_v;//movement vectorVECTOR2mov_a;//acceleration vector..somemoreprivates..//some debug stuff...insertdebugstuff...}
so basically an object would be a collection which provides "utility" & necessary functions into ONE "tool"
Let's say if one was programming a game, what would an object really do for that game or instead, what would an object really do for an anti-virus program?
The idea is to make objects (or classes) really powerful in order to make your life easier.
Ima take an example from a program I'm currently working on:
//Creates a unit at Point(0,0). "zombie.aset" is the unit's model, //"stand" is it's starting animation.Unitz=newUnit(Point(0,0),"res\\zombie.aset","stand");
That's a short line of script. Really easy for me to write, but internally the object takes care of a bunch of different things so I don't have to care about it anymore:
Point(0,0) - Can a unit spawn there? Is it outside of the map area? If yes, then move it somewhere valid.
"zombie.aset" - Is there a model with that name? If not, can I find one and load it? If not, then tell the user about that problem.
"stand" - Does the model have a name like this? If not, switch to a different animation and tell the user about that problem.
I could check all these things by hand, but it'd result in something like this:
Pointp=newPoint(0,0);if(!IsPointValid(p)){p=MovePointSomewhereElse(p);}Modelzomb=GetModelFromDatabase("zombie");if(!IsModelValid(zomb)){zomb=LookForModelInGameDirectory("zombie");if(!IsModelvalid(zomb)){Error("We got a problem with the model here!");}}...andsoon...
You can use objects to split up problems and tasks into smaller parts. This way you can take care of it much better.
to expand on that line of thought its kinda like when you write a function that does something your going to do a lot ... but you dont want have to type out the long hand version of it over and over and over
so you write a function you can call to handle it for you
objects take this a step further they generally hang on to a chunk of memory ... they are a variable of sorts containing many different verialbes..
I do alot of Visual basic programing
and you have a simple "object" like a text box...... now it is possible to defeine and create place and add all the functionality of a text box with out using an object.... but id rather slash my wrists and bleed to death
I can just drop the "text box" object onto a "form" and whala ... i have a completely functional text box...
Rollback Post to RevisionRollBack
Skype
KageNinpo = SN
My Libraries
DialogLeaderboard & TeamSort
My Projects
SPACEWAR Tribute
Infinite TD
I'm afraid I don't have the answers to those questions. I'm no expert with Java.. Been using it on and off the past two years. I actually found it to be very confusing to learn, maybe cos I'm too used to C. Even at this point I can say there's still a lot of things about it I don't understand. Either that or its the jargon that just messes me up. Oh and my example above probably has some syntax errors in it since it was pulled off the top of my head, so just keep a note of that yea ><
Java is a good 'bread and butter' language. Java is still maintained by oracle, which acquired Sun. There will be a new JDK release soon. Is Java in decline? No. Is The java user base growing as fast as other languages? No.
I took a course of java last year. If you are used to C programming, it's a completely different way of programming. It's indeed clasified into a different programming paradigm. When I started taking java lessons, it seemed very confusing to me, as I was used to C programming. But when I wrote my first medium size program in java I liked it.
On the other hand, I still find some things I don't like about java. The main goals of the object oriented programming is an overall great idea, and you automatically notice it when you start using it. But java has gone too far over this idea and they have ended making everything too abstract and not intuitive. For example, if you want to make a program in java which saves a file into a xml document, you have to do something similar to this:
importjava.io.*;importjava.util.*;importorg.w3c.dom.*;importjavax.xml.parsers.*;importjavax.xml.transform.*;importjavax.xml.transform.dom.DOMSource;importjavax.xml.transform.stream.StreamResult;publicclassXMLDocument{privatestaticDocumentdom;// Create xml document:publicstaticvoidinit()throwsException{DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();DocumentBuilderdb=dbf.newDocumentBuilder();Filef=newFile("MyFile.xml");if(f.exists()){dom=db.parse("MyFile.xml");}else{dom=db.newDocument();dom.appendChild(dom.createElement("Root"));}}// Save xml document:publicstaticvoidsave()throwsException{TransformertFormer=TransformerFactory.newInstance().newTransformer();tFormer.setOutputProperty(OutputKeys.METHOD,"xml");tFormer.transform(newDOMSource(dom),newStreamResult(newFile("MyFile.xml")));}}
I still don't understand why do I have to do so many things to just save my xml document. It would be much easier to call a single functions in order to save it.
Some people also tends to make everything object oriented:
So, what I am seeing and learning here, is I could basically create a couple of objects that could detect...lets say...a Modem's speed using multiple classes, etc?
Detecting hardware parameters is a little bit different you wouldnt exactly need an object to do that.... just a library that handles getting that kind of information from somewhere specific....
Rollback Post to RevisionRollBack
Skype
KageNinpo = SN
My Libraries
DialogLeaderboard & TeamSort
My Projects
SPACEWAR Tribute
Infinite TD
Figured out my own question, hopefully this explained what I was trying to say, Soulcarver.
// New Class;PublicclassModem{intspeed;publicvoiddisplaySpeed(){System.out.println("Speed:"+speed);}}
// New Class;publicclassCableModemextendsModem{Stringmethod="cable connection";publicvoidconnect(){System.out.println("Connecting to the internet ...");System.out.println("Using a "+method);}}
// New Class;publicclassDslModemextendsModem{Stringmethod="DSL phone connection";publicvoidconnect(){System.out.println("Connecting to the internet ...");System.out.println("Using a "+method);}}
// New Class;publicclassModemTester{publicstaticvoidmain(String[]args){CableModemsurfboard=newCableModem();DslModemgateway=newDslModem();surfboard.speed=500000;gateway.speed=400000;System.out.println("Trying the cable modem:");surfboard.displaySpeed();surfboard.connect();System.out.println("Trying the DSL modem:");gateway.displaySpeed();gateway.connect();}}
I have a couple more questions if anyone is willing. Object Oriented Programming(OOP) makes my head hurt. >.>
First, I am kind of confused on what class variables are.
I know they would look something like this:
staticintvirusCount=0;
My first question is, what does static mean? I've been reading in this book and so far it tells me to create something called static int virusCount but does not tell me what static actually means and why I need to put this here. My second question is what does a class Variable do for the program and what is its uses? That may sound a bit redundant, hopefully you are able to understand that second question. >..<
To add onto that, my book reads: "When you create an object, it has its own version of all variables that are part of the object's class. Each object created from the Virus class of objects has its own version of the newSeconds, maxFileSize and author variables. If you modified one of these variables in an object, it would not affect the same variable in another Virus object."
If what I understand this correctly, that means that any variables you make in an object, are only for that object and nothing else? Or can they be used for other objects too? Or am I taking this completely the wrong way?
My second one is probably an easier question to answer. At least from what I can tell and know of objects, but my knowledge is not sufficient enough to answer this myself, sadly. Let me just make a small program here to explain what I mean.
Now if I wanted to make new object where would I do it? Would I do something like this?
/** Basically what I am trying to do is create a program that will set all my files last modified to a number above 60. */publicvoidvirusInfection(){Virusdevice=newVirus();device.newSeconds=92;}
So in shorter terms, I am trying to create a new object and give it a value, but I don't really know the 'proper' way to create a object in terms of code structuring. I've also made this small program just to kind of familiarize the reader so they understand more of what I am trying to convey then just throwing out 3 lines of code that will probably be a bit harder to understand.
That's all the questions I have for now...I mean there is more, but I will ask them some other time, maybe, if I don't find it out myself eventually. :)
I didn't read the rest of your post, but static means that any instance of your class shares exactly the same variable. If you have two instances of the virus class and run code like this:
Virus virusA = new Virus();
Virus virusB = new Virus();
virusA.virusCount = 100;pretending it's public here so this code isn't actually correct given the class you posted :P
Now if you print virusB.virusCount, it will also be 100. The virusCount variable is shared.
To add onto that, my book reads: "When you create an object, it has its own version of all variables that are part of the object's class. Each object created from the Virus class of objects has its own version of the newSeconds, maxFileSize and author variables. If you modified one of these variables in an object, it would not affect the same variable in another Virus object."
If what I understand this correctly, that means that any variables you make in an object, are only for that object and nothing else? Or can they be used for other objects too? Or am I taking this completely the wrong way?
Means:
Any object which is created by Virus()-Constructor has the same amount of variables, with the same name; also know as Attributes(Variables) & Methods (Functions) - except for what Catalisk said; static is shared between objects of same class (so virusCount = totalViruses - so you might end up doing a virusCount++; on construct and virusCount--; on deconstruct call)
as far as i know Virus <name> = new <Constructo>(...); is the correct one for java
think it's same like in C++ (or atleast very similiar)
//---main.cpp---#include "virus.h"intmain(intargc,char*argv[]){VirusfirstVirus=Virus(86);}//---virus.h---#include <math>#include <string>classVirus{public://open for external useVirus(intnS)//constructor{this->newSeconds=nS;this->maxFileSize=0x1<<15;// or 14 not sure yet; creating 2^15this->author="Sam Snett";}protected://shared with inheritsunsignedintnewSeconds;unsignedintmaxFileSize;stringauthor;private://no share}
protected is available to "child-classes" of the "base-class" while private is only available to the true owner (java has it too as far as i know - so if you want to extend smth it's useful to use protected - or you generate them with fixed values ) http://en.wikipedia.org/wiki/Java_syntax#Accessing_members
You can do private as long as you don't create extensions of it which require to use this argument too, if that's the case i would make them protected :x due to lazyness in writing
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
@FuzzYD: Go
That example actually helps a lot...Let's say if one was programming a game, what would an object really do for that game or instead, what would an object really do for an anti-virus program?
@StatusQ3: Go
There are practically a limitless amount of uses in which an object helps in the creation of a game. However, basically it mostly boils down to defining an object with the parameters and methods needed to work with or modify that object.
For example, if you were creating a game whose map comprised of tiles, you may want to create a Tile class that includes information such as x and y coordinates, the type of texture that Tile should contain, whether or not that Tile is passable, and any other objects that may be on that particular Tile (i.e. chest, monster, player, building entrance, etc.). Then maybe your Tile class has a paint method, which can be called when the world map needs to be drawn to the screen.
For another example, maybe you want to create a bunch of Items. You would want a class Item for all of your particular items to implement. Your Item class would have info pertaining to what kind of item it is (quest item, consumable, equippable), and how much bonus it grants to what attribute. Then it contains an Activate method which, depending on what Item it is, will perform some action (i.e. a health potion would add x amounts of health to the player, a spell book would perform x spell, a helmet would equip itself to the player and add a bonus x to armor, etc.).
For a non-game object example, you would want some kind of AnimationManager object. While not strictly necessary, it is extremely helpful to create an object whose sole purpose is to animate your game's sprites. Every time the AnimationManager Object's step method is called, it would advance the animations of all animating sprites by one frame, freeing the paint method from that responsibility and increasing your overall FPS.
There are tons of other examples, but these are a few that will hopefully show you a small amount of the power that object-oriented programming truly contains.
@StatusQ3: Go
Let's say you are working on a 2D sprite based game - so you have a class: (note this is kinda pseudo language mixed with
c++
)so basically an object would be a collection which provides "utility" & necessary functions into ONE "tool"
The idea is to make objects (or classes) really powerful in order to make your life easier.
Ima take an example from a program I'm currently working on:
That's a short line of script. Really easy for me to write, but internally the object takes care of a bunch of different things so I don't have to care about it anymore:
Point(0,0) - Can a unit spawn there? Is it outside of the map area? If yes, then move it somewhere valid.
"zombie.aset" - Is there a model with that name? If not, can I find one and load it? If not, then tell the user about that problem.
"stand" - Does the model have a name like this? If not, switch to a different animation and tell the user about that problem.
I could check all these things by hand, but it'd result in something like this:
You can use objects to split up problems and tasks into smaller parts. This way you can take care of it much better.
@s3rius: Go
to expand on that line of thought its kinda like when you write a function that does something your going to do a lot ... but you dont want have to type out the long hand version of it over and over and over
so you write a function you can call to handle it for you
objects take this a step further they generally hang on to a chunk of memory ... they are a variable of sorts containing many different verialbes..
I do alot of Visual basic programing
and you have a simple "object" like a text box...... now it is possible to defeine and create place and add all the functionality of a text box with out using an object.... but id rather slash my wrists and bleed to death
I can just drop the "text box" object onto a "form" and whala ... i have a completely functional text box...
Thanks guys, very helpful!
@StatusQ3: Go
I'm afraid I don't have the answers to those questions. I'm no expert with Java.. Been using it on and off the past two years. I actually found it to be very confusing to learn, maybe cos I'm too used to C. Even at this point I can say there's still a lot of things about it I don't understand. Either that or its the jargon that just messes me up. Oh and my example above probably has some syntax errors in it since it was pulled off the top of my head, so just keep a note of that yea ><
Java is a good 'bread and butter' language. Java is still maintained by oracle, which acquired Sun. There will be a new JDK release soon. Is Java in decline? No. Is The java user base growing as fast as other languages? No.
I took a course of java last year. If you are used to C programming, it's a completely different way of programming. It's indeed clasified into a different programming paradigm. When I started taking java lessons, it seemed very confusing to me, as I was used to C programming. But when I wrote my first medium size program in java I liked it.
On the other hand, I still find some things I don't like about java. The main goals of the object oriented programming is an overall great idea, and you automatically notice it when you start using it. But java has gone too far over this idea and they have ended making everything too abstract and not intuitive. For example, if you want to make a program in java which saves a file into a xml document, you have to do something similar to this:
I still don't understand why do I have to do so many things to just save my xml document. It would be much easier to call a single functions in order to save it.
Some people also tends to make everything object oriented:
It would be much easier to just do Number.a += 5; , instead of Number.add(5); .
So I think object oriented programming is cool, but you have to know where to use it, and where to stop using it.
So, what I am seeing and learning here, is I could basically create a couple of objects that could detect...lets say...a Modem's speed using multiple classes, etc?
@StatusQ3: Go
Detecting hardware parameters is a little bit different you wouldnt exactly need an object to do that.... just a library that handles getting that kind of information from somewhere specific....
@SouLCarveRR: Go
Sorry, I wasn't very clear...I meant to say, I could basically make a fake modem speed with disconnect() and connect() methods with objects, right?
Figured out my own question, hopefully this explained what I was trying to say, Soulcarver.
I have a couple more questions if anyone is willing. Object Oriented Programming(OOP) makes my head hurt. >.>
First, I am kind of confused on what class variables are.
I know they would look something like this:
My first question is, what does static mean? I've been reading in this book and so far it tells me to create something called static int virusCount but does not tell me what static actually means and why I need to put this here. My second question is what does a class Variable do for the program and what is its uses? That may sound a bit redundant, hopefully you are able to understand that second question. >..<
To add onto that, my book reads: "When you create an object, it has its own version of all variables that are part of the object's class. Each object created from the Virus class of objects has its own version of the newSeconds, maxFileSize and author variables. If you modified one of these variables in an object, it would not affect the same variable in another Virus object."
If what I understand this correctly, that means that any variables you make in an object, are only for that object and nothing else? Or can they be used for other objects too? Or am I taking this completely the wrong way?
My second one is probably an easier question to answer. At least from what I can tell and know of objects, but my knowledge is not sufficient enough to answer this myself, sadly. Let me just make a small program here to explain what I mean.
Now if I wanted to make new object where would I do it? Would I do something like this?
So in shorter terms, I am trying to create a new object and give it a value, but I don't really know the 'proper' way to create a object in terms of code structuring. I've also made this small program just to kind of familiarize the reader so they understand more of what I am trying to convey then just throwing out 3 lines of code that will probably be a bit harder to understand.
That's all the questions I have for now...I mean there is more, but I will ask them some other time, maybe, if I don't find it out myself eventually. :)
I didn't read the rest of your post, but static means that any instance of your class shares exactly the same variable. If you have two instances of the virus class and run code like this:
Virus virusA = new Virus(); Virus virusB = new Virus(); virusA.virusCount = 100;pretending it's public here so this code isn't actually correct given the class you posted :P
Now if you print virusB.virusCount, it will also be 100. The virusCount variable is shared.
@StatusQ3: Go
Means:
Any object which is created by Virus()-Constructor has the same amount of variables, with the same name; also know as Attributes(Variables) & Methods (Functions) - except for what Catalisk said; static is shared between objects of same class
(so virusCount = totalViruses - so you might end up doing a virusCount++; on construct and virusCount--; on deconstruct call)
as far as i know Virus <name> = new <Constructo>(...); is the correct one for java
think it's same like in
C++
(or atleast very similiar)so doing private int newSeconds is wrong? I should be doing:
Yeah, that's really off my question, but I just noticed it in the code you wrote.
Edit: Never mind, I just noticed you wrote that in C ++
@StatusQ3: Go
protected is available to "child-classes" of the "base-class" while private is only available to the true owner (java has it too as far as i know - so if you want to extend smth it's useful to use protected - or you generate them with fixed values ) http://en.wikipedia.org/wiki/Java_syntax#Accessing_members
You can do private as long as you don't create extensions of it which require to use this argument too, if that's the case i would make them protected :x due to lazyness in writing