打印 Java 构造函数的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33501500/
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
Print the value of a Java constructor
提问by Kevinb4940
I am a java beginner and I am trying to get used to objects. Is there anyway that I can print out the value of a constructor in main? How can I print out the value's Kevin,20? Thanks
我是一名 Java 初学者,我正在努力适应对象。无论如何,我可以打印出main中构造函数的值吗?如何打印出值的 Kevin,20?谢谢
public class MainConstructor {
public static void main(String[] args) {
ConstructorClass emp1 = new ConstructorClass("Kevin", 20);
}
}
//Constructor Class
//构造函数类
public class ConstructorClass {
private String name;
private int number;
public ConstructorClass(String name, int number) {
this.name = name;
this.number = number;
System.out.println("called");
}
}
回答by jonk
Add a toString()
method to ConstructorClass
:
添加一个toString()
方法到ConstructorClass
:
public String toString() {
return name + "," + number;
}
Then call this from main
:
然后从main
以下调用:
public static void main(String[] args) {
ConstructorClass emp1 = new ConstructorClass("Kevin",20);
System.out.println(emp1.toString());
}
回答by Grenther
Try using toString() in your class
尝试在您的班级中使用 toString()
public String toString() {
return this.name + "," + this.number;
}
and in your main just do emp1.toString();
to print it to your console
并在您的主要工作中将emp1.toString();
其打印到您的控制台
回答by The Law
Constructor is basically just another method (but for the love of what is holy, never say that during interview or even to your professor) so there is nothing wrong with doing this:
构造函数基本上只是另一种方法(但出于对神圣的热爱,永远不要在面试时甚至对你的教授说)所以这样做没有错:
public class ConstructorClass {
private String name;
private int number;
public ConstructorClass(String name, int number) {
this.name = name;
this.number = number;
System.out.println(name+" "+number);
}
}
But this solution is really ugly and kind of "hotfixy". Better solution would be to have constructor to only get the values and have separate method to print what you want:
但是这个解决方案真的很丑陋而且有点“热修复”。更好的解决方案是让构造函数只获取值并有单独的方法来打印你想要的内容:
public ConstructorClass(String name, int number) {
this.name = name;
this.number = number;
}
void printNameAndNumber() {
System.out.println(name+" "+number);
}
And use the class like this in your main
并在您的主要内容中使用这样的课程
ConstructorClass c = new ConstructorClass("John",85)
c.printNameAndNumber();
Also some people like to handle this by going through hoops and loops and overriding ToString
, but that is being too overzealous and there is really no benefit in your case (or any other primitive case).
还有一些人喜欢通过循环和覆盖来处理这个问题ToString
,但这太过分了,对你的情况(或任何其他原始情况)来说真的没有好处。
回答by Rehman
public ConstructorClass(String name, int number) {
this.name = name;
this.number = number;
System.out.println(name + "," + number);
}
回答by Kebab Programmer
If you want to properly print out those values, you should have getter methods set in your methods
如果你想正确地打印出这些值,你应该在你的方法中设置 getter 方法
Example below
下面的例子
public String getName(){
return name;
}
public int getNumber(){
return number;
}
Then to print those values, you should then use methods toString() and method print() to display your values
然后要打印这些值,您应该使用方法 toString() 和方法 print() 来显示您的值
Example
例子
public String toString(){
return getName() + " " + getNumber();
}
public void print(){
System.out.println(toString());
}
Then in the class with the main method, you call your print method for that specific class
然后在具有 main 方法的类中,为该特定类调用打印方法
Example
例子
ConstructorClass emp1 = new ConstructorClass("Kevin",20);
emp1.print();
Hope this helped, Enjoy :)
希望这有帮助,享受:)