简体   繁体   English

Java:在另一个类中使用类中的对象

[英]Java: using object from class in another class

I have two different files.我有两个不同的文件。 In the file below I create a new object with an attribute and I create a method to retrieve that attribute.在下面的文件中,我创建了一个具有属性的新对象,并创建了一个方法来检索该属性。

public class Journey
{
    public double singleCost;

    public void setSingleCost(double cost) {
        singleCost = cost;
    }

    public double getSingleCost() {
        return singleCost;
    }

    public static void main (String[] args) {
        Journey leicester_loughborough = new Journey();
        leicester_loughborough.setSingleCost(2.5);

        Journey leicester_nottingham = new Journey();
        leicester_nottingham.setSingleCost(3.5);
    }
}

In a separate file, I would like to print out that object's attribute.在一个单独的文件中,我想打印出该对象的属性。 I'm not sure how to do that though.我不知道该怎么做。 The following doesn't seem to work.以下似乎不起作用。

public class JourneyMethods
{    
    public static void main (String[] args) {
        System.out.println(leicester_loughborough.getSingleCost());
    }
}

I would appreciate any help, thanks.我很感激任何帮助,谢谢。

You are thinking about this too much as main methods.您过多地将此视为主要方法。

Your JourneyMethods main method knows nothing about the Journey class unless you tell it .您的JourneyMethods主方法对Journey类一无所知,除非您告诉它 JourneyMethods will be able to interact with a Journey instance if you create a reference to a Journey .如果您创建对Journey的引用, JourneyMethods将能够与Journey实例交互。

You simply create a getter()-method for the attribute you want to have in the class that has it (in your case Journey.class).您只需为您想要在拥有它的类中拥有的属性创建一个 getter() 方法(在您的情况下为 Journey.class)。 Like this:像这样:

public double getSingleCost() {
    return singleCost;
}

You can then get the attribute by calling the getSingleCost() from the object-instance you have.然后,您可以通过从您拥有的对象实例调用 getSingleCost() 来获取属性。 Like this:像这样:

double cost = leicester_loughborough.getSingleCost();

Please note that this is really basic stuff and you might want to read some more Java tutorials first.请注意,这是非常基本的内容,您可能想先阅读更多 Java 教程。

Oh, and your program is only supposed to have one main() method as entry point.哦,你的程序应该只有一个main()方法作为入口点。 (there are reasons to have more than one but those are very case-specific). (有多个原因,但这些都是针对特定情况的)。 If you run your program in the way it's written in the OP, the address spaces as well as the execution of the two applications are seperated from each other and you do not have a means to access objects that are contained in a different application (well, there are ways like serialization, but those are more advanced topics).如果您按照 OP 中编写的方式运行您的程序,地址空间以及两个应用程序的执行将彼此分开,并且您无法访问包含在不同应用程序中的对象(好吧,有像序列化这样的方法,但这些是更高级的主题)。 Have your classes/objects contained in the same program.将您的类/对象包含在同一个程序中。

Edit: Fixing OP's code to work as intended:编辑:修复 OP 的代码以按预期工作:

public class Journey
{
    public double singleCost;

    public void setSingleCost(double cost) {
        singleCost = cost;
    }

    public double getSingleCost() {
        return singleCost;
    }
}

JourneyMethods:旅程方法:

public class JourneyMethods
{    
    public static void main (String[] args) {
        Journey leicester_loughborough = new Journey();
        leicester_loughborough.setSingleCost(3.5);
        System.out.println(leicester_loughborough.getSingleCost());
    }
}

You need to create the two journey objects in your JourneyMethods class so you have a reference to them.您需要在 JourneyMethods 类中创建两个旅程对象,以便对它们进行引用。 Also you should refer to the 'files' as classes, because 'file' usually implies files on the filesystem, not files representing Java classes.此外,您应该将“文件”称为类,因为“文件”通常意味着文件系统上的文件,而不是代表 Java 类的文件。 That confused me a bit.这让我有点困惑。

Your class object :你的类对象:

public class Journey
{
    public double singleCost;

    public void setSingleCost(double cost) {
        singleCost = cost;
    }

    public double getSingleCost() {
        return singleCost;
    }

}

Your main class :您的主要课程:

public class JourneyMethods
{    
    public static void main (String[] args) {
        Journey Leicester_loughborough = new Journey();
        leicester_loughborough.setSingleCost(2.5);
        Journey leicester_nottingham = new Journey();
        leicester_nottingham.setSingleCost(3.5);
        System.out.println(leicester_loughborough.getSingleCost());
    }
}

This is how Java works, I think that you are confused with C (with .h and .c files) Having a main() function in your Journey class does not make sense, you can instantiate your object from anywhere in your code, so have it in your main() function :)这就是 Java 的工作方式,我认为您对 C(带有 .h 和 .c 文件)感到困惑把它放在你的 main() 函数中:)

In Java, you generally want to put methods in the Object class.在 Java 中,您通常希望将方法放在 Object 类中。

To access the object attributes, you have to make the objects in the external class and use getter and setter methods to get the values of the objects.要访问对象属性,您必须在外部类中创建对象并使用 getter 和 setter 方法来获取对象的值。

public class Journey{
public double singleCost;

public void setSingleCost(double cost) {
    singleCost = cost;
}

public double getSingleCost() {
    return singleCost;
}  

} }

public class JourneyMethods{    
public static void main (String[] args) {
    Journey Leicester_loughborough = new Journey();
    leicester_loughborough.setSingleCost(2.5);
    Journey leicester_nottingham = new Journey();
    leicester_nottingham.setSingleCost(3.5);
    System.out.println(leicester_loughborough.getSingleCost());
}

} }

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

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