?? constructortest.java
字號:
/** 一個應用程序,用來演示重載的構造方法 */
/** Constructor類,在該類中定義了四個構造方法 */
class Constructor{
private int x;
private double y;//定義私有變量
/** 沒有參數的構造方法 */
Constructor(){
x=0;
y=0.0;
}
/** 一個參數的構造方法 */
Constructor(int x){
this.x=x; //使用this關鍵字標識成員變量,以區別于同名參數。下同
}
/** 一個參數的構造方法,參數與前一構造方法不同 */
Constructor(double y){
this.y=y;
}
/** 兩個參數的構造方法 */
Constructor(int x,double y){
this.x=x;
this.y=y;
}
/** show()方法顯示成員變量 */
void show(){
System.out.println("x="+x+" y="+y);
}
}//Constructor類結束
/** ConstructorTest類 */
public class ConstructorTest{
/** main()方法 */
public static void main(String args[]){
Constructor co1=new Constructor();
Constructor co2=new Constructor(3);
Constructor co3=new Constructor(0.9);
Constructor co4=new Constructor(5,5.9);
//上面四行語句創建了四個Constructor對象,每一個對象的創建使用了不同的構造方法
co1.show();
co2.show();
co3.show();
co4.show();
//上面四行語句分別顯示四個對象的成員變量值
}
}//ConstructorTest類結束
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -