Experiment( hashtable.klass.asp )

Output

Q: Is Bob alive? A: True
Q: Is BlackNinja alive? A: True
Q: What's BlackNinja name? A: Nin Tendo
Q: How much energy BlackNinja possess? A: 100

>> BlackNinja attacks Bob <<

Q: How much energy BlackNinja still possess? A: 95
Q: Is Bob alive? A: False

>> Time to BlackNinja eat something <<

Q: How much energy BlackNinja still possess now? A: 97

Sources

/hashtable.klass.asp

<!--#include virtual="/lib/unit-tests.asp"-->
<!--#include virtual="/lib/axe/classes/Parsers/json2.asp"-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:lang="en">
    <head>
        <title>An ASP dynamic inheritance approach</title>
        <!--// meta tags //-->
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <!--// link tags //-->
        <link rel="stylesheet" type="text/css" media="screen" href="/lib/unit-tests.css" />
    </head>
    <body>
        <div id="container">
            <div id="container-hd">
                <h1>Experiment( hashtable.klass.asp )</h1>
            </div>
            <div id="container-bd">
 
<h2>Output</h2>
<div class="code"><pre><%
 
class [_klass]
 
    private this
 
    public default function addMethod(byVal name)
        set this(name) = getRef( this("type") & "_" & name )
    end function
 
    private function createClass()
        set createClass = Server.CreateObject("Scripting.Dictionary")
    end function
 
    private function cloneClass(byRef Parent, byRef Child)
        dim key
 
        for each key in Parent
            if isObject(Parent(key)) then
                set Child(key) = Parent(key)
            else
                Child(key) = Parent(key)
            end if
        next
 
        set cloneClass = Child
    end function
 
    public function create(byVal name)
        set this = createClass()
        this("type") = name
        set create = this
    end function
 
    public function extends(byRef Parent, byVal childType)
        set this = cloneClass(Parent,createClass())
        set this("parent") = Parent
        this("type") = childType
        set extends = this
    end function
 
    public function [new](byRef Parent, byVal argv)
        set this = cloneClass(Parent,createClass())
        call this("initialize")(this, argv)
        set [new] = this
    end function
 
end class : dim Klass : set Klass = new [_klass]
 
 
 
' Human class
dim Human : set Human = Klass.create("Human")
 
    ' --[ Properties ]----------------------------------------------------------
    Human("name")    = ""
    Human("age")     = 0
    Human("isAlive") = true
    Human("energy")  = 1
 
    ' --[ Methods ]-------------------------------------------------------------
    function Human_initialize(this, argv)
        this("name") = argv(0)
        this("age")  = argv(1)
    end function : Klass("initialize")
 
    function Human_eat(this)
        this("energy") = this("energy") + 1
    end function : Klass("eat")
 
 
 
' Ninja class
dim Ninja : set Ninja = Klass.extends(Human, "Ninja")
 
    ' --[ Properties ]----------------------------------------------------------
    Ninja("energy")  = 100' overwrites super property
    Ninja("side")    = "GOOD"
 
    ' --[ Methods ]-------------------------------------------------------------
    function Ninja_initialize(this, argv)
        this("name")  = argv(0)
        this("age")   = argv(1)
        this("side")  = argv(2)
    end function : Klass("initialize")
 
    function Ninja_eat(this)
        this("energy") = this("energy") + 2
    end function : Klass("eat")
 
    function Ninja_attack(this, target)
        this("energy") = this("energy") - 5
        target("isAlive") = false
    end function : Klass("attack")
 
 
 
 
 
 
 
 
 
 
' Using stuff
dim Bob : set Bob = Klass.new( Human, array("Robert Weak", 25) )
dim BlackNinja : set BlackNinja = Klass.new( Ninja, array("Nin Tendo", "unknown", "EVIL") )
 
Response.write( "Q: Is Bob alive? A: " )
Response.write( Bob("isAlive") )' prints true
Response.write( vbNewline )
 
Response.write( "Q: Is BlackNinja alive? A: " )
Response.write( BlackNinja("isAlive") )' prints true
Response.write( vbNewline )
 
Response.write( "Q: What's BlackNinja name? A: " )
Response.write( BlackNinja("name") )' prints Nin Tendo
Response.write( vbNewline )
 
Response.write( "Q: How much energy BlackNinja possess? A: " )
Response.write( BlackNinja("energy") )' prints 100
Response.write( vbNewline )
 
Response.write( vbNewline )
call BlackNinja("attack")(BlackNinja, Bob)
Response.write( ">> BlackNinja attacks Bob <<" ) : Response.write( vbNewline )
Response.write( vbNewline )
 
Response.write( "Q: How much energy BlackNinja still possess? A: " & BlackNinja("energy") ) : Response.write( vbNewline )
Response.write( "Q: Is Bob alive? A: " & Bob("isAlive") ) : Response.write( vbNewline )' Bob never had a chance ...
 
Response.write( vbNewline )
call BlackNinja("eat")(BlackNinja)
Response.write( ">> Time to BlackNinja eat something <<" ) : Response.write( vbNewline )
Response.write( vbNewline )
 
Response.write( "Q: How much energy BlackNinja still possess now? A: " & BlackNinja("energy") ) : Response.write( vbNewline )
 
 
 
%></pre></div>
 
            </div>
            <div id="container-ft">
                <h2>Sources</h2>
                <h3><%= Request.ServerVariables("SCRIPT_NAME") %></h3>
                <div class="code"><%= geshify( loadTextFile( Server.mapPath( Request.ServerVariables("SCRIPT_NAME") ) ), "asp" ) %></div>
            </div>
        </div>
        <!--// javascript tags //-->
    </body>
</html>