[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Can somebody explain like I'm brand new to programming how

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 24
Thread images: 4

File: c master race.jpg (27KB, 500x600px) Image search: [Google]
c master race.jpg
27KB, 500x600px
Can somebody explain like I'm brand new to programming how the keyword 'this' is implemented?

Googling leads me to stackoverflow which is just a bunch of pretentious faggots pretending to be smarter than they really are and answering with buzzwords and avoiding the question

So I think I have a decent grasp here's an example
Assume its in Java

if I was making a game of chess programmed in java and I wanted to make a chess board class, I would :

public void class ChessBoard {

public void ChessBoard(something x, something y) {
this.x = 64;
this.y = 64;
}
}

Assume the x and y are the matrix values for the rows and columns for the chess board

So every time I call this constructor it would invariant have these values (x = 64 y = 64) correct?

What the fuck is the point of this then? How is what I wrote any different from simply writing x = 64 y = 64?

Please excuse me if this is a brainlet tier question I really fucking hate java
>>
File: sahar.jpg (100KB, 640x960px) Image search: [Google]
sahar.jpg
100KB, 640x960px
ansrew me lads i struggle greatly with this concept and my poo in loo professor thinks im retarded
>>
>>59174404
have you considered the possibility that you're retarded?
>>
Behind the scenes, a class is just a bunch of functions (its member functions). A classes' instances are just a bunch of structures that contain its member variables.
When you call a function on an instance, behind the scenes all you do is call that classes' function and pass it this instance's struct.
The this keyword just points to this instance's struct.
>>
"this" is a reference to the object that is currently running the code. Generally you do not need to use "this" to refer to object variables unless they are named the same thing as the parameters (which you should not be doing except for constructors and setters).

public void class GameBoard {
int width;
int height;

public GameBoard( int width, int height) {
// sets the internal width and height to the parameter width/height
this.width = width;
this.height = height;
}

public int getWidth() {
return width;
//return this.width; also would have worked; when there is no ambiguity they refer to the same thing
}

public void addPiece( Piece p, int x, int y) {
// this.x does not exist. x is a parameter, not part of GameBoard
}
}


You can also use this to store a reference to the object in case you need to pass it to something from inside the class.
>>
>>59174328
>java
>too stupid to understand SO answers
>too stupid to understand "this"
sounds like you are the poo in loo, anon
>>
>>59174532
>your average stack overflow answer:

>"Yeah the hexadecimal beer-battered sriracha tacos at my startup use unsigned integer division instead of this haha, it's archaic"
>>
>>59174548
This is literally the first result for "java this stack overflow"

The this keyword is a reference to the current object.Another way to think about it is that the this keyword is like a personal pronoun that you use to reference yourself. Other languages have different words for the same concept. VB uses Me and the Python convention (as Python does not use a keyword, simply an implicit parameter to each method) is to use self.

If you were to reference objects that are intrinsically yours you would say something like this:

My arm or my leg

Think of this as just a way for a type to say "my".
>>
Imagine in your class that you had a PlacePiece() which took 3 parameters: the x coordinate to place, the y coordinate to place, and some kind of "Piece" structure.

There's a problem: when you reference x or y in this method, are you referring to the parameters passed to the method, or are you referring to the private members x and y in your class definition? It's ambiguous, and I challenge you to figure out what will happen.

You can remove this ambiguity by using "this." which explicitly says to use the current object's private members rather than the parameters passed to the method.
>>
>>59174328
>>59174404
x refers to the local parameter named x
this.x refers to the class member variable named x
Look up what scope is
>>
>>59174328
I don't think you're going to make it anon. Perhaps construction is more your thing
>>
>>59174548
retarded, not true
>>
>>59174328
I'm a little confused, why do you have parameters in your constructor which aren't used?

In that constructor you'd need to specify the scope of x and y because you have something x and something y as well as this.x and this.y

this.y = object's y
something.y = parameter y.

if you just used

y = 64

you'd be assigning it to the parameter.

I like >>59174583 's answer.
>>
>>59174516
/thread
>>
Another use for this in Java: embracing bad design.
public class AnimFile {
public class Animation {
public class Frame {
public List<SubFrame> subFrames;
public AnimFile getFile() {return AnimFile.this;}
public Animation getAnim() {return Animation.this;}

public class SubFrame {
public AnimFile getFile() {return AnimFile.this;}
public Animation getAnim() {return Animation.this;}
public Frame getFrame() {return Frame.this;}
public int spriteID;
public int ox;
public int oy;
}
}
}
}
>>
How can you "program" a single thing in Java without knowing what "this" means?
>>
>>59174516
So this.width refers to that specific gameboard, but you can have another one with different parameters?
>>
>>59175081
Yes, for example.

public class GameBoard {
int width, height;

/** merges the two game boards together resulting in a game board that can fit both game boards */
public void mergeGameBoard( GameBoard other) {
this.width = this.width + other.width;
this.height = Math.max( this.height, other.height);
}
}

// would be called ala
{
GameBoard gb1 = new GameBoard(8,8);
GameBoard gb2 = new GameBoard(4,10);
gb1.mergeGameBoard(gb2);
// gb1 now has width 12 and height 10
}
>>
File: 1488248541329.jpg (10KB, 258x195px) Image search: [Google]
1488248541329.jpg
10KB, 258x195px
>Mfw certified java rogramer
>Mfw I have no idea what the fuck OP is talking about
>>
>>59175034
Is this a flawed inheritance technique?
>>
Why is your chess board 64x64?
>>
File: IMG_4002.jpg (93KB, 932x477px) Image search: [Google]
IMG_4002.jpg
93KB, 932x477px
I literally drawed it for you OP
>>
>Googling leads me to stackoverflow which is just a bunch of pretentious faggots pretending to be smarter than they really are and answering with buzzwords and avoiding the question

Going to these lengths just to avoid admitting you were too dumb to understand the answers is a little sad.
>>
The only time it's okay to be confused about this is when you start writing javascript and realize that sometimes when you call a bound method it's not being called by the object its bound to because fuck you.

But hey, you can just wrap it in a clojure.
Thread posts: 24
Thread images: 4


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.