this

How does the “this” keyword in Java inheritance work?

好久不见. 提交于 2019-11-28 21:11:10
In the below code snippet, the result is really confusing. public class TestInheritance { public static void main(String[] args) { new Son(); /* Father father = new Son(); System.out.println(father); //[1]I know the result is "I'm Son" here */ } } class Father { public String x = "Father"; @Override public String toString() { return "I'm Father"; } public Father() { System.out.println(this);//[2]It is called in Father constructor System.out.println(this.x); } } class Son extends Father { public String x = "Son"; @Override public String toString() { return "I'm Son"; } } The result is I'm Son

smart pointers + “this” considered harmful?

喜你入骨 提交于 2019-11-28 20:49:43
In a C++ project that uses smart pointers, such as boost::shared_ptr , what is a good design philosophy regarding use of " this "? Consider that: It's dangerous to store the raw pointer contained in any smart pointer for later use. You've given up control of object deletion and trust the smart pointer to do it at the right time. Non-static class members intrinsically use a this pointer. It's a raw pointer and that can't be changed. If I ever store this in another variable or pass it to another function which could potentially store it for later or bind it in a callback, I'm creating bugs that

React.js - Implementing sorting of components

折月煮酒 提交于 2019-11-28 20:40:03
问题 I'm trying to learn React concepts, especially re: state and dynamic UIs, by coding a small sports roster-like UI. I've included the code below and the whole app + visual is at http://codepen.io/emkk/pen/dGYXJO. This app basically creates player cards from an array of player objects I've defined earlier. I would like to implement sorting of the player cards upon button click. I've created a <Sort/> component that renders the said buttons. I'd attach event listeners but don't know how to have

Difference between Python self and Java this

旧时模样 提交于 2019-11-28 19:33:03
问题 I had done a bit of Python long back. I am however moving over to Java now. I wanted to know if there were any differences between the Python "self" method and Java "this". I know that "self" is not a keyword while "this" is. And that is pretty much what I could figure out. Am I missing anything else? 回答1: About self in Python (here is the source: Python self explanation): The reason you need to use self . is because Python does not use the @ syntax to refer to instance attributes. Python

Why must I use the “this” keyword for forward references?

筅森魡賤 提交于 2019-11-28 19:28:40
问题 When I use the this keyword for accessing a non-static variable in a class, Java doesn't give any error. But when I don't use it, Java gives an error. Why must I use this ? I know when should normally I use this , but this example is very different from normal usages. Example: class Foo { // int a = b; // gives error. why ? int a = this.b; // no error. why ? int b; int c = b; int var1 = this.var2; // very interesting int var2 = this.var1; // very interesting } 回答1: Variables are declared

return “this” in C++?

牧云@^-^@ 提交于 2019-11-28 18:53:25
In Java you can simply return this to get the current object. How do you do this in C++? Java: class MyClass { MyClass example() { return this; } } Well, first off, you can't return anything from a void -returning function. There are three ways to return something which provides access to the current object: by pointer, by reference, and by value. class myclass { public: // Return by pointer needs const and non-const versions myclass* ReturnPointerToCurrentObject() { return this; } const myclass* ReturnPointerToCurrentObject() const { return this; } // Return by reference needs const and non

What does “this()” method mean?

懵懂的女人 提交于 2019-11-28 17:46:35
I ran into this block of code, and there is this one line I don't quit understand the meaning or what it is doing. public Digraph(In in) { this(in.readInt()); int E = in.readInt(); for (int i = 0; i < E; i++) { int v = in.readInt(); int w = in.readInt(); addEdge(v, w); } } I understand what this.method() or this.variable are, but what is this() ? Avi This is constructor overloading: public class Diagraph { public Diagraph(int n) { // Constructor code } public Digraph(In in) { this(in.readInt()); // Calls the constructor above. int E = in.readInt(); for (int i = 0; i < E; i++) { int v = in

Difference in context this and getContext()

假如想象 提交于 2019-11-28 17:13:40
What is difference between this and getContext() , when I say this I mean this within an Activity . In general there are two type of classes. Ones that extend ContextWrapper class ( Activity , Service , Application ) and those that do not extend it (like View ). If class extends ContextWrapper then you can use this as Context . Such classes normally do not have getContext() method. Those classes that do not extend ContextWrapper but still save and use Context normally expose getContext() function. And you cannot use this as Context in such cases. And these two cases are mutually exclusive. At

Javascript scope addEventListener and this

旧时模样 提交于 2019-11-28 16:54:50
I am a C# developer experimenting with JavaScript and I'm trying to get my head around the scope :) I have the following code which contains an addEventListener in which I want to use a field from my object: (function(window) { function Keyboard() { this.keys = {}; } Keyboard.prototype.handle_keydown = function(args) { this.keys[args.keyCode] = true; } Keyboard.prototype.listen = function() { window.addEventListener('keydown', this.handle_keydown); } app.util.keyboard = new Keyboard(); })(window); I would like to use the keys array in my hander, but understand that I cannot access is by using

Why declare properties on the prototype for instance variables in JavaScript

落花浮王杯 提交于 2019-11-28 16:43:33
I'm trying to get my head around this black art called JavaScript - and, I must admit, pretty excited about it. I've been looking at code examples, mainly from "easeljs" since that is what I will be using mainly. And I'm a bit confused.. I (think I) understand the difference between using the prototype for functions or properties that are class variables and using this.someProp for 'instance' variables (Yes, I understand that there are no classes in JavaScript.) The code I have looked at, and am using as templates for my own code, declare prototype variables and then refers to them with this i