java入門:(建構函式的妙用)實現複數的加減乘除
阿新 • • 發佈:2018-12-11
在複數類中,加減乘除各用兩種方法實現。舉個例子,就比如a = b + c和a += b;
在該複數類中,用到了static修飾符,如此,被修飾的方法可以用類名通過圓點運算子直接呼叫。可以解決沒有物件呼叫的問題。用類名直接呼叫,傳入兩個引數。
減法利用加法實現,除法利用乘法實現。
在除法中,如果除數為0,則會丟擲異常。
編寫了四種構造方法:無參,單參,雙參以及直接傳入一個複數。使用this()的方法呼叫雙參建構函式完成無參和單參構造。
並實現了getter和setter方法,以便取得和注入初值。
過載了toString方法,自定義輸出形式。
可以實現複數的加減乘除。因為有a += b這類方法,使得不光可以實現兩個複數的運算,還可以實現連加,連乘等操作。
具體程式碼如下:
/* * @auther-yc * 2018/9/23 */ package com.mec.complex; public class Complex { //定義兩個全域性變數,分別表示複數的實部和虛部; private double row; private double col; //無參構造 public Complex() { //使用this()方法呼叫雙參構造,使得預設初始值為0.0 + 0.0i; this(0.0, 0.0); } public Complex(double row) { this(row, 0.0); } public Complex(Complex complex) { this(complex.row, complex.col); } public Complex(double row, double col) { this.setRow(row); this.setCol(col); } //實現一系列的getter和setter方法; public double getRow() { return row; } public void setRow(double row) { this.row = row; } public double getCol() { return col; } public void setCol(double col) { this.col = col; } //該方法可取得一個複數的相反數,這樣就可以利用加法來實現減法運算。 public static Complex opposite(Complex complex) { complex.row = - complex.row; complex.col = - complex.col; return complex; } //該方法可取的一個複數的倒數,這樣就可以利用乘法來實現除法運算。 public static Complex reciprocal(Complex complex) { double row = (complex.row) / (complex.row * complex.row + complex.col * complex.col); double col = (-complex.col) / (complex.row * complex.row + complex.col * complex.col); return new Complex(row, col); } //a = b + c;將該方法定義為靜態,可以使用類名直接呼叫,然後輸出結果。不用新定義一個複數去接收其和值; //且該方法實質上是用其中一個引數呼叫單參的加法實現。 public static Complex add(Complex one, Complex another) { return new Complex(one).add(another); } //a += b; public Complex add(Complex one) { this.row += one.row; this.col += one.col; return this; } //a = b - c; public static Complex sub(Complex one, Complex another) { return new Complex(one).add(opposite(another)); } public Complex sub(Complex complex) { return this.add(opposite(complex)); } //a = b * c; public static Complex mul(Complex one, Complex another) { return new Complex(one).mul(another); } //a *= b; public Complex mul(Complex complex) { double row = this.row; this.row = this.row * complex.row - this.col * complex.col; this.col = row * complex.col + this.col * complex.row; return this; } //a /= b;此處丟擲自定義的異常“除零錯”,當傳入的引數作為除數運算是就會報異常! public Complex div(Complex complex) throws DivideZeroException { if((Math.abs(complex.row) < 1e-6) && (Math.abs(complex.col) < 1e-6)) { throw new DivideZeroException("除零錯"); } return new Complex(this.mul(reciprocal(complex))); } //a = b / c; public static Complex div(Complex one, Complex another) throws DivideZeroException { if((Math.abs(another.row) < 1e-6) && (Math.abs(another.col) < 1e-6)) { throw new DivideZeroException("除零錯"); } return new Complex(one).mul(reciprocal(another)); } //過載toString方法,可以按自定義的格式輸出; @Override public String toString() { return "(" + row + ", " + col + ")"; } } /* * 異常類 * @auther-yc * 2018/9/23 */ package com.mec.complex; public class DivideZeroException extends Exception { private static final long serialVersionUID = -917710262683840175L; public DivideZeroException() { } public DivideZeroException(String arg0, Throwable arg1, boolean arg2, boolean arg3) { super(arg0, arg1, arg2, arg3); } public DivideZeroException(String arg0, Throwable arg1) { super(arg0, arg1); } public DivideZeroException(String arg0) { super(arg0); } public DivideZeroException(Throwable arg0) { super(arg0); } }