【发布时间】:2018-05-06 11:45:48
【问题描述】:
我创建了一个没有参数的类的对象,但在程序中的某个时刻我想将参数传递给它的构造函数。
我该怎么办?
我的类有 3 个级别或继承,所以在构造函数中我使用 'super()' 将值传递给基类。
public void main(String args[])
{
Obj obj1 = new Obj();
// Some more code
// After the code i want to pass value to constructor of Obj class
// So the Constructor of obj class will pass value to the upperclass(parent class)
/* What should I write here? */
}
提前致谢。
【问题讨论】:
-
我不清楚你打算用这个去哪里。一旦实例化对象,构造函数已被调用,因此尝试重新调用它是没有意义的。
-
你没有。构造函数用于构造新对象,而不是修改现有对象。
-
这个我也不懂。如果 ctor 需要稍后才可用的参数,请稍后构造对象。
-
感谢大家的回答,对于不清楚的问题,我很抱歉这是我第一次在网上问...谢谢顺便说一句。
标签: java oop constructor