[英]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);
}
}
我的代碼有什么問題? 編譯器警告的錯誤並不能幫助我理解問題。
您的代碼有5個問題。 首先,變量應以小寫字母開頭,因此我將Age
更改為age
。 其次,構造函數不應位於main
方法中。 第三,您需要printf
而不是println
。 另外,您還需要擺脫那些+
號。 +
是一個運算符,需要2個String
(在任一側),而不僅僅是一個String
。 最后,我假設您的意思是\\t
而不是/t
。 我已更改此設置,還添加了兩個換行符。
更正的代碼是
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);
}
}
構造函數是一種特殊的方法。 這些通常需要在其他方法之外。
//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);
}
默認情況下,構造函數在每個類中。 如果您的Person類中沒有constructuro,那么您將有一個默認值,因此您可以調用
new Person();
通過添加一個帶有string,string,int的構造函數,然后可以松開該默認構造函數。 如果您想要一個沒有輸入參數的默認值,那么您將需要再次添加它
Person(){}
嘗試這個:
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.