简体   繁体   English

在Java中使用公共属性时遇到问题

[英]Trouble using a public attribute in java

I have just started coding in java and am trying to wrap my head around classes. 我刚刚开始用Java进行编码,并试图将我的头缠在类上。

I seem to be having trouble using a public attribute in another class. 我似乎在另一个类中使用公共属性时遇到麻烦。 I have 3 classes: one contains the main method; 我有3类:一种包含main方法;另一种包含main方法。 and the other 2 are input and output. 另外两个是输入和输出。 I am using non-static variables and methods and I don't want to use static. 我正在使用非静态变量和方法,并且我不想使用静态。

Even though I have instantiated the input class in the output class, the output class fails to recognize the public attribute. 即使我在输出类中实例化了输入类,但输出类也无法识别public属性。 Why is that so? 为什么会这样?

Here are the 3 classes: 这是3类:

package random;
import java.util.Scanner;
import java.util.Arrays;

public class random
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter your first name: ");  
        input inputObject = new input();
        inputObject.setFirstName();
        output outputObject = new output();
        outputObject.getFirstName();
    }
}

package random;

import java.util.Scanner;
public class input
{
     public String firstName;
     public input() {}
     public void setFirstName() {
        Scanner keyboard = new Scanner(System.in);
        this.firstName = keyboard.nextLine();
    }
}

package random;
import java.util.Scanner;

public class output
{
    public void getFirstName() 
    {
        input inputObject = new input();
        System.out.println("Your first name is " + inputObject.firstName);
    }
}

The input object created in the getFirstName method in the output class is a separate instance from the one created in your main method. 在输出类的getFirstName方法中创建的输入对象是与您的main方法中创建的对象不同的实例。 This means essentially you are creating a new instance of input where the input isn't set for firstName yet so when you print out that property there is nothing to print. 这实际上意味着您正在创建一个新的input实例,其中尚未为firstName设置输入,因此当您打印出该属性时,无需打印任何内容。

In addition the get and set methods for a single property should be inside the same class. 另外,单个属性的get和set方法应该在同一类中。 The general way of doing this is like this: 这样做的一般方式是这样的:

class Foo {

    private String property = "";

    Foo () {}; //empty default constructor

    //sets the property to what is passed in as a parameter
    public void setProperty(String newProperty) {
        this.property = newProperty;
    }

    //returns the property
    public String getProperty() {
        return this.property;
    }
}

Generally all class properties should be private with public setters and getters. 通常,所有类属性都应由公共设置者和获取者私有。 If you are new to programming I suggest reading up on this more. 如果您是编程的新手,建议您进一步阅读。

It's because you're using two different input objects. 这是因为您正在使用两个不同的input对象。 We can tell this because your program has the following line twice 我们可以这样说,因为您的程序有两次以下行

input inputObject = new input();

The input object you assign a name to is a different object from the one you try to read the name from. 您为其分配名称的input对象是一个与您尝试从中读取名称的对象不同的对象。

It makes no sense for the class output to exist at all. output根本不存在是没有意义的。 You should just have a method getFirstName() in the input class. 您应该在input类中只有一个方法getFirstName()

(Also class names in Java usually begin with a capital letter.) (Java中的类名通常也以大写字母开头。)

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

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