-
Class Constructors
Hi, I'm pacify and this is my first time posting.
Anyways, I have simple, class design type of question.
I'm reading up a little bit on C# programming. The book that I'm reading recommends creating constructors in the following way: "the root constructor should represent the constructor with the most parameters. This should then call the constructor with the next level of paramets setting any data not handled by that constructor itself"(Guide to C# and...... )
The book goes on to talk about a constructor syntax:
Code:
ClassConstructor(<paramaters>) : this(person){
....
}
......where ": this(person)" calls the next, most parameter encompassing constructor.
I do not understand the reasoning behind calling all the constructors in a class. It seems to me that some variables would be continously reinitialized as your progressed from the simplest, 0-parameter constructor, to the most parameter encompassing (most overloaded constructor).
Here is the example from the book:
Code:
using System;
public class Account{
private double balance = 0.0;
private String name= "";
Account(double amount, String person) : this(person){
balance = amount;
Console.WriteLine("(double amount, String person) constructor");
}
Account(String person) : this(){
name = person;
Console.WriteLine("(String person) constructor"));
}
Account(){
Console.WriteLine("Default Constructor");
}
public static void Main(){
Account acc = new Account(120.00, "J B");
}
}
-------output --------------
Default Constructor
(String person) constructor
(double amount, String person) constructor
Bottom line, what is the point of this?
-
One reason -
If, when I created an object, I wanted to do something additional, I could make the change in one loacation and know it will be implimented regardless of which constructor is called. If I had made each of the constructors separate, then I'd have to make the change in each one, thus opening up the chance that I'd forget or mess one up....
Code:
using System;
public class Account{
private double balance = 0.0;
private String name= "";
Account(double amount, String person) : this(person){
balance = amount;
Console.WriteLine("(double amount, String person) constructor");
}
Account(String person) : this(){
name = person;
Console.WriteLine("(String person) constructor"));
}
Account(){
Console.WriteLine("Default Constructor");
// do something here and it happens regardless of which constructor I call....
Console.WriteLine("Doing something additional.....");
}
public static void Main(){
Account acc = new Account(120.00, "J B");
}
}
-------------------------------------------------
Bradley L. Jones
Author, and more.
QuinStreet / Internet.com
(Developer.com, CodeGuru.com, DevX, jGuru, and more!)
-------------------------------------------------
Similar Threads
-
Replies: 5
Last Post: 01-15-2006, 07:10 PM
-
By none_none in forum Java
Replies: 17
Last Post: 04-28-2005, 03:00 PM
-
Replies: 5
Last Post: 10-17-2002, 01:58 PM
-
By max caber in forum .NET
Replies: 13
Last Post: 10-03-2001, 04:05 PM
-
By Chris reid in forum Java
Replies: 3
Last Post: 02-11-2001, 08:17 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks