2016-11-18 3 views
1

Ich bin verwirrt über objektorientierte Programmierung und Vererbung in Javascript.Wie erben Sie statische und nicht statische Eigenschaften in Javascript?

Ich denke, das Studium des folgenden Codes wird eine gute Möglichkeit sein, die Vererbung in Javascript zu verstehen. Die Klasse B beide erbt und überschreibt Eigenschaften der Basisklasse A.

jemand So könnte mir bitte zeigen eine kurze Äquivalent der folgenden Java-Code in Javascript

public class A { 
    public static String a = "a", b = "b"; 

    public static String c() { 
     return "c"; 
    } 
    public static String d() { 
     return "d"; 
    } 

    public String e = "e", f = "f"; 

    public String g() { 
     return "g"; 
    } 
    public String h() { 
     return "h"; 
    } 
} 
public class B extends A { 
    public static String a = "A"; 

    public static String c() { 
     return "C"; 
    } 

    public String e = "E"; 

    public String g() { 
     return "G"; 
    } 
} 

so dass die folgende Javascript-Code gibt den entsprechenden Ausgabe

var a = new A(); 
var b = new B(); 
console.log(A.a); // a 
console.log(B.a); // A override static property 
console.log(A.b); // b 
console.log(B.b); // b inherit static property 
console.log(A.c()); // c 
console.log(B.c()); // C override static method 
console.log(A.d()); // d 
console.log(B.d()); // d inherit static method 
console.log(A.e); // e 
console.log(B.e); // E override non-static property 
console.log(A.f); // e 
console.log(B.f); // e inherit non-static property 
console.log(a.g()); // g 
console.log(b.g()); // G override non-static method 
console.log(a.h()); // h 
console.log(b.h()); // h inherit non-static method 

Antwort

Verwandte Themen