【问题标题】:How to call the method correctly如何正确调用方法
【发布时间】:2016-06-28 14:08:23
【问题描述】:

我早些时候发布了这种问题,但我得到了否定的回应,因为我没有展示我的尝试。所以我再次发布这个,包括我的尝试,但我仍然不明白 Java 中的方法是如何工作的。 这是我的尝试:

import java.util.Scanner;

public class MethodTest {

    public static exception(String name) {

        if (name == Abudi) {
            System.out.println("Your " + name + " is not allowed to proceed");
        }
    }

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        exception pc = new exception();
        String name;
        System.out.print("Enter your name here: "); name = sc.nextLine();

        pc.exception(name);
    }
}

如何正确调用exception 方法? 谢谢。

【问题讨论】:

  • 您的代码存在几个(实际上很多)问题。我可能建议您在开始编码之前阅读基本的 Java 教程。
  • @Yassin Hajaj 是的,我对编程世界还比较陌生。谢谢你。
  • 我相信“异常”这个名字是java上的保留字。尝试将您的方法名称更改为其他名称。

标签: java string class methods static


【解决方案1】:

使用类名,因为static 方法对于该类的所有实例都是通用的。

MethodTest.exeption(name);

或者以防万一您的方法由调用它的类拥有。

exeption(name);

实际上缺少返回语句。如果您不返回任何内容,请使用void

public static void exception(String name) {
    ...
}

并删除以下行exception pc = new exception();。这没有任何意义,因为根本没有构造方法。

一般来说,调用static方法不需要在调用类的构造函数和创建实例之前。 static 方法不固定在实例上。


最后,main 方法的主体看起来像这样:

Scanner sc = new Scanner(System.in);
System.out.print("Enter your name here: "); 
String name = sc.nextLine();
exception(name);

【讨论】:

  • 并删除exception pc = new exception()这一行,因为它什么都不做。
【解决方案2】:

首先,您将exception“方法”称为构造函数。您可以阅读有关构造函数的更多信息here

要使exception 方法工作,首先需要正确定义它,将返回类型设置为void。这意味着该方法不返回任何内容。

public static void exception(String name) {
    if (name == "Abudi") {
        System.out.println("Your " + name + " is not allowed to proceed.");
    }
}

然后,要调用此方法,请使用:

MethodTest.exception(name); /* If you are calling it from another class */

或者只是:

exception(name); /* If you are calling this static method from its own class */

【讨论】:

  • 所以语法总是 .(..) 如果我从那个类调用它?
  • 如果您从一个不包含您要调用的静态方法的类中调用它,请使用SomeClass.someMethod(...) 调用。否则,如果您从该方法所在的类中调用该静态方法,则可以只使用someMethod(...),但如上所述进行调用并没有错。
【解决方案3】:

你可以看看这个程序。它只需要输入一次并关闭。但是您将能够理解该方法的工作原理。

import java.util.Scanner;

public class ExceptionTest {

    public static void exception(String name) {

        if (name.equals("Abudi")) {
            System.out.println("Your " + name + " is not allowed to proceed");
        }
    }

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

        //exception1(sc.next());
        String name;
        System.out.print("Enter your name here: "); 
        name = sc.nextLine();

        exception(name);
        sc.close();
    }
}

【讨论】:

    【解决方案4】:

    如果你想在 MethodTest 类之外调用方法你需要做的:

    MethodTest methodtest = new MethodTest();
    
    methodtest.exception(name);
    

    或者如果你想调用MethodTest类内部的方法你需要做的:

    exception(name);
    

    您只需在方法main(String[] args) 中调用方法exception(String name) 即可:

    public static void main(String args[]) {
    
        Scanner sc = new Scanner(System.in);
        String name;
        System.out.print("Enter your name here: ");
        name = sc.nextLine();
    
        exception(name);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-02
      • 2019-11-08
      • 1970-01-01
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多