繁体   English   中英

在此示例中需要帮助识别类/原始实例变量

[英]Need help identifying class/primitive instance variables in this example

我对应该是一个简单的问题有一个很大的问题:

从下面的代码中识别原始实例变量和 class 变量

class StudentDetails { Int rollnumber; String studentname; } StudentDetails firststudent = new StudentDetails(19236, "Thomas");

我最初的提交被发回给我,因为我描述了原始实例变量和 class 变量是什么,我认为我错误地识别了它们。 评论返回 state “显然 state 哪些变量是原始实例变量(有 1 个),哪些是 class 变量(有 1 个)。”

现在我必须阅读 10 篇不同的文章/帖子,解释什么是实例变量和 class 变量,我仍然对有问题的 class 变量感到困惑。

在我见过的所有示例中,class 变量在 class 中使用 static 关键字声明,但在方法、构造函数或块之外。 在这个问题中没有 static 关键字。 它是否以某种方式暗示? class 变量不需要吗?

我假设字符串 studentname 是 class 变量,因为 int rollnumber 是原始变量。 但是,我不确定并想了解答案。

我识别 class 变量的概念来自以下网站:

这听起来像是一个术语问题。

I would agree that a field declared with static in Java or C# is a "class field" (there is only one field, belonging to the class itself) and other fields are "instance fields" (there is a field for each instance of the班级)。

但听起来这个问题实际上是在询问“类类型”与“原始类型”。 Your code isn't valid Java or C#, but assuming based on context that it's supposed to be Java, int (lowercase) is a primitive type, and String is a class type.

所以rollnumber是一个实例字段,它的值是原始类型( int ) - 因此,它是一个“原始类型实例字段”。 并且studentname也是一个实例字段,但它的值是 class 类型( String ) - 因此,它是一个“类类型实例字段”。

我建议与给你分配任务的人讨论这个问题,这样你就可以理解他们在未来的任务中所期望的术语。 我想他们可能会使用,例如,术语“静态字段”而不是“类字段”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM