Board index FlightGear Development Nasal

Inheritance in Nasal  Topic is solved

Nasal is the scripting language of FlightGear.

Re: Inheritance in Nasal

Postby rominet » Sun Feb 11, 2018 11:14 pm

Hi,

It seems that if you want the parent constructor to be executed by child instances, you have to explicitly call it from the child constructors (as in Python when you redefine __init__() in a child class):
Code: Select all
var Parent = {
   new: func(id=0) {
      var obj = {parents: [Parent]};
      obj.parentInit(id);
      return obj;
   },
   parentInit: func(id=0) {
      me.id = id;
   },
   getInfo: func {
      print("no info\n");
   },
   getId: func {
      print("id: ", me.id, "\n");
   }
};

var Child = {
   new: func(id=0) {
      var obj = {parents: [Child, Parent]};
      obj.parentInit(id);
      return obj;
   },
   getInfo: func {
      print("I am a Child\n");
   }
};

var Dog = {
   new: func {
      var obj = {parents: [Dog, Parent]};
      obj.parentInit();
      return obj;
   }
};

var child1 = Child.new(1);
var child2 = Child.new(2);
var dog = Dog.new();

child1.getInfo();
child1.getId();
child2.getInfo();
child2.getId();
dog.getInfo();
dog.getId();
rominet
 
Posts: 605
Joined: Sat Nov 01, 2014 2:33 pm
Callsign: F-KATS
Version: Git next
OS: Debian GNU/Linux

Re: Inheritance in Nasal

Postby xcvb » Mon Feb 12, 2018 10:20 am

OK, thanks to all!
xcvb
 
Posts: 132
Joined: Sat Mar 14, 2015 3:08 pm
Version: Next
OS: Fedora Kinoite

Previous

Return to Nasal

Who is online

Users browsing this forum: No registered users and 4 guests