سؤال

I have two classes, lines and points. Both class implements 'highlightable'. I want to make a variable that can hold different type of objects that implements the 'highlightable' interface.

var currentObject:lines; //Won't work. It can only hold 'lines' object.
var currentObject:points; //won't work because it can only hold 'points' object.
var currentObject:Object; //Won't work because it can hold any objects. I want it to hold only those objects that implements the 'highlightable' interface.

Is it possible?

Thanks

هل كانت مفيدة؟

المحلول

All you need to do is make your variable the type of your interface.

var currentObject:highlightable = new classA();

or

var currentObject:highlightable = new classB();

Where both classA and classB implement the highlightable interface.

نصائح أخرى

Just implement the interface in your class so you force the pattern.

public interface IHighlightable

public class Lines implements IHighlightable
public class Points implements IHighlightable


// for the usage you type cast as the interface
var currentObject:IHighlightable = new Lines()
var currentObject:IHighlightable = new Points()

**Be sure to follow naming conventions

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top