Java學習方法——類的構造方法
阿新 • • 發佈:2021-02-04
技術標籤:java
class Student{
private int age;
String name;
double score;
Student(){
System.out.println("構造方法一被呼叫");
}
Student(int newage, String newname){
System.out.println("構造方法二被呼叫");
age = newage;
name = newname;
}
Student(int newage, String newname, double newscore){
System.out.println("構造方法三被呼叫");
age = newage;
name = newname;
score = newscore;
}
}
public class Test {
public static void main(String[] args) {
Student stu1 = new Student();
Student stu2 = new Student(18, "czx");
Student stu3 = new Student(18, "czx" , 99.5);
}
}
執行結果