예제 #1
0
        // A	constructor	allows	a	programmer	to	control	the	process	by	which	objects	are created.
        // Constructors	can	be	used	with	value	types	(structures)	and	reference types	(classes).
        // A	constructor	has	the	same	name	as	the	object	it	is	part	of	but does	not	have	a	return	type.
        // Constructors	can	perform	validation	of	their	parameters	to	ensure	that	any objects	that	are	created	contain	valid	information.
        // If	the	validation	fails,	the constructor	must	throw	an	exception	to	prevent	the	creation	of	an	invalid	object.
        public void CreateConstructors()
        {
            AlienClassConstr x = new AlienClassConstr(100, 100);

            Console.WriteLine("x	{0}", x);
            // Constructors	can	be	given	access	to	modifiers.	If	an	object	only	has	a	private	constructor	it	cannot	be instantiated
            // unless	the	object	contains	a	public	factory	method	that	can	be	called to	create	instances	of	the	class.
        }
예제 #2
0
 // Constructors	can	be	overloaded,	so	an	object	can	contain	multiple	versions	of a constructor with different   signatures
 public void OverloadedConstructors()
 {
     AlienClassConstr x = new AlienClassConstr(100, 100, 4);
 }