Re: Constructors?
Byron Foster <[email protected]> Tue, 12 Oct 2004 00:12:40 -0700
| Newsgroups | gmane.comp.lang.nice.general |
|---|---|
| Message-ID | <[email protected]> |
Daniel Bonniot wrote:
>> One aspect I like about this approach is that constructors are
>> inherited. I've seen allot of sub classes that simply override a
>> few methods, but require all the constructors of the base class to
>> be re wrapped, in both Java and C++.
>
> You're right, that's an additional benefit.
That brings up a question though, how might you "privatize" base class
constructors for a sub-class if you did not want any base class
constructors called from the sub-class?
>> Something peculiar also. If I have the following:
>>
>> class A {int x} new A(int x) {...do something...; this(x:x);}
>>
>> your get the following error: Ambiguity for symbol this.
>> Possibilities are : foo.C <init>(int count) foo.C <init>(int count)
>>
>>
>> Which is directly related to my original issue. How do I override
>> an existing autoconstructor?
>
> What would you want to do in "do something" ? There is already a 'new
> A(int x)' constructor, and writing one with a different behaviour
> seems like a highly confusing thing to do (for the clients of the
> class). You can think of the error message as a (bad) way to tell you
> that you are creating an ambiguity about what 'new A(x: ...)' means.
interface PropertyProvider
{
?String get(String key);
Collection getKeys();
}
class PropertyCache
{
private Map<String, String> map = new HashMap();
private PropertyProvider provider;
?String get(String key) // Returns value associated with key.
{if key not in hash then get key from provider and place in hash}
}
Ok, so here is somewhat of a real life setup. PropertyProvider defines
an interface for looking up a value associated with a property key. The
implementation may be a file, database, remote server, whatever. The
PropertyCache provides an interface for the PropertyProvider so that
properties are only queried from the PropertyProvider once, and stored
locally. (In reality PropertyCache would probably implement
PropertyProvider)
Now, It may be the case that I want to "override" the existing
this(provider:) constructor to modify behavior. For example I want to
not use HashMap but some other type of map that is faster for a small
number of keys. Or I want to load up all key/value pairs from the
provider all at once and store them in the hash. The initializer is not
the appropriate place for this because I may not want all constructors
to have this behavior.
-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl