有没有人知道是否可以使用resharper强制使用this关键字?对于田地等......
Resharper能够显示可以删除的位置,但我们的编码标准告诉我们使用this关键字.
考虑:
class TestParent{
public int i = 100;
public void printName(){
System.err.println(this); //{TestChild@428} according to the Debugger.
System.err.println(this.i); //this.i is 100.
}
}
class TestChild extends TestParent{
public int i = 200;
}
public class ThisTest {
public static void main(String[] args) {
new TestChild().printName();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道有类似的问题已被提出,但我无法对Java中的'this'变量有一个明确的理解.
让我试着解释一下我是如何理解上面图像的结果的.
因为它是一个new TestChild()
调用printName()
方法this
的TestChild
对象,所以根据调试器,第6行中的变量被设置为一个对象 - {TestChild @ 428}.
但是,由于Java没有虚拟字段 - 我不完全确定这意味着什么,但我从概念上理解它与Java方法相反,它支持多态性 - 在编译时this.i
设置为100 TestParent
.
所以不管是什么this
,this.i
在一个TestParent
方法中总是会i …
很抱歉再次询问,已经有一些关于此关键字的问题.但他们所有人都说出了"这个"的目的.
什么时候使用这个关键字
C#何时使用此关键字
在C#中使用静态方法的形式参数中的"this"关键字在C#中
正确使用"this."关键字?
我的问题是何时不使用'this'关键字.
或者
在代码之类的情况下始终使用此关键字是否正确
class RssReader
{
private XmlTextReader _rssReader;
private XmlDocument _rssDoc;
private XmlNodeList _xn;
protected XmlNodeList Item { get { return _xn; } }
public int Count { get { return _count; } }
public bool FetchFeed(String url)
{
this._rssReader = new XmlTextReader(url);
this._rssDoc = new XmlDocument();
_rssDoc.Load(_rssReader);
_xn = _rssDoc.SelectNodes("/rss/channel/item");
_count = _xn.Count;
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
这里我没有使用'this'和"_xn"和"_count"也没有使用"_rssDoc.Load(_rssReader);" 好吗?我应该在类中使用"this"以及所有类变量吗?
编辑:在类中为自己的变量使用'this' 是没用的吗?
在 node.js 中,我与类/对象和“this”关键字斗争。例如:
function Set(arr,leq) {
this.leq = leq ? leq : function(x,y) {return x<=y;};
this.arr=arr.slice().sort(this.geq);
}
Set.prototype.geq=function(x,y) { return this.leq(y,x);}
var myset = new Set([1,5,3,2,7,9]);
TypeError: Object #<Object> has no method 'leq'
at [object Context]:1:47
at Array.sort (native)
at new Set ([object Context]:3:22)
at [object Context]:1:13
at Interface.<anonymous> (repl.js:171:22)
at Interface.emit (events.js:64:17)
at Interface._onLine (readline.js:153:10)
at Interface._line (readline.js:408:8)
at Interface._ttyWrite (readline.js:585:14)
at ReadStream.<anonymous> (readline.js:73:12)
Run Code Online (Sandbox Code Playgroud)
但是,如果我.sort
像这样删除片段:
function Set(arr,leq) {
this.leq = leq ? leq : function(x,y) {return x<=y;}; …
Run Code Online (Sandbox Code Playgroud) 我有以下java类.当我在构造函数中调用login方法时,我username
使用类名访问静态实例变量,password
使用关键字访问静态实例变量this
.我的问题是这两种方法有什么区别?应该在什么情况下使用哪一个?
public class MyClass {
private Main main;
private static String username = "mylogindetails";
private static String password = "mypassword";
public MyClass(){
this.main = new Main();
this.main.login(MyClass.username, this.password);
}
public static void main(String args[]){
MyClass myclass = new myclass();
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个表,其中每行包含一个表单.见下文
<table>
<tr><th>Fee Type</th><th>Amount</th><th>% Discount</th><th>Discounted Amount</th><th>Payable to</th><th>Remove</th></tr>
<tr>
<form>
<td></td>
<td class="cost"></td>
<td> <input /> </td>
<td class="discount"></td>
<td></td>
</form>
<tr>
</table>
Run Code Online (Sandbox Code Playgroud)
现在我的目的是拥有一个全局保存按钮,它将遍历每个表单并执行ajax帖子.见下文
function saveEditedFees(){
$("#savefeechanges").click(function(){
$("#feestable form").each(function(){
alert( $(this).attr('id') );
});
});
}
Run Code Online (Sandbox Code Playgroud)
我的问题是使用$(本)使用类的元素访问值,为"TD" "成本"和"打折" 我试着
alert( $(this).find("td").eq(2).html() );
alert($(this).siblings('.cost').text());
alert($("td .cost" ,this).text());
Run Code Online (Sandbox Code Playgroud)
基本上,我试图测试其中包含的值<td class="cost">
是否等于<td class="discount">
,这样我将有选择地做一个ajax帖子.
请帮忙.
我在我的C#项目中使用Resharper.
我创建了一些私有全局变量,通常我使用'this'关键字来为变量名添加前缀(例如this.FirstName).
Resharper显然希望我使用下划线而不是'this',但实际上足够大胆地声明'this'关键字是多余的.在MSDN网站上没有提到这一点; 我的印象是,无论你使用什么命名约定(使用_或this.或大写/小写),最终都是用户的选择(即使是MSDN网站也会混淆一下(尽管从不相同)类)).
任何人都可以澄清他们是否听过这样的话,或者你觉得多余的评论只是Resharper的意见,因为声称似乎有些荒谬.