有人可以解释一下这段代码是如何编译的吗?

huangapple go评论111阅读模式
英文:

Can someone explain how this code is compiling

问题

我对编程完全是零基础,正在学习Java。我一直在跟着Codecademy的Java课程学习,目前进展顺利,但是我不太明白程序的一些部分:

    public class Store {
        //实例变量
        String productType;
    
        //构造方法
        public Store(String product) {
            productType = product;
        }
    
        public void greetCustomer(String customer) {
            System.out.println("欢迎光临店铺," + customer + "!");
        }
    
        //广告方法
        public void advertise() {
            String message = "出售" + productType + "!";
            System.out.println(message);
        }
    
        //主方法
        public static void main(String[] args) {
            Store lemonadeStand = new Store("柠檬水");
            lemonadeStand.greetCustomer("Tyler");
        }
    }

这段代码的输出是:"欢迎光临店铺,Tyler!"

但是其他部分的代码是如何工作的呢?只有在主方法(main)中调用的方法才会产生输出吗?我只调用了advertise()方法,其他方法的输出被忽略了吗?

谢谢帮助!我真的在努力养成学习代码实际作用的习惯,而不仅仅是编写能运行但不知道原理的代码!
英文:

I'm 110% brand new to coding and I'm learning Java. I have been following along with Codecademy with their Java course and it's been great so far, but I don't know how some of this program works:

public class Store {
    //instance fields
    String productType;

    //constructor method
    public Store(String product) {
        productType = product;
    }

    public void greetCustomer(String customer) {
        System.out.println("Welcome to the store, " + customer + "!");
    }

    //advertise method
    public void advertise() {
        String message = "Selling " + productType + "!";
        System.out.println(message);
    }

    //main method
    public static void main(String[] args) {
        Store lemonadeStand = new Store("Lemonade");
        lemonadeStand.greetCustomer("Tyler");
    }
}

The output of this is: "Welcome to the store, Tyler!"

But what is happening with the rest of the code? Does an output only happen when its method is called in main()? Did I only call the advertise() method and the others are left out of the output?

Thanks for the help! I'm really trying to get in to the habit of learning what my code actually does instead of just making something that works and not knowing why!

答案1

得分: 1

为了回答你的问题,你的代码其余部分只是现有的,是的,你需要在main()中调用你的方法来运行代码。就像Sweeper所说,你没有调用advertise方法,只调用了GreetCustomer方法。

如果我看你的代码,这是我会逐步看到的:

首先,你创建了一个名为productType的字符串变量。

然后,在你的第一个方法public Store()中,你接受一个参数(括号内的输入)并将productType赋值为该值。

然后你有一个greetCustomer()方法,它接受一个名为customer的字符串参数,该方法打印"Welcome to the store 'customer'!"(其中参数值为customer)。

你的advertise()方法不接受输入,而是直接打印消息行。

Main创建了一个名为lemonadeStand的新Store变量,并且productType变量/字符串设置为"Lemonade"。

你正在打印的是greetCustomer()方法,它在main中被调用为lemonadeStand.greetCustomer("Tyler"),其中参数为"Tyler",返回"Welcome to the store Tyler!"。

如果你想要调用advertise,你可以这样做:lemonadeStand.advertise();

英文:

To answer your questions, the rest of your code is just present and yes you need to have your methods called in main() to run the code. And like Sweeper said, you did not call advertise method, only GreetCustomer().

If I were to look at your code, this is actually how I'd see it step-by-step:

First you create a String variable called productType.

Then in your first method public Store() you are taking in an argument(the input in the bracket) and assigning productType to be that value.

You then have a greetCustomer() method that takes in a String argument called 'customer' and the method prints "Welcome to the store '(parameter value called customer)'!".

Your advertise() method does not take in an input but rather prints the message line.

Main creates a new Store variable called lemonadeStand and the productType variable/string is set to "Lemonade"

What you are printing is the greetCustomer() method which is called in main as lemonadeStand.greetCustomer("Tyler") which takes in "Tyler" as the arg and returns "Welcome to the store Tyler!"

If you wanted to get advertise you'd do lemonadeStand.advertise();

答案2

得分: 1

对于任何Java程序,main()方法是入口点。您将代码编译为类文件。然后执行类文件以运行程序。在运行时,JRE将检查类文件中的main()方法。它将从那里开始执行代码。如果没有main()方法,JRE将抛出错误。因此,要执行任何代码片段,您应确保从main()方法调用它,或者从实际从主方法调用的方法中调用它。在这种情况下,您可以从main方法调用advertise(),或者从从main()方法调用的greetCustomer()方法调用它。在这两种情况下,您将在控制台中打印出预期的行。

英文:

For any Java program, main() method is the entry point. You compile your code to class file. Then you execute the class file to run the program. At run time, JRE will check for main() method in the class file. It will start executing code from there. If there is no main() method, JRE will throw error. So to execute any piece of code, you should make sure that you are calling it from main() method or calling it from a method which is actually getting called from the main method. In this case, you can call advertise() from main method or call it from greetCustomer() method which is getting called from main() method. In both cases, you will get the expected line printed in the console.

huangapple
  • 本文由 发表于 2020年9月13日 14:03:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63867673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定