我调用方法没有对象为什么会运行?

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

I call method without object why is it run?

问题

public class SalaryManager {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SalaryManager sm = new SalaryManager();
        System.out.println(sm.getMonthlySalary(20000000));
    }

    public double getMonthlySalary(int yearlySalary) {
        double monthlySalary = yearlySalary / 12.0;

        double tax = calculateTax(monthlySalary);//HERE
        double pension = calculateNationalPension(monthlySalary);//HERE
        double insurance = calculateHealthInsurance(monthlySalary);//HERE
        double sum = tax + pension + insurance;

        monthlySalary -= sum;

        return monthlySalary;
    }

    public double calculateTax(double monthSalary) {
        return monthSalary * 0.125;

    }

    public double calculateNationalPension(double monthSalary) {
        double pension = monthSalary * 0.081;

        return pension;
    }

    public double calculateHealthInsurance(double monthSalary) {
        double insurance = monthSalary * 0.135;

        return insurance;
    }
}

这段代码定义了一个名为SalaryManager的Java类,其中包含了一些方法来计算月薪和相关税收、养老金以及健康保险的费用。在main方法中,它创建了SalaryManager类的对象sm,然后调用了getMonthlySalary方法来计算年薪为20000000的员工的月薪。

关于你提到的方法调用的问题,你可以在非静态方法中调用其他非静态方法,而无需创建对象。在getMonthlySalary方法中,你调用了calculateTaxcalculateNationalPensioncalculateHealthInsurance这些非静态方法,这是允许的。方法之间的调用在类的内部是有效的,因此代码可以正常工作。

英文:
public class SalaryManager {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	SalaryManager sm=new SalaryManager();
	System.out.println(sm.getMonthlySalary(20000000));
}

public double getMonthlySalary(int yearlySalary) {
	double monthlysalary=yearlySalary/12.0;
	
	double tax=calculateTax(monthlysalary);//HERE
	double pension=calculateNationalPension(monthlysalary);//HERE
	double insurance=calculateHealthInsurance(monthlysalary);//HERE
	double sum=tax+pension+insurance;
	
	monthlysalary-=sum;
	
	return monthlysalary;
}

public double calculateTax(double monthSalary) {
	return monthSalary*0.125;
	
}

public double calculateNationalPension(double monthSalary) {
	double pension=monthSalary*0.081;
	
	return pension;
}

public double calculateHealthInsurance(double monthSalary) {
	double insurance=monthSalary*0.135;
	
	return insurance;
}
}

I know that if I call method, then I must make object without static method;
But I call method without object in "getMonthlySalary Method", It run well.
How does this code work?

答案1

得分: 4

当您已经在一个成员方法中时,类似于calculateTax(monthlysalary);的写法等同于this.calculateTax(monthlysalary);。您正在调用当前对象上的方法。要更好地理解这一点,了解一下Java中的this代表什么。

英文:

When you are already in a member method, something like calculateTax(monthlysalary); is shorthand for this.calculateTax(monthlysalary);. You are calling the method on the current object. To understand this better, learn about what this means in Java.

答案2

得分: 1

你可以从"getMonthlySalary"方法中调用任何方法,因为这个方法不是静态方法。你已经在主方法中创建了一个对象,然后调用了"getMonthlySalary"方法。所以代码运行正常。

英文:

You can call any method from "getMonthlySalary" method as this method is not static method. You already have created an object in main method and then you called the method "getMonthlySalary". So the code works fine.

答案3

得分: 0

这是绝对不可能的,在没有附加到它的对象的情况下调用非静态方法。
非静态方法使程序员以两种特别显著的方式访问对象的成员:

  1. 通过 this 关键字
  2. 直接访问对象的其他成员,而不需要显式引用对象。

您认为可以在没有与之关联的对象的情况下运行的语句实际上是以后一种方式访问对象的成员。

因此,对于以下形式的对象:

obj a
{
 var one;
 非静态方法 two();
}

在非静态方法 two() 中:

a.one 等效于 this.oneone

英文:

It is absolutely impossible to call a non-static method without an object attached to it.
A non-static method enables the programmer to access the members of an object in two especially notable ways:

  1. Via the this keyword
  2. Accessing other members of the object directly without
    referring to the object explicitly.

The statements you believe are functioning without an object linked to them are actually accessing the object's members in the latter manner.

So, for an object of the following form:

obj a
{
 var one;
 non-static method two();
}

Inside the non-static method two():

a.one is equivalent to both this.one and one.

huangapple
  • 本文由 发表于 2020年8月1日 12:21:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63201740.html
匿名

发表评论

匿名网友

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

确定