貴州省網(wǎng)站建設選哪家廣州seo網(wǎng)站服務公司
文章目錄
- day31 整數(shù)矩陣及其運算
- 面向?qū)ο笏枷?/li>
- java異常處理
- java中的getter和setter方法
- 代碼
day31 整數(shù)矩陣及其運算
面向?qū)ο笏枷?/h2>
結(jié)合之前day7和day8面向過程開發(fā),只關注了矩陣加法和矩陣乘法的功能。而day31是面向?qū)ο箝_發(fā),一個矩陣類,在這個類對象中包含有矩陣的加法,乘法,獲取數(shù)據(jù)等功能(如add,multiply方法)。同時通過get,set方法來讓用戶通過方法獲取類相關數(shù)據(jù)(getData,getRows,getColumns,setValue等),而非直接獲取數(shù)據(jù)。在IntMatrix類,方法名為add的有兩個,但這兩個方法的區(qū)別在于傳參以及返回不同,這體現(xiàn)了方法重載。
面向?qū)ο蟮娜筇攸c(定義的描述來自百度):
- 封裝:
(1)定義:將數(shù)據(jù)和對數(shù)據(jù)的操作封裝在一個對象內(nèi)部,對外部隱藏對象的實現(xiàn)細節(jié),保證了程序的安全性和可靠性。
(2)IntMatrix類就體現(xiàn)了封裝性,將數(shù)據(jù)和相關的操作封裝在對象內(nèi)部,我們對外只提供相應餓方法,如我們調(diào)用矩陣相乘就可以直接調(diào)用multiply方法即可 - 繼承
(1)定義:通過定義父類和子類,子類可以繼承父類的屬性和方法,減少代碼重復,提高代碼的可維護性 - 多態(tài)
(1)定義:同一個方法可以根據(jù)不同的對象調(diào)用不同的實現(xiàn)方式,從而提高代碼的靈活性和可擴展性。多態(tài)一般是通過繼承或接口來實現(xiàn)的
java異常處理
在之前寫哈夫曼樹時已經(jīng)涉及異常處理了,java的異常處理方法
-
try-catch-finally: 將可能要出現(xiàn)異常的代碼放入try中,catch 捕獲 try 中的異常,并處理,不管有沒有異常,finally中的代碼都會執(zhí)行。(finally不是必須)
-
throw: 一般是語句拋出一個異常, 一般是手動拋出,并且可以拋出更為明確的異常
-
throws:一般是方法拋出一個異常,在方法后面聲明異常(表示該方法可能會產(chǎn)生異常)
java中的getter和setter方法
一般在創(chuàng)建java實體類時,會把類相關屬性設置為私有private(這是從安全角度去考慮),想要獲取或設置這些私有屬性可以通過方法去獲取或設置,即getXXX,setXXX,而不是直接去操作這一個變量。這也體現(xiàn)了java的一大特點:封裝性。
訪問權(quán)限修飾符(private,procted,public,default)不同的訪問權(quán)限,訪問的范圍不一樣(從網(wǎng)上找了一個這樣的圖)
在項目中使用lombok可以減少寫getter/setter/toString等方法的編寫
代碼
package matrix;
import java.util.Arrays;
public class IntMatrix {int[][] data;/*** The first constructor.* @param paraRows The number of rows* @param paraColumns The number of columns*/public IntMatrix(int paraRows, int paraColumns){data = new int[paraRows][paraColumns];}/*** The second constructor. Construct a copy of the given matrix.* @param paraMatrix The given matrix.*/public IntMatrix(int[][] paraMatrix){data = new int[paraMatrix.length][paraMatrix[0].length];for (int i = 0; i < data.length; i++) {for (int j = 0; j < data[0].length; j++) {data[i][j] = paraMatrix[i][j];}}}/*** The third constructor. Construct a copy of the given matrix.* @param paraMatrix The given matrix.*/public IntMatrix(IntMatrix paraMatrix) {this(paraMatrix.getData());}/*** Get identity matrix. The values at the diagonal are all 1* @param paraRows* @return*/public static IntMatrix getIdentityMatrix(int paraRows) {IntMatrix resultMatrix = new IntMatrix(paraRows, paraRows);for (int i = 0; i < paraRows; i++) {// According to access control, resultMatrix.data can be visitedresultMatrix.data[i][i] = 1;}return resultMatrix;}/*** Overrides the method claimed in Object, the superclass of any class.* @return*/@Overridepublic String toString() {return Arrays.deepToString(data);}/*** Get my data. Warning, the reference to the data instead of a copy of the data is returned.* @return*/public int[][] getData() {return data;}public int getRows() {return data.length;}public int getColumns() {return data[0].length;}/*** Set one the value of one element.* @param paraRow The row of the element.* @param paraColumn The column of the element.* @param paraValue The new value.*/public void setValue(int paraRow, int paraColumn, int paraValue){data[paraRow][paraColumn] = paraValue;}/*** Get the value of one element.* @param paraRow The row of the element.* @param paraColumn The column of the element.* @return*/public int getValue(int paraRow, int paraColumn) {return data[paraRow][paraColumn];}/*** Add another matrix to me.* @param paraMatrix The other matrix.* @throws Exception*/public void add(IntMatrix paraMatrix) throws Exception {// Step 1. Get the data of the given matrix.int[][] tempData = paraMatrix.getData();// Step 2. Size check.if (data.length != tempData.length) {throw new Exception("Cannot add matrices. Rows not match: " + data.length + " vs. "+ tempData.length + ".");}if (data[0].length != tempData[0].length) {throw new Exception("Cannot add matrices. Rows not match: " + data[0].length + " vs. "+ tempData[0].length + ".");}// Step 3. Add to me.for (int i = 0; i < data.length; i++) {for (int j = 0; j < data[0].length; j++) {data[i][j] += tempData[i][j];}}}/*** Add two existing matrices.* @param paraMatrix1 The first matrix.* @param paraMatrix2 The second matrix.* @return A new matrix.* @throws Exception*/public static IntMatrix add(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {// Step 1. Clone the first matrix.IntMatrix resultMatrix = new IntMatrix(paraMatrix1);// Step 2. Add the second one.resultMatrix.add(paraMatrix2);return resultMatrix;}/*** Multiply two existing matrices.* @param paraMatrix1 The first matrix.* @param paraMatrix2 The second matrix.* @return A new matrix.* @throws Exception*/public static IntMatrix multiply(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {// Step 1. Check size.int[][] tempData1 = paraMatrix1.getData();int[][] tempData2 = paraMatrix2.getData();if (tempData1[0].length != tempData2.length) {throw new Exception("Cannot multiply matrices: " + tempData1[0].length + " vs. "+ tempData2.length + ".");}// Step 2. Allocate space.int[][] resultData = new int[tempData1.length][tempData2[0].length];// Step 3. Multiply.for (int i = 0; i < tempData1.length; i++) {for (int j = 0; j < tempData2[0].length; j++) {for (int k = 0; k < tempData1[0].length; k++) {resultData[i][j] += tempData1[i][k] * tempData2[k][j];}}}// Step 4. Construct the matrix object.IntMatrix resultMatrix = new IntMatrix(resultData);return resultMatrix;}public static void main(String args[]) {IntMatrix tempMatrix1 = new IntMatrix(3, 3);tempMatrix1.setValue(0, 1, 1);tempMatrix1.setValue(1, 0, 1);tempMatrix1.setValue(1, 2, 1);tempMatrix1.setValue(2, 1, 1);System.out.println("The original matrix is: " + tempMatrix1);IntMatrix tempMatrix2 = null;try {tempMatrix2 = IntMatrix.multiply(tempMatrix1, tempMatrix1);} catch (Exception ee) {System.out.println(ee);}System.out.println("The square matrix is: " + tempMatrix2);IntMatrix tempMatrix3 = new IntMatrix(tempMatrix2);try {tempMatrix3.add(tempMatrix1);} catch (Exception ee) {System.out.println(ee);}System.out.println("The connectivity matrix is: " + tempMatrix3);}
}