Polymorphic naming, anyone?

Post

Posted
Rating:
#1 (In Topic #1500)
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
Hi, just got a few moments while dinner cooks.
Some may know that in a mad moment recently I got involved in a university effort to create a "workbench" for a Linear Programming course. Weird, I know! At my age? and I didn't even think that LP wasn't still alive. Apparently it is and thriving (and a hell-lot more mature than when I last saw it in the 1980's). These days it is more of a course in econometrics than "computer science", the goal to produce people who can understand the (weird) mathematics involved and who can be effectively deployed into the growth industries of defense and services planning. What? Yeah that got me too.
Anyway the lovely people are quite cluey when it comes to computers doing this mathematical  witchcraft but now and then stump me with some concept that blows my more standard sort of brain.
Today I got asked how to …

(now into the real bit)

There is a logical class "machine" which is something that can produce some "commodity" or maybe more than one. There are "types" of machines that indicates a particular sort of machine. Putting that another way, there is an "Animal" class and various specialized classes that inherit from "Animal": "Cat", "Dog","Fish","Duck",etc,etc.

With their "machines", which has a defined set of "states" as a Property. Now for some sub-type of machine, "states" it is more logical to be give it a different name but it is still a reference to the parent "states".  IOW an industrial machine.state may be "operable" or "inoperable" but a cow type of machine can be "calf", "mature" or "dead" for instance. Fine, but the point is they want to be able to set the property name in the Cow class to "LifeStage" instead of "State".
(Aside: The values of all "states" in their world is numeric, e.g industrial machine.state can only be 1 or zero, cow.lifestage can only be 1,2 or 3. These are their "rules". OK? Fine.)

I believe this is possible in Gambas but I just cant see or explain how the different names can point to the parent class property.

Code (gambas)

  1. Class Cow
  2. Inherits Machine
  3. ...
  4. Property LifeStage as Integer Use Parent.Stage       ' sort of thing
  5. ...
  6.  

Woops! There goes the dinner bell (a.k.a. the oven timer)
Anyone understand what I'm talking about?

Online now: No Back to the top

Post

Posted
Rating:
#2
Banned
Just do it the old fashioned way without Use

Code (gambas)

  1.  
  2. ' Class Cow ' don't need this , just call the file Cow.class
  3.  
  4. Inherits Machine
  5.  
  6. '' Synonym for Machine.State
  7. Property LifeStage as Integer
  8.  
  9. Private Function LifeStage_Read() As Integer
  10.  
  11.   Return Super.State
  12.  
  13.  
  14. Private Sub LifeStage_Write(Value As Integer)
  15.  
  16.   Super.State = Value
  17.  
  18.  
  19.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Banned
You could also set your own Constant values to match the State values of that particular class.
Eg…

Code (gambas)

  1.  
  2. Inherits Machine
  3.  
  4. '' Synonym for Machine.State, Will be Calf (0), Mature (1) or Dead (2)
  5. Property LifeStage as Integer
  6.  
  7. Public Enum Calf, Mature, Dead
  8.  
  9. Private Function LifeStage_Read() As Integer
  10.  
  11.   Return Super.State
  12.  
  13.  
  14. Private Sub LifeStage_Write(Value As Integer)
  15.  
  16.   Super.State = Value
  17.  
  18.  
  19.  

Then you could use something like this…
If myCow.LifeStage = Cow.Mature Then

Endif
Online now: No Back to the top
1 guest and 0 members have just viewed this.