03.07.2013 Views

Guide de reference du langage ActionScript 2.0 - PowWeb

Guide de reference du langage ActionScript 2.0 - PowWeb

Guide de reference du langage ActionScript 2.0 - PowWeb

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

L'exemple précé<strong>de</strong>nt est fonctionnel, mais les propriétés bookcount et bookname sont<br />

ajoutées à chaque occurrence <strong>de</strong> l'objet Book, ce qui implique <strong>de</strong>ux propriétés pour chaque<br />

occurrence <strong>de</strong> l'objet. Si la classe comporte <strong>de</strong> nombreuses propriétés, telles que bookcount et<br />

bookname,, ces propriétés risquent <strong>de</strong> consommer beaucoup <strong>de</strong> mémoire. Une alternative<br />

consiste à ajouter les propriété à Book.prototype pour que les propriétés bookcount et<br />

bookname n'existent qu'en un seul emplacement. L'effet obtenu est toutefois i<strong>de</strong>ntique à celui<br />

<strong>du</strong> co<strong>de</strong> dans l'exemple où bookcount et bookname étaient directement ajoutés à chaque<br />

occurrence. Si vous tentez d'accé<strong>de</strong>r à l'une <strong>de</strong> ces propriétés dans une occurrence <strong>de</strong> Book,<br />

l'absence <strong>de</strong> la propriété entraîne le chaînage <strong>de</strong> prototype jusqu'à ce que les versions définies<br />

dans Book.prototype soient détectées. L'exemple suivant indique comment ajouter les<br />

propriétés à Book.prototype:<br />

function Book() {}<br />

Book.prototype.setQuantity = function(numBooks:Number):Void {<br />

this.books = numBooks;<br />

};<br />

Book.prototype.getQuantity = function():Number {<br />

return this.books;<br />

};<br />

Book.prototype.getTitle = function():String {<br />

return "Catcher in the Rye";<br />

};<br />

Book.prototype.addProperty("bookcount", Book.prototype.getQuantity,<br />

Book.prototype.setQuantity);<br />

Book.prototype.addProperty("bookname", Book.prototype.getTitle, null);<br />

var myBook = new Book();<br />

myBook.bookcount = 5;<br />

trace("You or<strong>de</strong>red "+myBook.bookcount+" copies of "+myBook.bookname);<br />

L'exemple suivant indique comment utiliser les fonctions <strong>de</strong> lecture/définition implicites qui<br />

sont disponibles dans <strong>ActionScript</strong> <strong>2.0</strong>. Au lieu <strong>de</strong> définir la fonction Book et modifier<br />

Book.prototype, définissez la classe Book dans un fichier externe appelé Book.as. Le co<strong>de</strong><br />

suivant doit figurer dans un fichier externe distinct appelé Book.as qui ne contient que cette<br />

définition <strong>de</strong> classe et figure dans le chemin <strong>de</strong> classe <strong>de</strong> l'application Flash :<br />

class Book {<br />

var books:Number;<br />

function set bookcount(numBooks:Number):Void {<br />

this.books = numBooks;<br />

}<br />

function get bookcount():Number {<br />

return this.books;<br />

}<br />

function get bookname():String {<br />

return "Catcher in the Rye";<br />

}<br />

}<br />

Object 1029

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!