Re: Classless class-oriented programming

new topic     » goto parent     » topic index » view thread      » older message » newer message

Here is an example of usage:

include std/datetime.e 
include std/console.e 
include classes.e 
 
function get_years(atom secs) 
  atom days = floor(secs/86400) 
  return floor(days/365.25) 
end function 
 
function describe(integer entityID, sequence params) 
  atom secs = diff(getProperty(entityID, "birth_date"), now()) 
  printf(1, "\nHello! I speak %s.\n", {getProperty(entityID, "mother_tongue")}) 
  if length(params) then 
    printf(1, "I am %d years old. I was born in %s (%s).\n", {get_years(secs), getProperty(entityID, "birth_place"), params[1]}) 
  else 
    printf(1, "I am %d years old. I was born in %s.\n", {get_years(secs), getProperty(entityID, "birth_place")}) 
  end if 
  return 0 
end function 
 
function sound(integer entityID, sequence params) 
  sequence name = getProperty(entityID, "name") 
  if equal(getProperty(entityID, "energy"), "electricity") then 
    puts(1, name & ": Bzzz\n") 
  else 
    puts(1, name & ": Vroom\n") 
  end if 
  return 0 
end function 
 
procedure main(sequence cmd) 
  object void 
 
  integer TPerson = class("TPerson") 
 
  addProperty(TPerson, "birth_date", now()) 
  addProperty(TPerson, "birth_place", "") 
  addProperty(TPerson, "mother_tongue", "") 
  addMethod(TPerson, "describe", routine_id("describe")) 
 
  integer TVehicle = class("TVehicle") 
 
  addProperty(TVehicle, "name", "") 
  addProperty(TVehicle, "nb_wheels", 0) 
  addProperty(TVehicle, "energy", "") 
  addMethod(TVehicle, "sound", routine_id("sound")) 
 
  integer englishman = classes:new(TPerson) 
  setProperty(englishman, "birth_date", datetime:new(2010, 5, 14)) 
  setProperty(englishman, "birth_place", "Liverpool") 
  setProperty(englishman, "mother_tongue", "english") 
  void = callMethod(englishman, "describe", {"Merseyside"}) 
 
  integer scooter = classes:new(TVehicle) 
  setProperty(scooter, "name", "Scooter") 
  setProperty(scooter, "nb_wheels", 2) 
  setProperty(scooter, "energy", "gazoline") 
 
  integer car = classes:new(TVehicle) 
  setProperty(car, "name", "Car") 
  setProperty(car, "nb_wheels", 4) 
  setProperty(car, "energy", "electricity") 
 
  puts(1, "\n") 
  void = callMethod(scooter, "sound") 
  void = callMethod(car, "sound") 
end procedure  -- main 
 
main(command_line()) 
maybe_any_key() 

He is the resulting code:

 
Hello! I speak english. 
I am 11 years old. I was born in Liverpool (Merseyside). 
 
Scooter: Vroom 
Car: Bzzz 
Press Any Key to continue... 

Compared to the method used with the preprocessor, this one allows to define more than one class per file at the cost of a loss of speed.

Enjoy!

Jean-Marc

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu