忽略作为参数传递的值 [英] Ignoring values passed in as parameters

查看:105
本文介绍了忽略作为参数传递的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里问了这个问题(认为我会帮助人们) 创建一个不必要的吸气剂,并且发掘了我所拥有的巨大的愚昧之地.

I asked this question here (thinking I would help people) Creating an unnecessary getter and unearthed a huge area of ignorance I have.

此答案中,有人指出,我的代码存在致命缺陷,为方便起见,我引用了以下内容:

In this answer it was pointed out to me I had a fatal flaw in my code and I quote for ease:

这是错误的:

public Patient(final String ptNo, final String ptName,
        final String procDate, final int procType, final String injury,
        final String drName) throws IOException
{
    Patient.ptNo = getPtNo();
    Patient.ptName = getPtName();
    Patient.procDate = getProcDate();
    Patient.procType = getProcType();
    Patient.injury = getPtNotes();
    Patient.drName = getDrName();
}

当您完全忽略作为参数传递的所有值时.而是:

As you're completely ignoring all values passed in as parameters. Instead do:

public Patient(final String ptNo, final String ptName,
        final String procDate, final int procType, final String injury,
        final String drName) throws IOException
{
    Patient.ptNo = ptNo;
    Patient.ptName = ptName;
    Patient.procDate = procDate;
    Patient.procType = procType;
    Patient.injury = injury;
    Patient.drName = drName;
}

您在此处使用参数值设置班级的字段."

Where here you're setting your class's fields with the parameter values."

我不明白的是为什么忽略了这些值.我为例如分别调用方法:

What I don't understand, is why the values are being ignored. I call separate methods for eg:

public static String getPtName()
{
    System.out.print("Enter patient name: \n");
    try
    {
        ptName = stdin.readLine();
    } catch (IOException e)
    {
        System.out.println("Error! Enter a valid option.");
        getPtName();
    }
    return ptName;
}

所以我认为这是相同的,只是写第二段代码的方式更长.

So I thought that was the same, in a longer way of writing the second block of code.

有人可以向我解释一下,为什么与众不同?

Can someone please explain to me, why it is different?

修改 uni的分配要求.

C)为接受患者编号的类提供一个构造函数 (一个字符串),患者姓名(一个字符串),手术日期( 格式dd/mm/yy),程序类型(int),伤害说明(a 字符串)和管理该医生的医生的医生姓名 病人的治疗方法.

C) Provide a constructor for the class that accepts the patient number (a String), patient name (a String), procedure date (a String in the format dd/mm/yy), procedure type (an int), the injury description (a String) and the doctor name of the physician who is administering the patient’s treatment.

此构造函数应使用来初始化实例变量 传入的相应参数值-应该 还初始化患者注释实例变量以适应伤害 最初传入并初始化患者的描述 状态实例变量为"S"(表明新患者已 安排了一个程序).

This constructor should initialise the instance variables with the corresponding parameter values that have been passed in - it should also initialise the patient notes instance variable to the injury description that was passed in initially and initialise the patient status instance variable to ‘S’ (indicating that the new patient has had a procedure Scheduled).

public Patient (String patientNo, String patientName, 
                 String procedureDate, int procedureType,
                 String injuryDescription, String doctorName)

D)为患者编号,患者姓名, 手术日期,患者注意事项和医生姓名实例变量.

D) Implement accessors for the patient number, patient name, procedure date, patient notes and doctor name instance variables.

推荐答案

在第一个示例中:

public Patient(final String ptNo, final String ptName,
               final String procDate, final int procType,
               final String injury, final String drName)
               throws IOException

构造函数从不使用传入的变量.也就是说,它们不会出现在函数主体中的任何位置.

The constructor never uses the variables that are passed in. That is, they don't appear anywhere within the body of the function.

这特别令人困惑,因为函数的参数名称与类变量完全相同,但是Patient.ptNo与参数ptNo相同不是. (实际上,Patient.ptNo应该是this.ptNo,因为它属于该类的特定实例.Patient.ptNo指的是 all 通用的 single 值.类型为Patient的对象.)编写更正的表单时

It's particularly confusing because the parameters to the function have exactly the same names as the class variables, but Patient.ptNo is not the same variable as the parameter ptNo. (Actually, Patient.ptNo should be this.ptNo, because it belongs to this particular instance of the class. Patient.ptNo would refer to a single value that's common to all objects of type Patient.) When you write the corrected form

this.ptNo = ptNo;

您正在将 class 变量设置为与 parameter 变量相同的值.实际上,在这种情况下,您必须限定类变量,否则将使用参数变量.

you're setting the class variable to the same value as the parameter variable. In fact, in this case, you must qualify the class variables, otherwise the parameter variables will be used instead.

一些程序员遵循约定,例如在所有类变量的名称上添加前缀或后缀,以使这些名称冲突不会发生.因此,例如,您可能有

Some programmers follow conventions, such as adding a prefix or suffix to the names of all class variables, to make it impossible for these name collisions to occur. So, for example, you might have

this.c_ptNo = ptNo;

或者只是

c_ptNo = ptNo;

其中c_前缀表示类变量.

这篇关于忽略作为参数传递的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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