View mode: basic / threaded / horizontal-split · Log in · Help
May 16, 2012
Re: Problem using Interfce
Gah, I need to proof-read my code before posting. Here's the corrected
version:

	class Simpson {
		string name;
	}
	class Bart : Simpson {
		this() {
			name = "Bart Simpson";
		}
	}
	class Homer : Simpson {
		this() {
			name = "Homer Simpson";
		}
	}

	void main() {
		Simpson[] simpsons = [
			new Bart,
			new Homer
		];

		foreach (simpson; simpsons) {
			writeln(simpson.name);
		}
	}


--T

-- 
My program has no bugs! Only undocumented features...
May 16, 2012
Re: Problem using Interfce
On 5/16/12 9:24 AM, Stephen Jones wrote:
> Ary: I seem to remember playing around with a Simpsons extending program
> in Java that did this; you could throw all the different Simpsons into a
> single Array because they extended "Simpson", and you could walk through
> the array and each would call their own name. I kind of purposely left
> the language vague in case I was mistaken.

But in Java you'd also need to cast the Simpson to a specific class if 
it contains the fields you are interested it.
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home