Zum Inhalt
Javascript-OOP
Aus ProgrammingWiki
<
Bergervei
Archivierte Version vom 2016-06-12T07:26:22+00:00.
Aktuelle Version
Inhaltsverzeichnis
1
Kapselung von Daten
2
Vererbung
3
Überschreiben von Methoden
4
Polymorphie
Kapselung von Daten
function ClassName(param) { var wert = param; this.GetWert = function() { return wert; } }
var Objekt = new ClassName(42); Objekt.GetWert();
ausführen
Vererbung
function SuperClass(param1, param2) { var a = param1; var b = param2; this.GetA = function() { return a; } this.GetB = function() { return b; } }
var SuperObjekt = new SuperClass(1, 2, 3); SuperObjekt.GetA();
ausführen
function SubClass(param1, param2, param3) { SuperClass.call(this, param1, param2); var c = param3; this.GetC = function() { return c; } }
var SubObjekt1 = new SubClass(1, 2, 3); SubObjekt1.GetC();
ausführen
Überschreiben von Methoden
function SubSubClass(param1, param2, param3) { SubClass.call(this, param1, param2, param3); var c = param1 + param2 + param3; this.GetC = function() { return c; } }
var SubObjekt2 = new SubSubClass(1, 2, 3); SubObjekt2.GetC();
ausführen
Polymorphie
function AbstractClass() { var a = 0; var b = 0; var c = 0; this.GetA = function() { return a; } this.GetB = function() { return b; } this.GetC = function() { return c; } } SuperClass.prototype = new AbstractClass(); SubClass.prototype = new AbstractClass(); SubSubClass.prototype = new AbstractClass();
var TestObjekt1 = new SuperClass(1, 2); var TestObjekt2 = new SubClass(2, 3, 4); var TestObjekt3 = new SubSubClass(5, 6, 7); TestObjekt1.GetC() + "\n" + TestObjekt2.GetC() + "\n" + TestObjekt3.GetC() + "\n" ;
ausführen
Seite
Quelltext
Versionen/Autoren
Konto registrieren
Anmelden
Navigation
Startseite
Letzte Änderungen
Alle Seiten
Einführungskurs
Hilfe
Dateien
Suche
Suchen
Werkzeuge
Links auf diese Seite
Spezialseiten