作者:张雍昊 | 来源:互联网 | 2023-05-17 18:30
IhaveanexamquestionfromapastpaperthatImtryingtoanswer:我在过去的一篇论文中有一个考试题,我正在尝试回答:Disc
I have an exam question from a past paper that I'm trying to answer:
我在过去的一篇论文中有一个考试题,我正在尝试回答:
Discuss variables of type primitive, reference and static in the context of a programming language. Give suitable examples [8].
在编程语言的上下文中讨论基元,引用和静态类型的变量。举个合适的例子[8]。
The answer I have so far is:
我到目前为止的答案是:
A primitive type is an object which the language has given a predefined value. These types include int, bool and float. Reference type objects refer to these primitive types in a particular sequence when instantiated. Examples of these are strings and arrays. The static keyword, when assigned to a variable, means that there is only one instance of this variable and the value assigned applies to all references of the variable.
基本类型是语言已给出预定义值的对象。这些类型包括int,bool和float。引用类型对象在实例化时以特定顺序引用这些基元类型。这些的示例是字符串和数组。 static关键字在分配给变量时,意味着此变量只有一个实例,并且赋值的值适用于变量的所有引用。
I'm fairly new to programming so I don't know if this is exactly right, so if anyone could give me some tips on how to improve the mark I would get for this question I'd greatly appreciate it.
我对编程很新,所以我不知道这是否完全正确,所以如果有人能给我一些关于如何改进标记的提示我会得到这个问题我会非常感激。
3 个解决方案
The answer to that question, in my opinion -- has not a thing to do with OOP and everything to do with the compiler and microprocessor.
在我看来,这个问题的答案与OOP没有任何关系,与编译器和微处理器有关。
The simplest and most accurate definition of the term that subsumes all of the qualities of a primitive type -- as I understand it -- is:
包含原始类型的所有特性的术语的最简单和最准确的定义 - 据我所知 - 是:
A primitive type must fit into the register used for operations on it -- IOW, in an X86 system -- the Accumulator.
原始类型必须适合用于操作的寄存器 - IOW,在X86系统中 - 累加器。
So, primitive types are limited to the size of the Accumulator and can be operated upon by native processor instructions. (Basic math and Boolean/bit-shifting operations). Yes, it fits into heap memory and on the stack, but those are still essentially 8-bit entities and the registers are not.
因此,原始类型仅限于累加器的大小,并且可以由本机处理器指令操作。 (基本数学和布尔/位移操作)。是的,它适合堆内存和堆栈,但那些仍然是8位实体,寄存器不是。
OOP languages do not use primitive types for their managed memory processes, they use structures that mimic primitive types. (Even in .NET, when you use the keyword int -- it uses System.Int32 to wrap that.)
OOP语言不使用原始类型作为其托管内存进程,它们使用模仿原始类型的结构。 (即使在.NET中,当您使用关键字int时 - 它使用System.Int32来包装它。)