Tag: 构造函数

在java中inheritance创build多less个对象?

假设我有三个类: class A { A() { // super(); System.out.println("class A"); } } class B extends A { B() { // super(); System.out.println("class B"); } } class C extends B { public static void main(String args[]) { C c = new C(); //Parent constructor will get called } } 当我创build一个类C的实例时,它调用超类的构造函数。 那么,是不是有更多的对象被创build? 如果只创build一个对象,那么super()如何像另一个类的构造函数? super()方法在内部创build一个对象吗? 我所知道的是,构造函数也是一种方法(我可能是错的)。 我的问题是: 在这种情况下创build了多less个Object? […]

在C#中,你需要调用基础构造函数吗?

在C#中,如果我有一个默认的构造函数的inheritance类,我必须显式调用基类的构造函数或将隐式调用? class BaseClass { public BaseClass() { // … some code } } class MyClass : BaseClass { public MyClass() // Do I need to put ": base()" here or is it implied? { // … some code } }

C#调用另一个构造函数

我需要调用另一个构造函数的构造函数,该怎么做? 基本上 class foo { public foo (int x, int y) { } public foo (string s) { // … do something // call another constructor this (x, y); // doesn't work foo (x, y); // neither } }

如何在C ++类的初始化列表中初始化member-struct?

我在c ++中有以下类定义: struct Foo { int x; char array[24]; short* y; }; class Bar { Bar(); int x; Foo foo; }; 并希望在Bar类的初始化程序中将“foo”结构(及其所有成员)初始化为零。 这可以做到这一点: Bar::Bar() : foo(), x(8) { } …? 或者foo(x)在初始化列表中做了什么? 还是该结构甚至从编译器自动初始化为零?

什么是在C#中的构造函数的返回types?

我在这个链接上提出了Java的这个问题 我在java.Now中得到了一些答案,我想知道它在C#中。 正如我们所知,我们不必将任何返回types添加到C#构造函数中。 class Sample{ ….. Sample(){ …….. } } 在Objective C中,如果我们创build一个构造函数,它会返回一个指向它的类的指针。 但我认为这不是强制性的。 AClass *anObject = [[AClass alloc] init];//init is the constructor with return type a pointer to AClass 同样,是构造函数转换为一个方法,返回对自己的类的引用? 喜欢这个: class Sample{ ….. Sample Sample(){ …….. return this; } } 编译器是否将一个返回types的引用添加到构造函数的同一个类中? 发生了什么构造函数? 任何参考去研究这个?

ES6:调用没有新关键字的类构造函数

给定一个简单的类 class Foo { constructor(x) { if (!(this instanceof Foo)) return new Foo(x); this.x = x; } hello() { return `hello ${this.x}`; } } 是否有可能没有new关键字调用类的构造函数? 应该允许使用 (new Foo("world")).hello(); // "hello world" 要么 Foo("world").hello(); // "hello world" 但后者失败了 Cannot call a class as a function

使用接受string参数的构造函数实例化一个类对象?

我想从它的Class对象实例化一个对象,使用接受一个String参数的构造函数。 这是一些代码,接近我想要的: Object object = null; Class classDefinition = Class.forName("javax.swing.JLabel"); object = classDefinition.newInstance(); 但是,它没有文本实例化JLabel对象。 我想使用接受一个string作为初始文本的JLabel构造函数。 有没有办法从一个Class对象中select一个特定的构造函数?

如何在Ruby中使类的构造函数是私有的?

class A private def initialize puts "wtf?" end end A.new #still works and calls initialize 和 class A private def self.new super.new end end 完全不工作 那么正确的方法是什么? 我想通过工厂方法来创buildnew私有方法。

调用构造函数重新初始化对象

是否有可能使用它的构造函数重新初始化一个类的对象?

我可以为字段和构造函数参数使用相同的名称吗?

class C { T a; public: C(T a): a(a) {;} }; 这是合法吗?