为什么不能this
在静态方法中使用关键字?我想知道为什么C#定义了这个约束.这种约束可以带来什么好处?
[更新]:实际上,这是我在采访中遇到的一个问题.我知道'静态'和'这个'的用法,基于你的所有回复,我想我知道为什么这两个不能一起使用.也就是说,静态方法用于更改状态或在类型级别执行某些操作,但是当您需要使用"this"表示您想要更改状态或在实例级别执行某些操作时.为了区分类型的状态变化和实例的状态变化,c#donot允许在静态方法中使用'this'.我对吗?
我已经看到ClassName.this
在SO和其他地方使用很多Android代码(而不是简单的this
关键字)来引用类的当前实例.我理解你可能决定this
在类名前加上以消除任何歧义,但根据我的经验,这样做通常是不必要的,因为实际上只有一件事this
可以引用 - 代码执行的类的当前实例是否有其他我忽略的建议将this
关键字与类名称对接是更好的做法,或者在某些情况下它实际上是必要的?
将this
上下文传递给匿名forEach函数的现代正确方法是什么?
function Chart() {
this.draw = function(data) {
data.forEach(function(value) {
//do something with values
console.log(this); //question: how to get Chart instead of global scope?
)};
});
};
Run Code Online (Sandbox Code Playgroud) 在学习Java一段时间之后,它第一次使用this
关键字让我如此困惑.
这是我如何感到困惑.我写了以下代码:
class BasicInheritanceTest3Base{
private int x = 0;
public int y;
public void a() {
x++;
this.x++;
System.out.println("BasicInheritanceTest3Base.a()");
b();
this.b();
System.out.println(x);
System.out.println(y);
}
public void b(){
System.out.println("BasicInheritanceTest3Base.b()");
}
}
public class BasicInheritanceTest3 extends BasicInheritanceTest3Base {
private int x = 3;
public int y = 2;
public void b() {
System.out.println("BasicInheritanceTest3.b()");
}
public static void main(String[] args){
BasicInheritanceTest3 bit2 = new BasicInheritanceTest3();
bit2.a();
}
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
BasicInheritanceTest3Base.a()
BasicInheritanceTest3.b()
BasicInheritanceTest3.b()
2
0
Run Code Online (Sandbox Code Playgroud)
现在第一个问题是:为什么x
并this.x
指向x
基类而不是Child类?如果 …
这是最小的例子:
class A
{
A* const& this_ref;
public:
A() : this_ref(this) {}
};
Run Code Online (Sandbox Code Playgroud)
GCC 5.3.0发出警告:
警告:临时绑定到'A :: this_ref'只会持续存在,直到构造函数退出[-Wextra] A():this_ref(this){}
那是this
暂时的吗?什么... MSVC 2015对此保持沉默,并且this_ref->member
在我的情况下在构造函数之外引用类成员给出了预期的行为(但可能仅仅是UB的情况,不确定).
编辑:
请注意,这个问题扩展了一个链接尽可能重复,因为它不是关于创建此类引用的方法的一般性问题,而是关于警告GCC(以及除MSVC之外的其他可能的编译器)在创建时产生的.
是否有可能重载的构造函数以某种方式调用类中的另一个构造函数,类似于下面的代码?
class A {
public:
A(std::string str) : m_str(str) {}
A(int i) { *this = std::move(A(std::to_string(i))); }
std::string m_str;
};
Run Code Online (Sandbox Code Playgroud)
上面的代码有效,但我担心在构造函数中调用它可能会导致未定义的行为.
如果确实如此,请解释原因并提出更好的选择?
我相信这有一个简单的答案,但是周五下午,我很累.:(
不确定如何解释它,所以我将继续并发布示例代码...
这是一个简单的对象:
var Bob =
{ Stuff : ''
, init : function()
{
this.Stuff = arguments[0]
}
, doSomething : function()
{
console.log( this.Stuff );
}
}
Run Code Online (Sandbox Code Playgroud)
在这里它被使用:
$j = jQuery.noConflict();
$j(document).ready( init );
function init()
{
Bob.init('hello');
Bob.doSomething();
$j('#MyButton').click( Bob.doSomething );
}
Run Code Online (Sandbox Code Playgroud)
一切都有效,除了最后一行.当jQuery调用doSomething方法时,它会覆盖'this'并阻止它工作.
试图使用只是Stuff
不起作用.
那么我如何以允许jQuery调用它的方式引用对象自己的属性,并且还允许对象使用调用jQuery对象?
即我希望能够做到这样的事情:
doSomething : function()
{
console.log( <CurrentObject>.Stuff + $j(<CallerElement>).attr('id') );
}
Run Code Online (Sandbox Code Playgroud)
(在哪里<CurrentObject>
和<CallerElement>
替换为适当的名称.)
class C{
//methods and properties
}
void C::some_method(C* b){
delete this;
this = b;
}
Run Code Online (Sandbox Code Playgroud)
这在编译时给出了以下错误:
error: lvalue required as left operand of assignment
Run Code Online (Sandbox Code Playgroud)
我的意图:假设有C类的对象a和b.C类的内容可能非常庞大,而且逐字段复制可能非常昂贵.我想以经济的方式a
将' '的所有内容替换为' b
'.
默认复制构造函数会执行预期的任务吗?
我找到了一个叫做'移动构造函数'的东西 http://akrzemi1.wordpress.com/2011/08/11/move-constructor/
也许,它可能会得到我想要的效果.
我想在TypeScript中进一步重写JavaScript"方法".我很想在课堂上这样做,像这样:
// export default class
export default class GroupItemMetadataProvider1
{
protected m_instance;
protected _grid;
protected _defaults;
protected m_options;
constructor(options)
{
this.m_instance = this;
this._defaults = {
groupCssClass: "slick-group",
groupTitleCssClass: "slick-group-title",
totalsCssClass: "slick-group-totals",
groupFocusable: true,
totalsFocusable: false,
toggleCssClass: "slick-group-toggle",
toggleExpandedCssClass: "expanded",
toggleCollapsedCssClass: "collapsed",
enableExpandCollapse: true,
groupFormatter: this.defaultGroupCellFormatter,
totalsFormatter: this.defaultTotalsCellFormatter
};
options = $.extend(true, {}, this._defaults, options);
this.m_options = options;
}
protected defaultGroupCellFormatter(row, cell, value, columnDef, item)
{
if (!this.m_options.enableExpandCollapse)
{
return item.title;
}
let indentation = item.level * 15 + "px"; …
Run Code Online (Sandbox Code Playgroud)