Java 打印方法在课堂上不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19980913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:03:01  来源:igfitidea点击:

Java print method won't work in class

javaclassoopmethods

提问by Anon Omus

I am trying to get the print method in my actor class to print the String that was built in the toString() method. However I keep getting an error. (invalid method declaration, return type required)

我试图在我的演员类中获取打印方法来打印在 toString() 方法中构建的字符串。但是我不断收到错误消息。(无效的方法声明,需要返回类型)

public class actor {

    private String name;
    private String address;
    private int age;


public actor(String name, String address, int age) {

    this.name = name;
    this.address = address;
    this.age = age;


}


public void setName (String name) {

    this.name = name;

}


public void setAddress (String address) {

    this.address = address;
}


public void setAge (int age) {

    this.age = age;

}

public void setFilm () {


}

public String getName () {
    return name;

}

public String getAddress () {
    return address;

}

public String toString (String name, int age, String address){

        return name+" who's "+age+" and lives in "+address;


}


public void print (){
    String a = toString();
    System.out.println(a);

}


print();
}

I have been trying to get this working for quite a while to no avail.

我一直试图让这个工作很长一段时间都无济于事。

回答by alexroussos

You're trying to call print() from your class body. Instead, write a main method and print from there:

您正在尝试从您的班级正文中调用 print() 。相反,编写一个主要方法并从那里打印:

public static void main(String[] args) {
  Actor a = new Actor(...);
  a.print();
}

回答by Dark Knight

Ideally you should do this way. Since purpose of toString() method is to give meaningful String representation of an object.

理想情况下,您应该这样做。由于 toString() 方法的目的是为对象提供有意义的 String 表示。

actor actorObj = new actor();
System.out.println(actorObj );

回答by Masudul

Calling print();on class body is invalid. Remove following method call.

调用print();类主体无效。删除以下方法调用。

 print();

回答by quazzieclodo

First off, you should be calling the print()method from somewhere else (mainfor example). Even with that, you have an error: You are calling the toString()method (with no arguments, which is taken from the Object class). Just remove the arguments from your toStringmethod to override that one. It can see the fields of its own class anyways. With this, you can do something like the following, and take advantage of Java's default toString call:

首先,您应该print()从其他地方调用该方法(main例如)。即便如此,您仍然有一个错误:您正在调用该toString()方法(没有参数,它是从 Object 类中获取的)。只需从您的toString方法中删除参数即可覆盖该参数。无论如何它都可以看到它自己的类的字段。有了这个,您可以执行以下操作,并利用 Java 的默认 toString 调用:

public static void main(String[] args) {
    System.out.println(new Actor("Bob", "410 Main Street", 42);
}

回答by Barzee

Java does not allow you to call a method in the body of a class as you are attempting to do with the line of code that is print();You must put the call to print inside another method. For example

Java 不允许您在类的主体中调用方法,因为您正在尝试使用以下代码行print();调用您必须将调用打印到另一个方法中。例如

public static void main(String[] args) {
    actor a = new actor();
    a.print();
}

回答by David Lau

You should have a main function to let the program run, like:

您应该有一个 main 函数来让程序运行,例如:

remove the last line print() then create a new file call Main.java, write

删除最后一行 print() 然后创建一个新文件调用 Main.java,写

package yourPackage // put them into the same package, 
                    main class can call actor class

public class Main{
   public static void main(String[] args) {
      actor a = new actor();
      a.print();
   }
}

回答by user1339772

Why do you need print()method ? You can just use -

为什么需要print()方法?你可以使用 -

Actor a = new Actor(...);
System.out.println(a);

This will implicitly execute toString()method

这将隐式执行toString()方法

回答by James Dunn

Here's the two parts to the trouble you're having:

这是您遇到的麻烦的两个部分:

  1. In order to run your program, you have to have a main method.
  2. You have to understand what static and non-static mean.
  1. 为了运行你的程序,你必须有一个 main 方法。
  2. 您必须了解静态和非静态的含义。

First, as mentioned by others, you can't just have a method run because it's declared and defined in your class. You actually need to have it called, directly or indirectly, by the main method. The main method is written like this:

首先,正如其他人所提到的,您不能只运行一个方法,因为它是在您的类中声明和定义的。您实际上需要通过 main 方法直接或间接调用它。主要方法是这样写的:

public static void main(String[] args) {
    // Do Stuff
}

Secondly, you have to understand what static and non-static mean. Which means, you have to understand the difference between classes and objects.

其次,您必须了解静态和非静态的含义。这意味着,您必须了解类和对象之间的区别。

Classesare blue prints. They describe how to build a particular type of object, what properties (or fields) it has, and what methods can be called off of it.

Objectsare the actual instancesof objects declared by a class. Think of it like this: A class is like the blueprint for a smart car. The objects are the smart cars themselves.

是蓝图。它们描述了如何构建特定类型的对象、它具有哪些属性(或字段)以及可以从中调用哪些方法。

对象是类声明的对象的实际实例。可以这样想:类就像智能汽车的蓝图。对象是智能汽车本身。

So now, static vs non-static.

所以现在,静态与非静态。

Staticmeans that it belongs to the class (the blueprint), rather than to the actual object. The mainmethod, you will notice, is static. It belongs to the class it's declared in, rather than to any instance objects. This means, outside of itself, the main method knows only about the class that it's in, and any other static methods or objects in that class. Things that are not static belong to the actual objects created from the class -- and the main method will know nothingabout these actual objects, unless they are created inside of the main method itself.

静态意味着它属于类(蓝图),而不是实际的对象。的主要方法,你会发现,是静态的。它属于它所声明的类,而不是任何实例对象。这意味着,在其自身之外,main 方法只知道它所在的类,以及该类中的任何其他静态方法或对象。非静态的东西属于从类创建的实际对象——并且 main 方法对这些实际对象一无所知,除非它们是在 main 方法本身内部创建的。

So, something like this won't work:

所以,这样的事情是行不通的:

public class StuffDoer {
    public void doStuff {
        System.out.println("Doing Stuff");
    }

    public static void main(String[] args) {
        doStuff();  // Won't work!
        // You can't call a non-static, instance method in a static method!
    }
}

Instead, you can first create a new instance object of your class inside of the main method, and then call the non-static instance method off of your instance object:

相反,您可以先在 main 方法内创建类的新实例对象,然后从实例对象中调用非静态实例方法:

public class StuffDoer {
    public void doStuff {
        System.out.println("Doing Stuff");
    }

    public static void main(String[] args) {

        new StuffDoer().doStuff();  // This will work,
        // because now you have an instance to call the instance method off of.
    }
}

This is usually not as good of a choice, but will also work:

这通常不是一个好的选择,但也可以工作:

public class StuffDoer {
    public static void doStuff { //Now, we make this method static
        System.out.println("Doing Stuff");
    }

    public static void main(String[] args) {
        doStuff();  // This will work now, because this method is static.
    }
}

回答by Johan Br?nnmar

By adding a main method to your class and using the constructor for Actor in that method you create an Author object. On this Author object call print().

通过向您的类添加一个 main 方法并在该方法中使用 Actor 的构造函数,您可以创建一个 Author 对象。在这个 Author 对象上调用 print()。

TJamesBoone has given a really good answer to give you an understanding of what is really happening. Follow his answer and it will do as you want.

TJamesBoone 给出了一个非常好的答案,让您了解真正发生的事情。按照他的回答,它会如你所愿。

https://stackoverflow.com/a/19981973/1785341

https://stackoverflow.com/a/19981973/1785341

回答by wassan imtiaz

here is your code.. compile and run.. public class actor {

这是你的代码..编译并运行..公共类演员{

   private String name;
   private String address;
   private int age;


 public actor(String name, String address, int age) {

   this.name = name;
  this.address = address;
  this.age = age;


  }


 public void setName (String name) {

  this.name = name;

 }


public void setAddress (String address) {

  this.address = address;
 } 


 public void setAge (int age) {

  this.age = age;

}

 public void setFilm () {


 }

 public String getName () {
   return name;

}

  public String getAddress () {
    return address;

 }
 @Override

  public String toString (){

       return name+" who's "+age+" and lives in "+address;


   }


   public void print (){
     //String a = toString();
     System.out.println(this);

   }

    public static void main( String[] args )
  {
       actor a = new actor( "xyz","abc",20 );
      a.print();
   }
  }