我无法理解非常简单的构造函数问题。爪哇

发布于 2024-11-15 23:00:58 字数 436 浏览 1 评论 0原文

  • 创建一个具有打印消息的默认构造函数(不带参数的构造函数)的类。在 main() 方法中,创建此类的对象。
  • 将重载的构造函数添加到步骤 1 中的代码中。新的构造函数应采用 String 参数并将其与消息一起打印。修改 main() 以便它使用 new 构造函数创建该类的第二个对象。

所以第二部分对我来说就像用另一种语言写的,我完全不知道该怎么做,第一部分是我到目前为止得到的:

public class Constructors {
    System.out.println("Message");
    public static void main(String[] args) {
    }
}

当我试图打印该消息时,它给了我一个错误。我很困惑,我不是要求你以任何方式为我做我的实验室,但我对此很困惑,它已经失控了。

  • Create a class with a default constructor (one that takes no arguments) that prints a message. In your main() method, create an object of this class.
  • Add an overloaded constructor to your code from step 1. The new constructor should take a String argument and print it along with your message. Modify main() so that it creates a second object of this class, using the new constructor.

So the second part is literally like written in another language to me I have absolutely no idea how to do that, and the first part here's what I got so far:

public class Constructors {
    System.out.println("Message");
    public static void main(String[] args) {
    }
}

It's giving me an error when I'm just trying to print that message. I'm so confused, I'm not asking you to do my lab for me by any means but I'm so confused by this it's out of control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼忆七猫命九 2024-11-22 23:00:58

构造函数不应该是类的名称,构造函数是创建类实例(对象)的方法,

因此第一点意味着您创建一个不带参数的对象,当您从main 方法

public class WhateverClass{

    //this is the first constructor
    public WhateverClass(){

        System.out.prinln("A message");

    }

    //this is the main method
    public static void main (String[] args){
        new WhateverClass(); //will print the message
    }
}

然后您创建另一个构造函数来重载第一个构造函数,因为它具有相同的签名,只是它需要一个参数。然后你从 main 方法中调用它,就像第一个方法一样。这里:

public class WhateverClass{

    //this is the first constructor
    public WhateverClass(){

        System.out.prinln("A message");

    }

    //this is the second constructor
    public WhateverClass(String message){

        System.out.prinln(message);

    }

    //this is the main method
    public static void main (String[] args){
        new WhateverClass(); //will print the message
        new WhateverClass("A message");
    }
}

你的例子不起作用,因为你的打印方法不在任何方法中,并且无法从它所在的位置执行。

您确实应该阅读有关 OO 编程基础知识的书籍和文章。

The constructor shouldn't be the name of your class, the constructor is the method that creates an instance of your class (object)

So the first point means that you create an object without parameters that will print a message when you call it from the main method

public class WhateverClass{

    //this is the first constructor
    public WhateverClass(){

        System.out.prinln("A message");

    }

    //this is the main method
    public static void main (String[] args){
        new WhateverClass(); //will print the message
    }
}

Then you create another constructor that gonna overload the first one because it gonna have the same signature except it gonna takes a parameter. And then you call it from main method exactly as the first one. Here:

public class WhateverClass{

    //this is the first constructor
    public WhateverClass(){

        System.out.prinln("A message");

    }

    //this is the second constructor
    public WhateverClass(String message){

        System.out.prinln(message);

    }

    //this is the main method
    public static void main (String[] args){
        new WhateverClass(); //will print the message
        new WhateverClass("A message");
    }
}

And your exemple doesn't work because your print method is not in any method and cannot be executed from where it is.

You should really read books and articles about the basics of OO programmation.

迷离° 2024-11-22 23:00:58

方法调用应该是方法体的一部分,或者是初始化表达式或块的一部分。将 println 调用移至实际的构造函数(google 一下),您应该可以开始了。您可以浏览“启动 Java(我的粘性线程)”线程面向初学者(请随意提供建议)。

顺便说一句,默认构造函数和无参数构造函数之间存在差异。默认构造函数始终是无参数构造函数,并且会自动提供,以防您不这样做,但反之则不然。

A method invocation should either be part of a method body or an initializer expression or block. Move that println invocation to an actual constructor (google that) and you should be good to go. You can go through the "Starting Java (my sticky thread)" thread aimed to beginners (feel free to offer suggestions).

BTW, there is a difference between default and no-arg constructor. A default constructor is always a no-arg constructor and is automatically provided in case you don't but the reverse is not true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文