在OOP中是OBJECT还是INSTANT与VARIABLE IN C编程一样? [英] is OBJECT or INSTANT in OOP is as same as VARIABLE IN C programming?

查看:71
本文介绍了在OOP中是OBJECT还是INSTANT与VARIABLE IN C编程一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好老板



是OJ中的OBJECT或INSTANT和VARIABLE IN C编程一样吗?

解决方案

不,不一样。从本质上讲,答案可以在我对这个问题的评论中找到。



我认为如果我只给你一些关于C范围的暗示就足够了。你还没有为OOP做好准备,在跳转到OOP之前需要了解一些更基本的概念。即使在C语言中,对象和变量也不尽相同。对象是您使用的东西,在运行时期间会在内存中发生。在编程中,有一个基本的东西,如指针。物理上,指针表示为某个机器地址(在一个或另一个地址空间;它取决于平台),因此,它本身和对象。地址可以指向一些内存位置。在这里可以有一个对象,指针指向的对象。



现在,指针可能在源代码中有可能没有名称。这将是变量名称。运行时期间不存在变量本身;有源代码的工件。在物理上,这也是某个对象的地址,堆栈,堆上或为静态对象保留的内存(一些开发人员知道数据段的概念,但这也取决于平台)。对象可能有也可能没有名称。考虑一下:

  int  a =  13 ;  //  整数对象,其名称为a(在运行时不存在) 
int b = 14 ;
int * c =& a; // 将指针c初始化为
// 现在指针c指向包含13的内存位置
c =& b; // 现在 - 包含14的不同位置



您可以将指针 c 传递给某些上下文,其中 a b 不存在,因此您可以使用某个名称(c)指向的指针,但整数对象本身没有名称。一种显而易见的方法是在堆上分配内存。所以,你可以拥有一个没有变量的对象。例如,链表可以在某种类型上具有 N 对象,但只有头对象具有指向它的指针。学习链表;没有它们,你几乎无法理解编程。



现在,OOP对象类似于 struct 类型的对象你可以在C中拥有它。但是,除此之外,你还可以拥有更多。 OOP的根是虚方法(因此是虚拟表),覆盖(动态调用调用),因此,后期绑定多态性。为了达到这一点,你可以理解所有这些,你真的需要理解对象和变量如何在一些低级语言中工作,例如C.



- SA

Hello bosses

is OBJECT or INSTANT in OOP is as same as VARIABLE IN C programming?

解决方案

No, not the same. In essence, the answer can be found in my comment to the question.

I think it would be enough if I give you some hint withing the scope of C only. You are not yet ready for OOP and need to understand some more fundamental notions before jumping to OOP. Even in C, object and variable is not the same. Object is something you work with and something which takes place in memory during runtime. In programming, there is such a fundamental thing as pointer. Physically, pointer is represented as some machine address (in one or another address space; it depends on the platform), and, therefore, is itself and object. And the address can point to some memory location. And here there can be one more object, the object pointed by the pointer.

Now, a pointer may or may not have the name in your source code. And this would be the variable name. Variables per se do not exist during runtime; there are the artifact of your source code. Physically, this is also an address of some object, on stack, on heap or in the memory reserved for static objects (some developers know the notion of "data segment", but this also depends on the platform). Object may or may not have the name. Consider this:

int a = 13; // integer object, its name is "a" (will not exist during runtime)
int b = 14;
int * c = &a; // initialize pointer c as address of a
// now pointer c points to location of memory containing 13
c = &b; // and now -- different location containing 14


You can pass the pointer c to some context where a and b do not exist, so you can have the pointer which is itself pointed by some name ("c"), but the integer object itself have no name. One obvious way to do that is to allocate memory on the heap. So, you can have an object without a variable. For example a linked list can have N objects on certain type, but only head object has a pointer pointing to it. Learn linked lists; without them, you hardly can understand programming.

Now, OOP object are similar to the objects of struct types which you could have in C. But, in addition to that, you can have a lot more. The root of OOP is virtual methods (and hence virtual tables), overriding (dynamic dispatch of calls) and hence, late binding and polymorphism. To get to the point you can understand all that, you really need to understand how objects and variables work in some low-level language like C.

—SA


这篇关于在OOP中是OBJECT还是INSTANT与VARIABLE IN C编程一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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