与构造函数相关 [英] Related to Constructor

查看:52
本文介绍了与构造函数相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

流动代码语法有什么区别



  class  A 
{
int a,b;
public A( int a, int b)
{
this .a = a;
.b = b;
}

}



当我们在类和构造函数中使用相同的变量名并与此关键字一起使用时,有什么不同没有使用这个关键字,是否有任何区别,如果是的话还是没有...





 < span class =code-keyword> class  A 
{
int a,b;
public A( int x, int y)
{
a = x;
b = y;
}

}

解决方案

绝对没有区别。假设引用并且需要明确用于解决名称冲突,就像在您的第一个代码示例中一样,这是绝对正确的:如果您有参数的名称冲突,一个成员,非限定名称当然是参数的名称,所以你通过添加this来正确地限定成员。



-SA

What is the difference between the flowing code syntax

class A
{
int a,b;
public A(int a,int b)
{
this.a=a;
this.b=b;
}

}


what the different when we use same variable name in class and constructor and use with this keyword and same for without using this keyword,Is there any difference or not if yes than waht..


class A
{
int a,b;
public A(int x,int y)
{
a=x;
b=y;
}

}

解决方案

There is absolutely no difference. The reference this is assumed and needed to be explicitly used to resolve name conflict, as in your first code sample, which is absolutely correct: if you have conflicting name of a parameter and a member, the unqualified name is of course the name of the parameter, so you correctly qualify the member by adding "this.".

—SA


这篇关于与构造函数相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆