为什么我们使用这
随着Android的方法名称关键字同一类中调用从另一个方法的方法。在Java或C#中,我们可以调用其他方法的情况下直接在这
关键字,如下面的code。
公共最后弦乐getElementValue(节点ELEM)
{
子节点;
如果(ELEM!= NULL)
{
如果(elem.hasChildNodes())
{
对于(子= elem.getFirstChild();!孩子= NULL;孩子= child.getNextSibling())
{
如果(child.getNodeType()== Node.TEXT_NODE)
{
返回child.getNodeValue();
}
}
}
}
返回 ;
}
/ **
*获取节点值
* @参数元素节点
* @参数密钥字符串
* * /
公共字符串的getValue(元素项,字符串str)
{
节点列表N = item.getElementsByTagName(STR);
返回this.getElementValue(n.item(0));
}
解决方案
在关于本规定问题的假设是不正确。 这是一样的java的 - 你不必添加this关键字
但建议使用它时,你必须具有相同名称的多个功能,但其范围是不同的(例如静态与正常功能)。
另外,建议如果字段没有一个preFIX(如M或_),你应该使用本。您还可以设置Eclipse来自动添加这个关键字这样的情况。
Why do we use the this
keyword along with the method name in Android to call a method from another method within the same class. In Java or C# we can call other method directly without the this
keyword, as in the code below.
public final String getElementValue( Node elem )
{
Node child;
if( elem != null)
{
if (elem.hasChildNodes())
{
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() )
{
if( child.getNodeType() == Node.TEXT_NODE )
{
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* @param Element node
* @param key string
* */
public String getValue(Element item, String str)
{
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
解决方案
the assumption in the question about the "this" requirement is incorrect . it's the same as on java - you don't have to add the "this" keyword .
however , it is recommended to use it when you have multiple functions that have the same name , yet their scope is different (for example static vs normal functions) .
also , it is recommended that if the fields don't have a prefix (such as "m" or "_" ) , you should use "this" . you can also set eclipse to automatically add the "this" keyword for such cases .