当前位置: 首页 > news >正文

广东建设工程造价管理协会网站百度一下官方网址

广东建设工程造价管理协会网站,百度一下官方网址,建设网站要编程bu,专业做网站优化排名文章目录day31 整数矩阵及其运算面向对象思想java异常处理java中的getter和setter方法代码day31 整数矩阵及其运算 面向对象思想 结合之前day7和day8面向过程开发,只关注了矩阵加法和矩阵乘法的功能。而day31是面向对象开发,一个矩阵类,在这…

文章目录

  • day31 整数矩阵及其运算
    • 面向对象思想
    • java异常处理
    • java中的getter和setter方法
    • 代码

day31 整数矩阵及其运算

面向对象思想

结合之前day7和day8面向过程开发,只关注了矩阵加法和矩阵乘法的功能。而day31是面向对象开发,一个矩阵类,在这个类对象中包含有矩阵的加法,乘法,获取数据等功能(如add,multiply方法)。同时通过get,set方法来让用户通过方法获取类相关数据(getData,getRows,getColumns,setValue等),而非直接获取数据。在IntMatrix类,方法名为add的有两个,但这两个方法的区别在于传参以及返回不同,这体现了方法重载。
面向对象的三大特点(定义的描述来自百度):

  • 封装:
    (1)定义:将数据和对数据的操作封装在一个对象内部,对外部隐藏对象的实现细节,保证了程序的安全性和可靠性。
    (2)IntMatrix类就体现了封装性,将数据和相关的操作封装在对象内部,我们对外只提供相应饿方法,如我们调用矩阵相乘就可以直接调用multiply方法即可
  • 继承
    (1)定义:通过定义父类和子类,子类可以继承父类的属性和方法,减少代码重复,提高代码的可维护性
  • 多态
    (1)定义:同一个方法可以根据不同的对象调用不同的实现方式,从而提高代码的灵活性和可扩展性。多态一般是通过继承或接口来实现的

java异常处理

在之前写哈夫曼树时已经涉及异常处理了,java的异常处理方法

  • try-catch-finally: 将可能要出现异常的代码放入try中,catch 捕获 try 中的异常,并处理,不管有没有异常,finally中的代码都会执行。(finally不是必须)

  • throw: 一般是语句抛出一个异常, 一般是手动抛出,并且可以抛出更为明确的异常

  • throws:一般是方法抛出一个异常,在方法后面声明异常(表示该方法可能会产生异常)

java中的getter和setter方法

一般在创建java实体类时,会把类相关属性设置为私有private(这是从安全角度去考虑),想要获取或设置这些私有属性可以通过方法去获取或设置,即getXXX,setXXX,而不是直接去操作这一个变量。这也体现了java的一大特点:封装性。
访问权限修饰符(private,procted,public,default)不同的访问权限,访问的范围不一样(从网上找了一个这样的图)
在这里插入图片描述
在项目中使用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);}
}

在这里插入图片描述

http://www.dinnco.com/news/23092.html

相关文章:

  • 广州互联网大厂公司有哪些阜阳seo
  • 西安网站建设云阔百度地图关键词排名优化
  • wordpress拖拽式主题网站的优化
  • 怎么把网站改为正在建设中最佳磁力引擎吧
  • 桂林北站到阳朔怎么坐车免费海报模板网站
  • 做门户网站需要什么条件seo关键词优化案例
  • 吉林省住房建设厅网站seo排名优化点击软件有哪些
  • 上海网站建设的传统营销与网络营销的整合方法
  • 郑州高端品牌网站建设长沙关键词优化公司电话
  • 北京网站排名优化线上宣传方式
  • 2020十大热点事件快速seo关键词优化方案
  • 网站的空间怎么查广告竞价排名
  • 做百度网站那家好网站免费建站app
  • 群辉做网站服务器配置龙岗网站制作
  • 建立网站时服务器的基本配置有哪些网络营销是干什么的
  • 天河网站建设技术怎么做竞价托管
  • 网站建设waocc直销的八大课程
  • 石材网站建设做网络推广费用
  • 网络推广网站百度付费推广的费用
  • 网站建设 宣传单互联网推广渠道
  • 公安局网站不备案南京网络营销服务
  • 怎么做英文的网站免费建站系统哪个好用吗
  • 长沙优化网站技巧企业网站的基本功能
  • 海南最新疫情今天新增厦门网站综合优化贵吗
  • 做网站什么语言最好百度推广需要什么条件
  • 仿 手机 网站模板html源码下载如何推广app更高效
  • 开发网站用那个平台网站优化公司哪家效果好
  • 学做川菜最好的网站成都最新疫情
  • 北京网站建设分析论文seo推广费用需要多少
  • 免费网站mvapp投放渠道有哪些