[英]How to use constructors in Java
public class Person{
public static void main(String[] args){
//Class constructor
Person(String name, String status, int Age){
this.name = name;
this.status = status;
this.Age = Age;
}
//Object creation
Person one = new Person("John", "Single", 18);
Person two = new Person("Kez", "Single", 21);
Person three = new Person("Bob", "Married", 31);
//Print out attributes
System.out.println("Person one Profile: %s/t%s/t%d", +one.name, +one.status, +one.Age);
System.out.println("Person two Profile: %s/t%s/t%d", +two.name, +two.status, +two.Age);
System.out.println("Person three Profile: %s/t%s/t%d", +three.name, +three.status, +three.Age);
}
}
What could be wrong with my code? 我的代码有什么问题? The errors am being warned of by the compiler are not helping me understand the problem.
编译器警告的错误并不能帮助我理解问题。
There are 5 problems with your code. 您的代码有5个问题。 Firstly, variables should begin with lower case letters, so I have changed
Age
to age
. 首先,变量应以小写字母开头,因此我将
Age
更改为age
。 Secondly, your constructor should not be in the main
method. 其次,构造函数不应位于
main
方法中。 Thirdly, you want printf
rather than println
. 第三,您需要
printf
而不是println
。 Also you need to get rid of those +
signs. 另外,您还需要摆脱那些
+
号。 +
is an operator that requires 2 String
s (one on either side), not just one String
. +
是一个运算符,需要2个String
(在任一侧),而不仅仅是一个String
。 Finally, I assume you mean \\t
rather than /t
. 最后,我假设您的意思是
\\t
而不是/t
。 I have changed this and also added two line breaks. 我已更改此设置,还添加了两个换行符。
The corrected code is 更正的代码是
public class Person {
private String name;
private String status;
private int age;
Person(String name, String status, int age){
this.name = name;
this.status = status;
this.age = age;
}
public static void main(String[] args) {
//Object creation
Person one = new Person("John", "Single", 18);
Person two = new Person("Kez", "Single", 21);
Person three = new Person("Bob", "Married", 31);
//Print out attributes
System.out.printf("Person one Profile: %s\t%s\t%d\n", one.name, one.status, one.age);
System.out.printf("Person two Profile: %s\t%s\t%d\n", two.name, two.status, two.age);
System.out.printf("Person three Profile: %s\t%s\t%d", three.name, three.status, three.age);
}
}
Constructor are a special kind of method. 构造函数是一种特殊的方法。 These generally need to be outside of other methods.
这些通常需要在其他方法之外。
//Class constructor
Person(String name, String status, int Age){
this.name = name;
this.status = status;
this.Age = Age;
}
public static void main(String[] args){
//Object creation
Person one = new Person("John", "Single", 18);
Person two = new Person("Kez", "Single", 21);
Person three = new Person("Bob", "Married", 31);
//Print out attributes
System.out.println("Person one Profile: %s/t%s/t%d", +one.name, +one.status, +one.Age);
System.out.println("Person two Profile: %s/t%s/t%d", +two.name, +two.status, +two.Age);
System.out.println("Person three Profile: %s/t%s/t%d", +three.name, +three.status, +three.Age);
}
Constructors are in every class by default. 默认情况下,构造函数在每个类中。 if you didnt have a constructuro in your Person class then you would have a default one so you could call
如果您的Person类中没有constructuro,那么您将有一个默认值,因此您可以调用
new Person();
By adding a constructor with string, string, int then you loose this default constructor. 通过添加一个带有string,string,int的构造函数,然后可以松开该默认构造函数。 If you want a default one with no input parameters then you will need to include it again
如果您想要一个没有输入参数的默认值,那么您将需要再次添加它
Person(){}
Try this: 尝试这个:
class Test{
public static void main(String[] args){
//Object creation
Person one = new Person("John", "Single", 18);
Person two = new Person("Kez", "Single", 21);
Person three = new Person("Bob", "Married", 31);
//Print out attributes
System.out.printf("Person one Profile:\t %s\t%s\t%d%n", one.name, one.status, one.age);
System.out.printf("Person two Profile:\t %s\t%s\t%d%n", two.name, two.status, two.age);
System.out.printf("Person three Profile: %s\t%s\t%d%n", three.name, three.status, three.age);
}
}
//Class constructor
class Person{
//object fields
String name;
String status;
int age;
//constructor
Person(String name, String status, int age){
//overloading... if you use different names you can remove 'this'
this.name = name;
this.status = status;
this.age = age;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.