Java使用静态字段 [英] Java use of static fields

查看:112
本文介绍了Java使用静态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Java类,我不得不做一个如下的作业:

I am taking java class and I had to do an assignment which reads as follows:

创建一个名为Purchase的类。每个购买都包含发票编号,销售金额和营业税金额。
包括发票编号和销售金额的设置方法。
在销售额的set()方法中,
计算营业税为销售额的7.5%(使用Purchase类中的静态变量)。
还包括一种显示方法,可在格式正确的输出显示中显示购买的详细信息。
将文件另存为Purchase.java。编译并运行您的程序,直到它起作用并且输出看起来不错为止。
按照课程文档中的说明添加必要的文档,然后将.java文件附加到该作业中

Create a class named Purchase. Each Purchase contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set() method for the sale amount, calculate the sales tax as 7.5% (using a static filed in the Purchase class) of the sale amount. Also include a display method that displays a purchase's details in a well formatted output display. Save the file as Purchase.java. Compile and run your programs until it works and the output looks nice. Add the necessary documentation as described in Course Documents, and then attach your .java files to this assignment

我的解决方法如下:

import java.util.*;

 public class Purchase {
    //Properties of Purchase class - static
    private static int invoiceNumber;
    private static double salesAmount;
    private static double salesTax;

    //setter for invoiceNumber
    public static void setInvoiceNum(int invNo){
        invoiceNumber = invNo;
    }

    //setter for salesAmount
    public static void setSalesAmount(double salesAmnt){
        salesAmount = salesAmnt;
        salesTax = 0.075*salesAmnt;
    }

    //public static method to display purchase info
    public static void displaySalesInfo(){
        System.out.println("Invoice Number: " + Purchase.invoiceNumber);
        System.out.println("Sales Amount: " + Purchase.salesAmount);
        System.out.println("Sales Tax: " + Purchase.salesTax);
    } 

    //main method that makes use of the static properties and display method    
    public static void main (String[] args) {
        //ask user to input purchase details
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter your invoice Number:" );
        int inv = scan.nextInt();

        System.out.println("Enter your Sales Amount:");
        double sales = scan.nextDouble();

        // send the user submitted purchase details to the setter methods and call display method
        setInvoiceNum(inv);
        setSalesAmount(sales);
        displaySalesInfo();     
    }

}

这是我老师的评论:
对于此任务,您将使用Purchase类中的静态字段提供7.5%的营业税。在您提交的代码中,您使用了数字文字,大多数情况下,这将视为非常糟糕的编程习惯。设置静态变量salesTax,但是您提供的值是基于实例方法参数的,这是一个逻辑错误,只有税率是静态的,所有其他字段都不应该是静态的,否则每次购买都将相同所提交的赋值代码表明您不了解静态字段的概念。

And here is my teacher's comments: "For this assignment you were to provide a sales tax of 7.5% using a static field in the Purchase class. In the code you submitted you used a numeric literal, which most will consider a very bad programming practice. You did set the static variable salesTax, however the value you give it is based on the instance method parameter, which is a logic error. Only the tax rate was to be static, all other fields should not be, otherwise each purchase would be the same regardless of what was purchased. The assignment code submitted indicated you do not understand the concept of static fields."

我只是不明白???
他说我不了解静态字段。.
我是不是很无知?

I just don't understand?? He says I am not understanding static fields.. Am I that ignorant? This is simply embarrassing..Please shed some light.

推荐答案

静态字段始终是类变量,这意味着此操作的每个实例该类在该类的静态字段上具有相同的引用。

Static fields are always class variables, which means that every instance of this class shares the same reference on this class' static fields.

在您的示例中,现实中的BUT无关紧要,您的代码将毫无用处。我认为您应该做的是:

In your example, it doesn't matter much BUT in the real world, your code would be useless. I think what you should have done is :

1-将salesTaxRate定义为像Juned所写的静态字段

1 - define the salesTaxRate as a static field like Juned wrote

2-定义其他字段不是静态的

2 - define your other fields not static

3-在您的主目录中,最好在某处查看Purchase myPur = new Purchase();

3 - in your main, it would have been better to see somewhere Purchase myPur = new Purchase();

换句话说(很抱歉,我可能在这里直接写了代码^^):

In other words (sorry for a possible mistake, i wrote the code directly here ^^) :

import java.util.*;

 public class Purchase {
    //Properties of Purchase class - static
    private static double taxRate = 0.075; // Shared by all instances

    // Members that are instance-visible
    private int invoiceNumber;
    private double salesAmount;
    private double salesTax;

    //setter for invoiceNumber, not static as it works on a non-static field
    public void setInvoiceNum(int invNo){
        invoiceNumber = invNo;
    }

    //setter for salesAmount, not static as it works on non-static fields
    public void setSalesAmount(double salesAmnt){
        salesAmount = salesAmnt;
        salesTax = Purchase.taxRate*salesAmnt; // Note the Purchase.taxRate notation
    }

    //public static method to display purchase info
    // I keep it static just as an example : here you HAVE to give the purchase to
    // display BECAUSE the method is static
    public static void displaySalesInfo(Purchase pur){
        System.out.println("Invoice Number: " + pur.invoiceNumber);
        System.out.println("Sales Amount: " + pur.salesAmount);
        System.out.println("Sales Tax: " + pur.salesTax);
    } 

    //main method that makes use of the static properties and display method    
    public static void main (String[] args) {
        //ask user to input purchase details
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter your invoice Number:" );
        int inv = scan.nextInt();

        System.out.println("Enter your Sales Amount:");
        double sales = scan.nextDouble();

        // send the user submitted purchase details to the setter methods and call display method
        Purchase myPurchase = new Purchase();
        myPurchase.setInvoiceNum(inv);
        myPurchase.setSalesAmount(sales);
        displaySalesInfo(myPurchase);     
    }

}

这篇关于Java使用静态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆