简体   繁体   English

如何在不同的 Java 文件中调用方法?

[英]How to call a method in a different Java File?

So I'm pretty new to Java and following a course to learn some coding.因此,我对 Java 很陌生,并且学习了一些编码。 Now for this course, I've got a book with theory and exercises.现在,对于这门课程,我有一本包含理论和练习的书。 Now I'm stuck at a certain exercise but the book doesn't provided theory or answers to this exercise.现在我被困在某个练习上,但是这本书没有提供这个练习的理论或答案。

So I've got this Class file ( DateFirstTry.java ) and a Main file, which runs the program.所以我有这个 Class 文件( DateFirstTry.java )和一个运行程序的主文件。

public class DateFirstTry {

    public String month;
    public int day;
    public int year;
    
    public void writeOutput()
    {
        System.out.println(month + " " + day + ", " + year);
    }
    
    public void makeItNewYears()
    {
        month = "January";
        day = 1;
    }
   
    public void yellIfNewYears()
    {
        if ( (month.equalsIgnoreCase("January") ) && (day == 1) ) 
            System.out.println("Hurrah!");
        else 
            System.out.println("Not New Year's Day.");
    }
}

public class DateFirstTryDemo {

    public static void main(String args[]) {
        DateFirstTry date1, date2;
        date1 = new DateFirstTry();
        date2 = new DateFirstTry();
        date1.month = "December";
        date1.day = 31;
        date1.year = 2012;
        System.out.println("date1:");
        date1.writeOutput();
    
        date2.month = "July";
        date2.day = 4;
        date2.year = 1776;
        System.out.println("date2:");
        date2.writeOutput();
    
        DateFirstTry makeItNewYears;
    
        DateFirstTry yellIfNewYears;
}

So, they explained how I should create the makeItNewYears and yellIfNewYears methods, but don't explain how to call them in the main code.所以,他们解释了我应该如何创建makeItNewYearsyellIfNewYears方法,但没有解释如何在主代码中调用它们。 I've tried somethings and DateFirstTry makeItNewYears;我已经尝试过一些东西和DateFirstTry makeItNewYears; is the only thing that doesn't give an error, but also doesn't give any output.是唯一没有给出错误,但也没有给出任何 output 的东西。

I hope someone can help me with this!我希望有人可以帮助我!

A class can have a state and behavior. class 可以具有 state 和行为。 State is data. State 是数据。 In your class DateFirstTry , these are the variables contributing to the state:在您的 class DateFirstTry中,这些是导致 state 的变量:

    public String month;
    public int day;
    public int year;

Methods represent a specific behavior or a set of operations.方法表示特定的行为或一组操作。 The behavior of the class performs action on the data. class 的行为对数据执行操作。 It may or may not change the state of the class while performing these actions.在执行这些操作时,它可能会或可能不会更改 class 的 state。 For instance,例如,

public void makeItNewYears()
{
    month = "January";
    day = 1;
}

the method makeItNewYears() changes the state, since it changes the value of month and day . makeItNewYears()方法改变了 state,因为它改变了monthday的值。 On the other hand,另一方面,

public void writeOutput()
{
    System.out.println(month + " " + day + ", " + year);
}

the method does some action but does not change state.该方法会执行一些操作,但不会更改 state。 It just uses the data but does not change its value.它只使用数据但不改变其值。

An object is an instance of this class. object 是此 class 的一个实例。 If Person is a class, then John is an instance of this class.如果Person是 class,则John是此 class 的实例。 A class can have any number of instances. class 可以有任意数量的实例。 Every instance has its own state and all the behavior defined in the class.每个实例都有自己的 state 以及 class 中定义的所有行为。 To create an instance, you use the following syntax:要创建实例,请使用以下语法:

DateFirstTry date1 = new DateFirstTry();
DateFirstTry date2 = new DateFirstTry();

date1 and date2 are two instances/objects of this class. date1date2是此 class 的两个实例/对象。 They have the same properties month , day and year .它们具有相同的属性monthdayyear But their values can be different for each object.但是对于每个 object,它们的值可能不同。 For calling a method, you need to call it on an instance.要调用方法,您需要在实例上调用它。 We have two instances date1 and date2 .我们有两个实例date1date2 For calling writeOutput() use:调用writeOutput()使用:

date1.writeOutput()

You can call the same method on date2 as well, it will perform the operation on its data.您也可以在date2上调用相同的方法,它将对其数据执行操作。

Each object is able to perform this behavior.每个 object 都能够执行此行为。

date1.yellIfNewYears()

will check if the values of month, day and year of date1 is New Years.将检查date1的月、日和年的值是否为新年。 If you call the same method as:如果您调用与以下相同的方法:

date2.yellIfNewYears()

it will perform this operation with the month, date and year value in date2 .它将使用date2中的月份、日期和年份值执行此操作。

Since these are instance methods, you first need to create an instance of DateFirstTry , like eg, you did with date1 :由于这些是实例方法,因此您首先需要创建DateFirstTry的实例,例如,您对date1所做的操作:

DateFirstTry someDate = new DateFirstTry();

You can then call methods on this instance using the .然后,您可以使用. syntax, followed by the method's name, and parentheses ( () ) containing the arguments you want to pass to the method (in this case - none):语法,后跟方法名称,括号 ( () ) 包含要传递给方法的 arguments (在本例中 - 无):

someDate.makeItNewYears();
someDate.yellIfNewYears();

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

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