Tag: 构造函数

带参数的PHP构造函数

我需要这样做的function: $arr = array(); // this is array where im storing data $f = new MyRecord(); // I have __constructor in class Field() that sets some default values $f->{'fid'} = 1; $f->{'fvalue-string'} = $_POST['data']; $arr[] = $f; $f = new Field(); $f->{'fid'} = 2; $f->{'fvalue-int'} = $_POST['data2']; $arr[] = $f; 当我写这样的东西: $f = new Field(1, […]

C#在构造函数中closures表单

是否有可能在构造函数正在执行时closures窗体(或仅仅是在此阶段停止显示)? 我有以下代码: public partial class MyForm : Form { public MyForm() { if (MyFunc()) { this.Close(); } } } 在Main()中抛出一个ObjectDisposedException,在这里: static void Main() { … // Following line errors Application.Run(new MyForm()); } 我试过这样检查MyForm的结果: static void Main() { … MyForm frm = new MyForm(); if (frm != null) { // Following line errors Application.Run(frm); } } […]

部分class级的“文本秩序”是否正式定义?

具体来说,关于字段初始值设定项(在这种情况下,是静态的) – ECMA 334中的§17.11: 如果一个类包含任何具有初始值设定项的静态字段,那么在执行静态构造函数之前,这些初始值设定项将按照文本顺序执行。 现在,如果我们在单独的文件中有多个partial类,那么这个顺序是在哪里确定的? 我的直觉说:“没有正式定义,但可能涉及到csproj中包含的顺序,或csc注明的顺序”。 它是否正确? (是的,我意识到最好避免模糊 – 可能通过将所有的初始化移动到静态构造函数)。 例如,如果我有a.cs : using System; partial class Program { private static int Foo = Write("Foo"); static int Write(string name) { Console.WriteLine(name); return 0; } static void Main() { Console.WriteLine("[press any key]"); Console.ReadLine(); } } 和b.cs : partial class Program { private static int Bar = […]

问题与C + +构造函数

编辑:这个问题出来了,我认为我aced它! 去StackOverflow! :d 我现在有考试了,去年考试的其中一个问题就是发现下面的构造函数的实现问题,并写出一个更正的问题。 Rectangle::Rectangle(string col, int len, int br) { setColour(col); length =len; breadth=br; } 类定义如下: class Polygon { public: Polygon(string col="red"); void printDetails(); // prints colour only virtual double getArea()=0; void setColour(string col); private: string colour; }; class Rectangle : public Polygon { public: Rectangle(string, int, int); void printDetails(); // prints colour and […]

如何在JS(Javascript)中重载一个对象的构造函数?

我能做些什么吗? function User(form) { this._username = form.username.value; this._password = form.password.value; this._surname = form.surname.value; this._lastname = form.lastname.value; this._birthdate = form.b_day.value+"-"+form.b_month.value+"-"+form.b_year.value; this._avatar = form.avatar; this._messages = new Array(); this._messagesCount=0; } function User(userName,password,surname,lastName,birthdate) { this._username = userName; this._password = password; this._surname = surname; this._lastname = lastName; this._birthdate = birthdate; this._avatar = form.avatar; this._messages = new Array(); this._messagesCount=0; }

Ctor不允许返回types

有代码: struct B { int* a; B(int value):a(new int(value)) { } B():a(nullptr){} B(const B&); } B::B(const B& pattern) { } 我得到错误信息: '错误1错误C2533:'B :: {ctor}':构造函数不允许返回types' 任何想法为什么? PS我正在使用VS 2010RC

使用StructureMap时传递构造函数参数

我正在为我的DI使用StructureMap。 想象一下,我有一个类需要1个参数,如: public class ProductProvider : IProductProvider { public ProductProvider(string connectionString) { …. } } 当我得到一个IProductProvider的实例时,我需要在运行时指定“connectionString”。 我已经configuration了StructureMap,如下所示: ForRequestedType<IProductProvider>.TheDefault.Is.OfConcreteType<ProductProvider>(). WithCtorArgument("connectionString"); 不过,我不想在这里调用EqualTo(“something …”)方法,因为我需要一些工具来在运行时dynamic指定这个值。 我的问题是:如何通过使用ObjectFactory获得IProductProvider的实例? 目前,我有这样的东西: ObjectFactory.GetInstance<IProductProvider>(); 但是,如你所知,这是行不通的… 任何build议将不胜感激。

在构造函数中做什么(不)

我想问你关于C ++构造函数的最佳实践。 我不太清楚我应该在构造函数中做什么,什么不是。 我应该只使用它的属性初始化,调用父构造函数等? 或者,我甚至可以把更复杂的function,如读取和parsingconfiguration数据,设置外部库aso 还是应该为此编写特殊function? RESP。 init() / cleanup() ? 什么是PRO和CON在这里? 我想通过使用init()和cleanup() ,例如我可以摆脱共享指针。 我可以创build堆栈中的对象作为类属性,并在构造完成后对其进行初始化。 如果我在构造函数中处理它,我需要在运行时实例化它。 然后我需要一个指针。 我真的不知道如何决定。 也许你可以帮我吗?

我如何访问一个类的私有构造函数?

我是一名Java开发人员。 在一次采访中,我被问到一个关于私人构造函数的问题: 你可以访问一个类的私有构造函数并实例化它吗? 我回答“不”,但是错了。 你能解释我为什么错了,给一个实例化一个私有构造函数的对象的例子吗?

Class API中的getDeclaredConstructors和getConstructors有什么区别?

我注意到在Java Reflection API中有两种调用构造函数的方法: getDeclaredConstructors / getConstructors方法。 虽然Java文档略有不同( getDeclaredConstructors似乎暗示它返回的是所有构造函数,而不是公共函数),但不清楚API为什么明确支持这两种不同的方法。 更重要的是,我想知道:如果我们正在dynamic调用类,什么时候一种方法比另一种方法更可取? 例如,访问私有构造函数的目的是什么?