简体   繁体   English

设置器和获取器不起作用

[英]Setter and getter not working

This is my assignment about setter and getter and it is not working for some reason. 这是我关于setter和getter的任务,由于某种原因,它没有起作用。 Could anyone check what the problem is for me? 谁能检查我的问题所在? Thank you. 谢谢。

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

        String name;
        String number;
        String Orig;
        String Desti;

        Scanner scan = new Scanner (System.in);

        Flight data = new Flight ();

        System.out.print ("Airline Name: ");
        String AirlineName = scan.nextLine ();
        data.setAirlineName (name);

        System.out.print ("Flight Number: ");
        String FlightNumber = scan.nextLine ();
        data.setFlightNumber (number);

        System.out.print ("Origin: ");
        String Origin = scan.nextLine();
        data.setOrigin (Orig);

        System.out.print ("Destination: ");
        String Destination = scan.nextLine ();
        data.setDestination (Desti);

        System.out.println (data);
    }   
}





public class Flight

{

    private String AirlineName;

    private String FlightNumber;

    private String Origin;

    private String Destination;

    public String setAirlineName()
    {
        String Name = AirlineName;
        return Name;
    }

    public Flight ()
    {
        AirlineName = "";
        FlightNumber = "";
        Origin = "";
        Destination = "";
    }

    public String getAirlineName()
    {
        return AirlineName;
    }

    public void setAirlineName (String name)
    {
        AirlineName = name;
    }

    public String getFlightNumber ()
    {
        return FlightNumber;
    }

    public void setFlightNumber (String number)
    {
        FlightNumber = number;
    }

    public String getOrigin ()
    {
        return Origin;
    }

    public void setOrigin (String Orig)
    {
        Origin = Orig;
    }

    public String getDestination ()
    {
        return Destination;
    }

    public void setDestination (String Desti)
    {
        Destination = Desti;
    }

    public String toString ()
    {
        String result = AirlineName + " flight number " + FlightNumber + " leaves from " + Origin + " to " + Destination + ".";
        return result;
    }
}

Here's the problem: 这是问题所在:

    String AirlineName = scan.nextLine ();
    data.setAirlineName (name);

You are reading a name and putting it into AirlineName , then calling the setter with a different variable as its argument. 您正在读取一个名称并将其放入AirlineName ,然后使用不同的变量作为参数调用设置器。

In fact, you should get a compilation error telling you that name is not initialized. 实际上,您应该得到一个编译错误,告诉您name未初始化。

You also have a second (bogus) setAirlineName method as follows: 您还有第二个(虚假的)setAirlineName方法,如下所示:

public String setAirlineName()
{
    String Name = AirlineName;
    return Name;
}

... which is not a proper setter. ...这不是一个合适的二传手。 But it looks like your main method doesn't call it, so that's not the cause of your problems. 但是看来您的main方法没有调用它,所以这不是问题的原因。 (You are calling the setAttributeName(String) overload ...) (您正在调用setAttributeName(String)重载...)

Finally, please, please, PLEASE, learn to follow standard Java style conventions for identitiers. 最后,请拜托,请学习为标识符遵循标准的Java样式约定。 A variable or attribute name should not start with an uppercase letter. 变量或属性名称应该以大写字母开头。 Change your AirlineName attributes and variables to airlineName , and so on. 将您的AirlineName属性和变量更改为airlineName ,依此类推。

It looks like you're reading the Scanner newLine() calls into new variables, and then passing in variables that haven't been initialized. 似乎您正在将Scanner newLine()调用读入新变量,然后传递尚未初始化的变量。 For example, I think you want something like this: 例如,我认为您想要这样的东西:

System.out.print ("Airline Name: ");
name = scan.nextLine ();
data.setAirlineName (name);

Note that the second line reads the nextLine() into the variable that you're passing into the setter. 请注意,第二行将nextLine()读入您要传递给setter的变量中。

You have this method: 您有此方法:

public String setAirlineName()
{
    String Name = AirlineName;
    return Name;
}

Which conflicts with your actual setter because they are the same name but of different return types, and hence the compiler might be spitting errors, which I assume is what you mean by "not working": 哪一个与您的实际setter冲突,因为它们的名称相同但返回类型不同,因此编译器可能会吐出错误,我认为这是您的意思是“不工作”:

public void setAirlineName (String name)
{
    AirlineName = name;
}

String setAirlineName(void) was probably meant to be String getAirlineName(void) , which you have already implemented just as a basic getter is intended to be: String setAirlineName(void)可能是String getAirlineName(void) ,您已经实现了它,就像打算将基本getter设置为:

public String getAirlineName()
{
    return AirlineName;
}

As Dante617 has answered , another mistake is that you're using different local variables to read input and ignoring the ones you declared (and didn't initialize) before your Scanner object. 正如Dante617回答的那样 ,另一个错误是您正在使用不同的局部变量读取输入,而忽略了在Scanner对象之前声明(但未初始化)的变量。

You are reading the variable AirlineName but passing a different uninitialized variable name to the setter: 您正在读取变量AirlineName但将另一个未初始化的变量name传递给setter:

String AirlineName = scan.nextLine ();
data.setAirlineName (name);
                     ^^^^

Same is the case with other 3 variables FlightNumber , Origin and Destination . 其他3个变量FlightNumberOriginDestination

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM