abstract.bmx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. Rem
  2. A BlitzMax type that contains Abstract methods becomes abstract itself.
  3. Abstract types are used to define interfaces that extending types must
  4. implement before they can be used to create new instances.
  5. In the following code TShape is an abstract type in that you can not
  6. create a TShape but anything extending a TShape must implement a Draw()
  7. method.
  8. End Rem
  9. Type TShape
  10. Field xpos,ypos
  11. Method Draw() Abstract
  12. End Type
  13. Type TCircle extends TShape
  14. Field radius
  15. Function Create:TCircle(x,y,r)
  16. local c:TCircle=new TCircle
  17. c.xpos=x;c.ypos=y;c.radius=r
  18. return c
  19. End Function
  20. Method Draw()
  21. DrawOval xpos,ypos,radius,radius
  22. End Method
  23. End Type
  24. Type TRect extends TShape
  25. Field width,height
  26. Function Create:TRect(x,y,w,h)
  27. local r:TRect=new TRect
  28. r.xpos=x;r.ypos=y;r.width=w;r.height=h
  29. return r
  30. End Function
  31. Method Draw()
  32. DrawRect xpos,ypos,width,height
  33. End Method
  34. End Type
  35. local shapelist:TShape[4]
  36. local shape:TShape
  37. shapelist[0]=TCircle.Create(200,50,50)
  38. shapelist[1]=TRect.Create(300,50,40,40)
  39. shapelist[2]=TCircle.Create(400,50,50)
  40. shapelist[3]=TRect.Create(200,180,250,20)
  41. graphics 640,480
  42. while not keyhit(KEY_ESCAPE)
  43. cls
  44. for shape=eachin shapelist
  45. shape.draw
  46. next
  47. flip
  48. wend
  49. end