可以在Java的类方法中使用类变量吗?

huangapple go评论77阅读模式
英文:

Can I use class variable in class method in Java?

问题

我正在学习Java,并且在自己练习。
我尝试创建一个类变量 String username,我以为会得到结果
"Hi Nat",因为方法 engHi() 在同一个类中与变量一起。

class Greetings{
    String userName = "Nat";
    public static void engHi(String userName){
        System.out.println("Hi "+userName);
    }
}
public class Main {
    public static void main(String[] args) {
        Greetings.engHi();
    }
}

然后我得到了这个错误消息。

Error:(11, 18) java: method engHi in class com.company.Greetings cannot be applied to given types;
  required: java.lang.String
  found: no arguments
  reason: actual and formal argument lists differ in length

然后我在方法 engHi() 中添加了 this.username = username

class Greetings{
    String userName = "Nat";
    public static void engHi(String userName){
        this.userName = userName;
        System.out.println("Hi "+userName);
    }
}
public class Main {
    public static void main(String[] args) {
        Greetings.engHi();
    }
}

结果是

Error:(12, 18) java: method engHi in class com.company.Greetings cannot be applied to given types;
  required: java.lang.String
  found: no arguments
  reason: actual and formal argument lists differ in length

我原以为,既然方法在同一个类中,方法会自动获取变量 username,所以在调用方法时我没有放任何参数。
那么...这是不是意味着方法不会从同一个类中获取任何变量呢?

英文:

I'm learning Java and practicing by myself.
I tried to make a class variable String username and I thought I would get the result
"Hi Nat" since method engHi() is in the same class with the variable.

class Greetings{
    String userName = "Nat";
    public static void engHi(String userName){
        System.out.println("Hi "+userName);
    }
}
public class Main {
    public static void main(String[] args) {
        Greetings.engHi();
    }
}

And I got this message.

Error:(11, 18) java: method engHi in class com.company.Greetings cannot be applied to given types;
  required: java.lang.String
  found: no arguments
  reason: actual and formal argument lists differ in length

and then I added this.username = username in the method engHi()

class Greetings{
    String userName = "Nat";
    public static void engHi(String userName){
        this.userName = userName;
        System.out.println("Hi "+userName);
    }
}
public class Main {
    public static void main(String[] args) {
        Greetings.engHi();
    }
}

And the result is

Error:(12, 18) java: method engHi in class com.company.Greetings cannot be applied to given types;
  required: java.lang.String
  found: no arguments
  reason: actual and formal argument lists differ in length

I thought since the method is in the same class I thought the method was gonna take the variable username 'automatically' so I didn't put any parameter when I called the method.
So.. It means methods don't take any variable from same class?

答案1

得分: 2

以下是翻译好的内容:

你代码中的问题在于你的方法 engHi() 要求一个用户名字符串作为参数传入。如果你移除 engHi() 的参数,这个错误应该会消失。

class Greetings{
    String userName = "Nat";
    public static void engHi(){ //调用 engHi() 时不需要传入任何参数
        System.out.println("Hi "+userName);
    }
}
public class Main {
    public static void main(String[] args) {
        Greetings.engHi();
    }
}
英文:

The issue with your code here is that your method engHi() requires the a username string to be sent in as a parameter. If you remove the parameters of engHi(), this error should go away.

class Greetings{
    String userName = "Nat";
    public static void engHi(){ //nothing required to be passed in when you call engHi()
        System.out.println("Hi "+userName);
    }
}
public class Main {
    public static void main(String[] args) {
        Greetings.engHi();
    }
}

答案2

得分: 0

在调用中添加用户名,就像这样:Greetings.engHi("Nat");,或者从接受函数中移除它,就像这样:

public static void engHi(){
}

你的 engHi 方法期望一个用户名,在从 Greetings.engHi(); 进行调用时你没有传递用户名。

英文:

Either add a userame in call like Greetings.engHi("Nat"); or remove that from acceptig function like

public static void engHi(){
}

Your methof engHi is expecting a username which you're not passing during the call from Greetings.engHi();

答案3

得分: 0

错误与使用类变量关系不大。

相反,如果一个方法声明为 N 个参数,则需要使用 N 个参数来调用它。

声明:engHi(String userName) - 1 个参数。

调用:Greetings.engHi() - 0 个参数。

参数数量不匹配是一种语言违规。

此外,在这两行代码中:

String userName = "Nat";
public static void engHi(String userName){

username 的两个出现并不指代同一物。第一个声明了类实例的成员;第二个是 engHi 方法的形式参数,它将根据调用者提供的实际参数值进行设置。

英文:

The error has little to do with using class variables.

Rather, if a method is declared with N arguments, it needs to be called with N arguments.

Declaration: engHi(String userName) - 1 argument.

Call: Greetings.engHi() - 0 arguments.

The mismatch in argument count is a language violation.

Moreover, in these two lines:

String userName = "Nat";
public static void engHi(String userName){

the two occurences of username are not referring to the same thing. The first declares a member of an instance of the class; the second is a formal argument of the engHi method, which will be set to whatever value is provided as an actual argument by the caller.

答案4

得分: 0

一个 static 方法只能访问 static 变量。一个非 static 方法可以访问 static 和非 static 变量。

英文:

A static method may only access static variables. A non-static method may access static and non-static variables.

答案5

得分: 0

engHi() 方法中创建一个 Greetings 类的对象。

public static void engHI() { 
   Greetings greetings = new Greetings(); 
   System.out.println("Hi " + greetings.userName);
}
英文:

Create an object of the Greetings class in engHi() method.

 public static void engHI() { 
   Greetings greetings = new Greetings(); 
   System.out.println("Hi " + greetings.userName);
}

huangapple
  • 本文由 发表于 2020年6月29日 02:17:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/62626511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定