Article View: aus.computers.java
Article #807Re: Constructors
From: Paul Murray
Date: Tue, 25 Apr 2000 00:00
Date: Tue, 25 Apr 2000 00:00
68 lines
3054 bytes
3054 bytes
These two constructors
public as2q1class(String thestudent_name, int theassign_one, int
theassign_two, int thetest_one, int thetest_two)
public void set(String newstudent_name, int newassign_one, int
newassign_two, int newtest_one, int newtest_two)
Are identical. Why do you have two? The names that you call the variables
don't matter, only the types do.
How does constructing a student from a "student name" differ from
constructing one from a "new student name"?
---------------------------
If there is a genuine difference, then you need a constructor like this:
public as2q1class(String thestudent_name, int theassign_one, int
theassign_two, int thetest_one, int thetest_two, boolean isNewStudent)
{
if(isnewStudent) {
Do new student construction
}
else {
Do old student construction
}
}
---------------------------
If a new student is identical to an old student but with some extras, you
can do this
public as2q1class(String thestudent_name, int theassign_one, int
theassign_two, int thetest_one, int thetest_two) {
do old student constriuction
}
public as2q1class(String newstudent_name, int newassign_one, int
newassign_two, int newtest_one, int newtest_two, boolean isNewStudent) {
// call oldstudent constructor
this(newstudent_name, newassign_one, newassign_two, newtest_one,
newtest_two);
if(isNewStudent) {
Do extra stuff for new students
}
}
---------------------------
These are also Identical.
public void set(int newassign_one) { assign_one = newaswign_one; }
public void set(int newassign_two) { assign_two = newassign_two; }
Lets put it this way. Take this bit of code:
as2q1class z = new as2q1class("Dave");
z.set(3);
When I call set, how is the class supposed to know whether I am trying to
set assign_one or assign_two?
Repeat after me: "In overloading, the names of the *parameters* don't
count" - only the name of the method and the types of the parameters. The
names of the parameters are just things internal to the method - the caller
of a method does not pass names of parameters, it only passes their values.
BTW: the return type doesn't count either.
--------------------------------------------------------------
http://www.users.bigpond.com/pmurray
ICQ: 26066755
Technical Details
Message-ID:
<g58N4.8005$E4.16113@newsfeeds.bigpond.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3902becc$0$20235@echo-01.iinet.net.au>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d6db,ba8cc3bfd1429785 X-Google-Attributes: gid10d6db,public From: "Paul Murray" <pmurray@nospam.bigpond.com> Subject: Re: Constructors Date: 2000/04/25 Message-ID: <g58N4.8005$E4.16113@newsfeeds.bigpond.com> X-Deja-AN: 615241163 References: <3902becc$0$20235@echo-01.iinet.net.au> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600 X-Trace: newsfeeds.bigpond.com 956632524 139.134.125.79 (Tue, 25 Apr 2000 13:15:24 EST) Organization: Telstra BigPond Internet Services (http://www.bigpond.com) X-MSMail-Priority: Normal NNTP-Posting-Date: Tue, 25 Apr 2000 13:15:24 EST Newsgroups: aus.computers.java