ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-使用对象标识符作为变量名

2019-11-27 17:00:11  阅读:139  来源: 互联网

标签:java c-4


就是想 …

玩C,我发现如果创建一个名为circle的类,然后声明一个与该类名称完全相同的变量,则编译器不会抱怨.例如:

class circle {
  // whatever it does in here
};

circle circle;   // is a valid statement, but
circle *circle = new circle();  // gives you a 'circle' is not a type complain

事实证明,这适用于字符串string =“ string”;也一样并用Java尝试过,也可以.我想它也可以在C#上工作,但是我还没有尝试过.

谁能告诉我这背后的原因以及这是否是故意的功能?

解决方法:

n3337 9.1 / 2

A class declaration introduces the class name into the scope where it is declared and hides any class, variable,
function, or other declaration of that name in an enclosing scope (3.3). If a class name is declared in a scope
where a variable, function, or enumerator of the same name is also declared, then when both declarations
are in scope, the class can be referred to only using an elaborated-type-specifier (3.4.4).

n3337 9.1 / 4

[ Note: The declaration of a class name takes effect immediately after the identifier is seen in the class
definition or elaborated-type-specifier. For example,
class A * A;
first specifies A to be the name of a class and then redefines it as the name of a pointer to an object of
that class. This means that the elaborated form class A must be used to refer to the class. Such artistry
with names can be confusing and is best avoided. —end note ]

所以.

class circle {
  // whatever it does in here
};

int main()
{
circle *circle;  // gives you a 'circle' is not a type complain
}

编译良好.

class circle {
  // whatever it does in here
};

int main()
{
circle *circle = new class circle();  // gives you a 'circle' is not a type complain
}

编译也很好.

标签:java,c-4
来源: https://codeday.me/bug/20191127/2075844.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有