繁体   English   中英

“java中的简单程序中的类型”错误没有定义该方法

[英]“The method is not defined for the type” error in simple program in java

所以我只是想学习Java,在看完一些教程并阅读一些基本的东西后,我不知道为什么这不会运行:

package Test;

public class TestProg {
    public static void main(String[] args) {
        Fetch fetc = new Fetch();
        fetc.more(10, 20);
    }
}

这是Fetch类代码:

package Test;

public class Fetch {

    public Fetch() {

        System.out.println("Fetched!"); 

        int a = 1;
        int b = 2;
        int c;

        while (a < 100 && b < 200) {
            a++;
            b++;
            c = a + b;
            System.out.println(c);
        }

        public void more(int d, int e) {
            System.out.println(d + e);
        }
    }
}

我得到一个“方法更多(int,int)没有为TestProg中的类型提取”错误定义。 如果我删除有关“更多”方法的代码(在两个类中),其余代码将正常运行。 我很难过为什么会发生这种情况,因为代码与我正在研究的例子非常相似。

你的方法more(int d, int e)Fetch()构造函数中

你的fetch方法应该如下(注意标记}关闭构造函数):

package Test;

public class Fetch {



    public Fetch() {

        System.out.println("Fetched!"); 

        int a = 1;
        int b = 2;
        int c;

        while (a < 100 && b < 200) {

            a++;
            b++;
            c = a + b;
            System.out.println(c);
        }
    }//<---- NOTE: closing constructor


    public void more(int d, int e) {

        System.out.println(d + e);


    }

}

more应该位于构造函数之外

这就是缩进非常重要的原因。我们将共同创造一个更好的居住地 - 缩进代码:)

您似乎已在Fetch构造函数中声明了more方法。

这不应该编译,也不能从类外部访问该方法。

要解决此问题,请在more方法声明之前添加一个结束花括号。

More必须位于fetch构造函数之外。

在while循环之后你有一个缺失}。 有一个线索,这个类也会出现错误!

您已在Fetch() constructor实现了more(int, int)方法。 请验证{}

目前的代码

class  Fetch {
   public Fetch() {
        public void more(int d, int e) {
            System.out.println(d + e);
         }
    }
}

需要改为如下

class  Fetch {
   public Fetch() {

    }
    public void more(int d, int e) {
            System.out.println(d + e);
    }      
}

更多应该在构造函数之外,更多的是你错过了紧密的大括号///

用下面的替换你的Fetch类,你在课堂上有一些问题:

class Fetch {

    public Fetch() {

        System.out.println("Fetched!");

        int a = 1;
        int b = 2;
        int c;

        while (a < 100 && b < 200) {

            a++;
            b++;
            c = a + b;
            System.out.println(c);
        }
    }

    public void more(int d, int e) {

        System.out.println(d + e);
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM