JS继承示例:递归过多

时间:2016-10-31 10:51:35

标签: javascript oop inheritance recursion override

抱歉转储问题我是js的新手。我想在D" class"中覆盖var B = function () { }; B.prototype.f2 = function (x) { return 2 * x; }; var C = function () { B.call(this); }; var D = function () { C.call(this); }; D.prototype.f2 = function (x) { return C.prototype.f2.call(this, x) * 7; }; inherit(B, C); inherit(C, D); function inherit(Child, Parent) { Child.prototype = Object.create(Parent.prototype); Child.prototype.constructor = Child; } var d = new D(); console.log(d.f2(3)); 函数。但出于某种原因,火狐告诉我:"过多的递归"。你能否指出我递归发生的地方以及如何使这段代码按预期工作?

public class Main {
public static void listfiles(String path) {
    File directory = new File(path);
    //get all the files from a directory
    if(directory.exists()) {
        File[] fList = directory.listFiles();
        for (File file : fList){

            if (file.isFile()){
                String filepath = file.getAbsolutePath().substring(path.length() + 1);

                if(filepath.contains(".c")
                        || filepath.contains(".cpp")
                        || filepath.contains(".cc")
                        || filepath.contains(".java")) {
                    System.out.println(filepath);

                    try {
                        String[] cmd = { "cccc.exe", file.getAbsolutePath()};
                        System.out.println("CCCC Started! " + file.getAbsolutePath());
                        Process p = Runtime.getRuntime().exec(cmd);
                        p.waitFor();
                    } 
                    catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            } else if (file.isDirectory()){
                listfiles(file.getAbsolutePath());    // file.getName() for just file names
            }
        }
    }
}

public static void main(String args[]) {
    listfiles("C:/proj/platform_packages_apps_contacts");
}

1 个答案:

答案 0 :(得分:5)

两个问题:

  1. 您需要在尝试向其添加属性之前设置XYZ.prototype个对象 。由于您的inherit函数会创建它们,因此您必须确保以正确的顺序执行操作。

  2. 您的inherit来电中有父母和子女的顺序。它是inherit(child, parent),而不是inherit(parent, child)

  3. var B = function () {
    };
    B.prototype.f2 = function (x) {
        return 2 * x;
    };
    
    var C = function () {
        B.call(this);
    };
    inherit(C, B);            // *** Moved and updated
    
    var D = function () {
        C.call(this);
    };
    inherit(D, C);            // *** Moved and updated
    
    D.prototype.f2 = function (x) {
        return C.prototype.f2.call(this, x) * 7;
    };
    
    function inherit(Child, Parent) {
        Child.prototype = Object.create(Parent.prototype);
        Child.prototype.constructor = Child;
    }
    
    var d = new D();
    console.log(d.f2(3));

    ES2015版本,用于比较:

    class B {
      f2(x) {
        return 2 * x;
      }
    }
    
    class C extends B {
    }
    
    class D extends C {
      f2(x) {
        return super.f2(x) * 7;
      }
    }
    
    const d = new D();
    console.log(d.f2(3));