Re: [Vala] instantiation via base class constructor



Hi

I hope this is what you had in mind.  I just joined so missed the
earlier postings.  Sorry I reformated you code (force of habit)

public class Aaa : Object {
    protected int x;
    public Aaa(int x) {
        this.x = x; 
    }
    
    public virtual void test() { 
        stdout.printf("%d\n", this.x); 
    }
}
        
public class Bbb : Aaa {

    public Bbb( int x){
        base(x);
    }
    
    public override void test() { 
        stdout.printf("%d\n", -this.x);            
    }
}
        
public class Driver : Object {
        
        public static void main(string [] args) {
               Bbb b = new Bbb(5);
               Aaa a = new Aaa(5);
               a.test();
               b.test();              
        }
}

It compiles and works.  If I missed something vala centric please let me
know.

Regards, Srecko

On Sun, 2008-12-28 at 18:33 +0100, Ciprian Mustiata wrote:
Hi Andre, 

You may use base keyword to call the super (derived base class)
methods and constructor. I know that did not help, but you may reduce
duplicating your code.

Regards,
Ciprian

On Wed, Dec 24, 2008 at 2:29 PM, Andre Kuehne <andre kuehne gmx net>
wrote:
        Hello,
        
        i am new to Vala, played with it the last two days and have
        now a bunch of open questions.
        Here's one of them: Is it possible to instantiate a class via
        a constructor of one of its base classes? For example the
        following does not work:
        
        class Aaa {
               public int x;
               Aaa(int x) { this.x = x; }
               public virtual void test() { stdout.printf("%d\n",
        this.x); }
        }
        
        class Bbb : Aaa {
               public override void test() { stdout.printf("%d\n",
        -this.x); }
        }
        
        void main() {
               new Bbb(5).test();
        }
        
        $ valac super.vala
        super.vala:13.2-13.11: error: Too many arguments, method
        `Bbb.new' does not take 1 arguments
               new Bbb(5).test();
               ^^^^^^^^^^
        Compilation failed: 1 error(s), 0 warning(s)
        
        Thanks in advance!
        Andre
        _______________________________________________
        Vala-list mailing list
        Vala-list gnome org
        http://mail.gnome.org/mailman/listinfo/vala-list

_______________________________________________
Vala-list mailing list
Vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]