我正在尝试学习Java,我正在学习Math库。

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

I'm trying to learn Java, and I was studying the Math library

问题

I had to put the private static double() because it didn't run and java kept asking for me to do that

public class Math {
    public static void main(String[] args) {

        double z = -9.8;
        double A;

        A = Math.abs(z);

        System.out.println("Valor absoluto de " + z + " = " + A);
    }

    private static double abs(double z) {

        double A = abs(z);

        return A;
    }
}

and i don't know what it's wrong, help please

请解释为什么我必须添加private static以及我必须在其中写什么。

英文:

So, I was trying to run this code:
I had to put the private static double() because it didn't run and java kept asking for me to do that

public class Math {
	public static void main(String[] args) {
		
		double z = -9.8;
		double A;

		
		A = Math.abs(z);
		
		System.out.println("Valor absoluto de " + z + " = " + A);
	}

	
	private static double abs(double z) {
		
		double A = abs(z);
		
		return A;
	}
}

and i don't know what it's wrong, help please

please explain why do I have to do the private static and what do I have to write in it

答案1

得分: 2

以下是您要翻译的内容:

这可能是一个稍微令人困惑的话题,如果您对Java还不熟悉的话。

首先,您需要理解计算机科学中的“对象”概念。

正如您可能知道的,编程语言可以划分为被称为“范式”的类别。Java 与其他几种编程语言,尤其是 C++C#Visual Basic,属于所谓的“面向对象”范式。

在计算机科学中,一个“对象”通常被定义如下。

"... 一个对象可以是变量、数据结构、函数或方法。"

需要注意的是,在大多数Java文献中,所谓的“对象”是指一个“类”,而不是变量或方法。这不是一个标准,只是一个常见的情况。

您的问题涉及为什么要编写“private static”。

static”关键字是您方法的上下文修饰符。换句话说,您正在取消对实例的需求 - 上下文是“静态”的,就其声明而言。

"具有static修饰符的静态方法应该使用类名调用,无需创建类的实例。"

例如

Example.methodName();

而不是

Example example = new Example();
example.methodName();

由于类的变量和方法都在一个可能的实例上下文中,静态修饰符会将其从该上下文中移除。因此,您不能从静态上下文中访问实例变量和方法。

就像您可以在没有实例的情况下访问_Math_方法一样。

考虑以下内容。

String string = "stack overflow";

static void methodName() {
    /* 'string' 在这里将无法访问 */
}

因此,总结一下,您可以为您的方法提供一个静态修饰符,以从“main”方法中访问它,因为“main”方法是静态的。

此外,您还可以简单地实例化您的封闭类,并以这种方式访问非静态方法。

public static void main(String[] args) {
    double z = -9.8;
    double A;
    
    Math math = new Math();
    A = math.abs(z);

    System.out.println("Valor absoluto de " + z + " = " + A);
}

private double abs(double z) {

    double A = abs(z);

    return A;
}

此外,我认为您的意思是这样写。

private double abs(double z) {

    double A = java.lang.Math.abs(z);

    return A;
}
英文:

This can be a slightly confusing topic, if you're new to Java.

First, you'll have to understand the concept of objects in computer science.

As you may know, coding languages can fall into categories referred to as paradigms.
Java, along with several others—notably, C++, C#, and Visual Basic—are within, what is termed an object-oriented paradigm.

In computer science, an object is typically defined as follows.

> "... an object can be a variable, a data structure, a function, or a method."

As a note, you'll find in most Java literature, an "object" is in reference to a class, and not the variables or methods.
This is not a standard, it's just a common occurance.

Your question is regarding why you have to write "private static".

The static keyword is a context modifier for your method.
In otherwords, you're negating the need for an instance—the context is static, in terms of its declaration.

> "Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class"

For example

Example.methodName();

As, opposed to

Example example = new Example();
example.methodName();

Since a class's variables and methods are within the context of a plausible instance, a static modifier will remove it from that context.
Thus, you cannot access the instance variables and methods, from within a static context.

Similar to how you can access the Math methods without an instance.

Consider the following.

String string = "stack overflow";

static void methodName() {
    /* 'string' will be inaccessible here */
}

So, in conclusion, you can provide a static modifier to your method, to access it from within the main method, which is static.

Additionally, you could simply instantiate your enclosing class, and access the non-static method that way.

public static void main(String[] args) {
    double z = -9.8;
    double A;
    
    Math math = new Math();
    A = math.abs(z);

    System.out.println("Valor absoluto de " + z + " = " + A);
}

private double abs(double z) {

    double A = abs(z);

    return A;
}

Furthermore, I believe you mean to have this written.

private double abs(double z) {

    double A = java.lang.Math.abs(z);

    return A;
}

huangapple
  • 本文由 发表于 2023年6月5日 04:12:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402230.html
匿名

发表评论

匿名网友

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

确定