如何使用参数化构造函数维护类属性中存储的值? [英] How Do I Maintain Value Stored In Properties Of Class Using Parameterized Constructor ?

查看:67
本文介绍了如何使用参数化构造函数维护类属性中存储的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii,



我来自我的主页传递对象,使用参数化构造函数进行设置。



现在在其他类中..我第一次得到了vale ..当同一个类中的任何对象被创建时,howerver被调用默认构造函数被调用并使用参数化的constrcuror丢失了值集。



请建议



public abc()

{



}



public abc(classexp obj)

{

connection = obj。连接;

年= obj.year;

}



方法1()

{

//这里我可以使用价值

}



方法2()

{

abc objabc = new abc();

abc.getdetails();

这里我失去了value ..由我的参数化构造函数设置

}

解决方案

在Method2中为什么要创建一个新的对象即

方法 2 ()
{
Abc objabc = new abc(); // 此处您正在创建新对象。
abc.getdetails();
这里我通过参数化构造函数
}丢失 .. set





参数化构造器的基本规则是no。类的对象被创建,许多没有。类中存在的变量副本被初始化。



这里你不能使用默认构造函数获取变量的值(使用参数化构造函数)。



进一步访问此链接 [ ^ ]



此链接 [ ^ ]


Hii ,

I am from my main page passing object using parameterized constructor to set .

Now in other class .. I got the vale for the first time .. howerver within the same class when any object of that gets created default constructor gets invoked and lost the value set using parameterized constrcuror ..

Please suggest

public abc()
{

}

public abc(classexp obj)
{
connection=obj.connection;
year=obj.year;
}

Method1()
{
// here i can use the value
}

method 2()
{
Abc objabc=new abc();
abc.getdetails();
here i am loosing the value.. set by my parameterized constructor
}

解决方案

in Method2 why are you creating a new object i.e.

method 2()
 {
 Abc objabc=new abc(); //here you are creating new object.
 abc.getdetails();
 here i am loosing the value.. set by my parameterized constructor 
} 

.

The basic rule for the parameterised constuctor is that the no. of objects of class gets created that many no. of copy of variables present inside the class gets initialised.

Here you cann't get the values of variables(using parameterised constructor) using default constructors.

for further visit this link[^]

this link[^]


这篇关于如何使用参数化构造函数维护类属性中存储的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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