类的差异投注实例化和类的声明对象 [英] Difference bet instantinstantiation of class and declaring object of class

查看:134
本文介绍了类的差异投注实例化和类的声明对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

以下2条语句之间有什么区别,我有点困惑.
请帮忙..

1)像这样声明类变量

hi All,

what is difference between following 2 statement, i bit confused..
pls help..

1)declaring class variable like this

classA objA;
objA.Fun1();



2)和另一个,如果我通过这种方式使用..



2)and the other one, if i use it by this way..

classA objB = new classA();
objB.fun1();



预先谢谢..



thanx in advance..

推荐答案

第一个只是变量的声明.它没有被实例化.运行它时,会出现未设置对象引用"或此类错误.第二个示例显示了如何实例化.它创建一个classA对象,并将其分配给objB.

通过books/google了解更多信息.
1st one is just the declaration of variable. It is not instantiated. When you run it, you get ''Object reference is not set'' or such error. 2nd one shows how to instantiate. It creates an object of classA and assigns it to objB.

Learn more by books/google.


在第一种情况下,您尚未实例化该对象.因此,您只能访问类的静态方法.

在第二种情况下,您实际上是在为内存中的实例分配位置.您实际上是在创建该类的对象.这就是您要在OOPS中执行的操作.创建对象,然后为每个这些对象设置属性.

互联网上有大量有关此的信息.
类似于 [
In the first case you have not intantiated the object. As a result, you only have access to the classes static methods.

Ine the second case, you are actually allocating a location for an instance in memory. You are actually creating an object of that class. This is what you would do in OOPS. Create objects and then set properties for each of these objets.

There is tons of information about this on the internet.
Something like this[^] should be good reading for you.


当您声明类似
的对象时
when you declare a object like

classA objA;


然后使用空引用创建类的对象

当您实例化类似
的对象时


then object of a class is created with null reference

when you instantiate an object like

classA objA = new classA()


然后将类的所有变量和方法都引用到一个对象.


then all the variables and methods of class is referenced to a object.


这篇关于类的差异投注实例化和类的声明对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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