Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – Representing different types of articles


Now, imagine that the company you work for wants a blog with different types of articles— text-based articles and image-based articles. What they want is simple: a text-based article that is made of a String, and an image-based article that stores the URL of the image. All types of articles need to have a title.

At first, let's write the Article class. This one will define what all articles have.

class Article
{
   public var title : String;
}

Notice that we did not create a constructor for this class: an article has to be either text-based or image-based in this example, being able to construct an instance of the Article class would be nonsense.

We want text-based and image-based articles to be able to generate some HTML code, but each one will need to do it in its own way. To make it usable by a calling function, we will define an IHTMLGenerator interface, as follows:

interface IHTMLGenerator
{
   public function generateHTML() : String...