Last active
December 16, 2015 00:09
-
-
Save 166MMX/5345135 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Model { | |
Model(seed) | |
{ | |
this.seed = seed | |
} | |
int seed | |
} | |
class A { | |
public Model seed | |
A () { | |
seed = new Model(Math.floor(Math.random() * 1000)) | |
println 'A() ' + seed | |
} | |
void a () { println 'A.instance.a()' } | |
void f () { println 'A.instance.f() ' + seed } | |
void xyz () { println 'A.instance.xyz() ' + seed } | |
} | |
class B extends A { | |
public Model seed | |
B () { | |
super() | |
seed = new Model(Math.floor(Math.random() * 1000)) | |
println 'B() ' + seed | |
} | |
void b () { println 'B.instance.b()' } | |
void f () { println 'B.instance.f() ' + seed } | |
void xyz () { | |
super.xyz() | |
println 'B.instance.xyz() ' + seed | |
} | |
} | |
class C extends B { | |
//public Model seed | |
C () { | |
super() | |
seed = new Model(Math.floor(Math.random() * 1000)) | |
println 'C() ' + seed | |
} | |
void c () { println 'C.instance.c()' } | |
void f () { println 'C.instance.f() ' + seed } | |
void g () { | |
f() | |
super.f() | |
} | |
void xyz () { | |
super.xyz() | |
println 'C.instance.xyz() ' + seed | |
} | |
} | |
def d = new B() | |
d.a() | |
println 'd instanceof A ' + (d instanceof A) | |
println 'd instanceof B ' + (d instanceof B) | |
println 'd instanceof C ' + (d instanceof C) | |
def e = new C() | |
e.g() | |
e.xyz() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var jPre = jQuery('<pre/>').appendTo('body'); | |
var preLog = function preLog(msg) { | |
jPre.append(msg + "\n"); | |
}; | |
// A extends Object | |
var A = function A() { | |
this.seed = Math.floor(Math.random() * 1000); | |
preLog('A() ' + this.seed); | |
}; | |
A.prototype.seed = null; | |
A.prototype.a = function a() { | |
preLog('A.prototype.a()'); | |
}; | |
A.prototype.f = function f() { | |
preLog('A.prototype.f() ' + this.seed); | |
}; | |
// B extends A | |
var B = function B() { | |
A.call(this); | |
this.seed = Math.floor(Math.random() * 1000); | |
preLog('B() ' + this.seed); | |
}; | |
var tmpPrototypeA = function tmpPrototypeA() {}; | |
tmpPrototypeA.prototype = A.prototype; | |
B.prototype = new tmpPrototypeA(); | |
B.prototype.super = A.prototype; | |
B.prototype.constructor = B; | |
B.prototype.seed = null; | |
B.prototype.b = function b() { | |
preLog('B.prototype.b()'); | |
}; | |
B.prototype.f = function f() { | |
preLog('B.prototype.f() ' + this.seed); | |
}; | |
// C extends B | |
var C = function C() { | |
B.call(this); | |
this.seed = Math.floor(Math.random() * 1000); | |
preLog('C() ' + this.seed); | |
}; | |
var tmpPrototypeB = function tmpPrototypeB() {}; | |
tmpPrototypeB.prototype = B.prototype; | |
C.prototype = new tmpPrototypeB(); | |
C.prototype.super = B.prototype; | |
C.prototype.constructor = C; | |
C.prototype.seed = null; | |
C.prototype.c = function c() { | |
preLog('C.prototype.c()'); | |
}; | |
C.prototype.f = function f() { | |
preLog('C.prototype.f() ' + this.seed); | |
}; | |
C.prototype.g = function g() { | |
this.super.super.f(); | |
this.super.f(); | |
this.f(); | |
}; | |
// new B instance | |
var d = new B(); | |
// calling method a on instance d | |
d.a('called within scope of instance d'); | |
// replace method a on A | |
A.prototype.a = function a(msg) { | |
preLog('A.prototype.a() - ' + msg); | |
}; | |
// calling method a on instance d | |
d.a('called within scope of instance d'); | |
preLog('d instanceof A ' + (d instanceof A)); | |
preLog('d instanceof B ' + (d instanceof B)); | |
preLog('d instanceof C ' + (d instanceof C)); | |
// new C instance | |
var e = new C(); | |
e.a('called within scope of instance e'); | |
e.g(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment