this

Inheritance and the “this” keyword

北慕城南 提交于 2019-12-03 06:35:52
Suppose that we have next situation: Parent class A: class A{ public A(){} public doSomething(){ System.out.println(this.getClass()); } } with a child class B: class B extends A{ public B(){} public void doSomething(){ super.doSomething(); System.out.println(this.getClass()); } } and Main class: class Main{ public static void main(String[] args){ A ab=new B(); ab.doSomething(); } } When I execute this code result is B B Why does this , referenced in superclass A, returns B as a class when the reference is of type A? It doesn't matter what the reference is, it's the instantiated object's class

jQuery using 'this' in an if statement

旧街凉风 提交于 2019-12-03 05:57:29
I'm using an if statement in order to determine if an element has any children. If it does NOT have any children, I want to do something to that element only. Here's the premise of what I'm trying to do: if ($("#div a").children().length > 0){ $(this).hide(); } So if an <a> tag has no children, I want to do something to that specific element (or multiple elements that also have no children). The problem is that this hasn't been defined because it's an if statement. I could be completely missing something but I'm not quite sure how to accomplish this. Any advice would be appreciated Edit: Added

jQuery: this: “$(this).next().next()” works, but “$(this).next('.div')” does Not

牧云@^-^@ 提交于 2019-12-03 05:07:30
Okay, I am trying to get this set of information to hide individually. <img class="arrow" src="images/navigation/arrowright.png"> <H2>More Information</H2> <div class="box"> <h2>Bibendum Magna Lorem</h2> <p>Cras mattis consectetur purus sit amet fermentum.</p> </div> <img class="arrow" src="images/navigation/arrowright.png"> <H2>A Second Group of Information</H2> <div class="box"> <h2>Bibendum Magna Lorem</h2> <p>Cras mattis consectetur purus sit amet fermentum.</p> </div> It works when I type this: $(".arrow").click(function() { $(this).next().next().slideToggle(); }); but not when I do this:

jQuery Passing $(this) to a Function

青春壹個敷衍的年華 提交于 2019-12-03 04:30:43
问题 I have lines of code like this: $(this).parent().parent().children().each(function(){ // do something }); It works well. But I need to run these lines multiple times. So I have created a function and pass $(this) parameter to a function: myFunc( $(this) ); function myFunc(thisObj) { thisObj.parent().parent().children().each(function(){ // do something }); } But in this way, It didn't work. 回答1: you can check this link. http://jsfiddle.net/zEXrq/38/ $("#f").click(function() { myFunc($(this));

jQuery if “this” contains

岁酱吖の 提交于 2019-12-03 04:19:36
问题 I'm trying to change any elements containing a particular text string to a red color. In my example I can get the child elements to become blue, but there's something about the way I've written the 'Replace Me' line that is incorrect; the red color change doesn't happen. I note that the "contains" method is usually written as :contains but I couldn't get that to validate with $(this) . $('#main-content-panel .entry').each(function() { $(this).css('color', 'blue'); }); $('#main-content-panel

What is purpose of a “this” pointer in C++? [duplicate]

左心房为你撑大大i 提交于 2019-12-03 01:55:06
This question already has an answer here: When should I make explicit use of the `this` pointer? 12 answers What is purpose of this keyword. Doesn't the methods in a class have access to other peer members in the same class ? What is the need to call a this to call peer methods inside a class? Two main uses: To pass *this or this as a parameter to other, non-class methods. void do_something_to_a_foo(Foo *foo_instance); void Foo::DoSomething() { do_something_to_a_foo(this); } To allow you to remove ambiguities between member variables and function parameters. This is common in constructors.

Way to increment objects inside an array for 2 hit detection c+

♀尐吖头ヾ 提交于 2019-12-03 01:19:20
问题 Im creating a basic game using SDL/C++. I need a way to implement 2 hit detection. When just trying one hit it works fine. Here is what i have for the two hit detection: int maxHit = 2; int hitCount = 0; // Detect collisions for(auto p : projectiles) { for(auto a : aliens) { if(p->CollidesWith(a) && hitCount == maxHit) { p->HandleCollision(); a->HandleCollision(); } if(p->CollidesWith(a) && hitCount != maxHit) { ++hitCount; } } } For some reason it works on a select few of the enemies on the

Passing “this” to a function from within a constructor?

北慕城南 提交于 2019-12-03 01:10:00
Can I pass "this" to a function as a pointer, from within the class constructor, and use it to point at the object's members before the constructor returns? Is it safe to do this, so long as the accessed members are properly initialized before the function call? As an example: #include <iostream> class Stuff { public: static void print_number(void *param) { std::cout << reinterpret_cast<Stuff*>(param)->number; } int number; Stuff(int number_) : number(number_) { print_number(this); } }; void main() { Stuff stuff(12345); } I thought this wouldn't work, but it seems to. Is this standard behavior

jQuery remove selected option from this

故事扮演 提交于 2019-12-03 01:09:59
first post here, I come in peace :) I've searched but can't quite find what I'm after. I am trying to manipulate the selected option of a select box. Can someone please explain why this works: $('#some_select_box').click(function() { $('#some_select_box option:selected').remove(); }); but this doesn't: $('#some_select_box').click(function() { $('this option:selected').remove(); }); I just want to use "this" instead of spelling out the id of the select box - can someone point me in the right direction for the correct syntax? It's driving me mad because it looks like it should be really simple.

Is std::move(*this) a good pattern?

只愿长相守 提交于 2019-12-03 01:05:56
In order to make this code with C++11 reference qualifiers work as expected I have to introduce a std::move(*this) that doesn't sound right. #include<iostream> struct A{ void gun() const&{std::cout << "gun const&" << std::endl;} void gun() &&{std::cout << "gun&&" << std::endl;} void fun() const&{gun();} void fun() &&{std::move(*this).gun();} // <-- is this correct? or is there a better option }; int main(){ A a; a.fun(); // prints gun const& A().fun(); // prints gun&& } Something doesn't sound right about it. Is the std::move necessary? Is this a recommended use for it? For the moment if I don