Object.new (type) doesn't work while create_default_instance () does.

This commit is contained in:
Kolan Sh 2014-06-26 13:02:57 +04:00
parent e8d0fbda5f
commit 8665b5297c
1 changed files with 74 additions and 0 deletions

View File

@ -0,0 +1,74 @@
public interface IDoc : Object {
public abstract string generate ();
public abstract Object copy ();
}
public abstract class AbstractDocList : Gee.ArrayList<IDoc>, IDoc {
protected abstract AbstractDocList create_default_instance ();
protected AbstractDocList list_copy () {
stdout.printf ("AbstractDocList::list_copy ()\n");
stdout.printf ("this.get_type ().name () = %s\n", this.get_type ().name ());
//var clone = Object.new (this.get_type ()) as AbstractDocList;
var clone = create_default_instance ();
foreach (IDoc dociface in this)
clone.add (dociface.copy () as IDoc);
return clone;
}
public Object copy () {
stdout.printf ("AbstractDocList::copy ()\n");
return list_copy ();
}
public string generate () {
stdout.printf ("AbstractDocList::generate ()\n");
var result = new StringBuilder ();
foreach (IDoc dociface in this)
result.append (dociface.generate ());
return result.str;
}
}
public class Text : Object, IDoc {
public string text = "";
public Text (string text) {
this.text = text;
}
public Object copy () {
stdout.printf ("Text::copy ()\n");
return new Text (text);
}
public string generate () {
stdout.printf ("Text::generate ()\n");
return text;
}
}
public class Glob : AbstractDocList {
protected override AbstractDocList create_default_instance () { return new Glob (); }
}
IDoc CreateObject () {
var glob = new Glob ();
for (var i = 0; i < 5; ++i) {
glob.add (new Text (i.to_string ()));
}
return glob;
}
void main () {
//stdout.printf ("Hello world!\n");
var idoc = CreateObject ();
stdout.printf ("idoc.generate () = %s\n", (idoc.copy () as IDoc).generate ());
}