泛型 Java语言程序设计 第十九章 (示例代码3)

程序小白,希望和大家多交流,共同学习

package matrix;

public abstract class GenericMatrix<E extends Number>
{
    //非泛型类方法。不是静态方法,那么参数可以使用泛型类型
    protected abstract E add(E o1, E o2);
    protected abstract E multiply(E o1, E o2);
    protected abstract E zero();

    public E[][] addMatrix(E[][] matrix1, E[][] matrix2)
    {
        if ((matrix1.length != matrix2.length) ||
            (matrix1[0].length != matrix2[0].length))
        {
            throw new RuntimeException("The matrix do not have the same size");
        }

        E[][] result = 
            (E[][])new Number[matrix1.length][matrix1[0].length];

        for (int i = 0; i < matrix1.length; i++)
        {
            for (int j = 0; j < matrix1[0].length; j++)
            {
                result[i][j] = add(matrix1[i][j], matrix2[i][j]);
            }
        }

        return result;
    }

    public E[][] multiplyMatrix(E[][] matrix1, E[][] matrix2)
    {
        if (matrix1[0].length != matrix2.length)
        {
            throw new RuntimeException("The matrix do not have compatible size");
        }

        E[][] result = (E[][])new Number[matrix1.length][matrix2[0].length];

        for (int i = 0; i < result.length; i++)
        {
            for (int j = 0; j < result[0].length; j++)
            {
                result[i][j] = zero();

                for (int k = 0; k < matrix1[0].length; k++)
                {
                    result[i][j] = add(result[i][j], multiply(matrix1[i][k], matrix2[k][j]));
                }
            }
        }
        return result;
    }

    public static void printResult(Number[][] m1, Number[][] m2, Number[][] m3, char op)
    {
        for (int i = 0; i < m1.length; i++)
        {
            for (int j = 0; j < m1[0].length; j++)
            {
                System.out.print(" " + m1[i][j]);
            }

            if (i == m1.length / 2)
            {
                System.out.print(" " + op + " ");
            }
            else
                System.out.print("   ");

            for (int j = 0; j < m2.length; j++)
            {
                System.out.print(" " + m2[i][j]);
            }

            if (i == m1.length / 2)
            {
                System.out.print(" = ");
            }
            else
                System.out.print("   ");

            for (int j = 0; j < m3[0].length; j++)
            {
                System.out.print(m3[i][j] + " ");
            }

            System.out.println();
        }
    }
}
package matrix;

public class IntegerMatrix extends GenericMatrix<Integer>
{
    @Override
    protected Integer add(Integer o1, Integer o2)
    {
        return o1 + o2;
    }

    @Override
    protected Integer multiply(Integer o1, Integer o2)
    {
        return o1 * o2;
    }

    @Override
    protected Integer zero()
    {
        return 0;
    }
}
package matrix;

public class RationalMatrix extends GenericMatrix<Rational>
{
    @Override
    protected Rational add(Rational r1, Rational r2)
    {
        return r1.add(r2);
    }

    @Override
    protected Rational multiply(Rational r1, Rational r2)
    {
        return r1.multiply(r2);
    }

    @Override
    protected Rational zero()
    {
        return new Rational();
    }
}
//测试RationalMatrix
package matrix;

public class TestRationalMatrix
{
    public static void main(String[] args)
    {
        Rational[][] m1 = new Rational[3][3];
        Rational[][] m2 = new Rational[3][3];

        for (int i = 0; i < m1.length; i++)
        {
            for (int j = 0; j < m1[0].length; j++)
            {
                m1[i][j] = new Rational(i + 1, j + 5);
                m2[i][j] = new Rational(i + 1, j + 6);
            }
        }

        RationalMatrix rationalMatrix = new RationalMatrix();

        System.out.println("\nm1 + m2 is ");
        GenericMatrix.printResult(m1, m2, rationalMatrix.addMatrix(m1, m2), '+');

        System.out.println("\nm1 * m2 is ");
        GenericMatrix.printResult(m1, m2, rationalMatrix.multiplyMatrix(m1, m2), '*');
    }
}

猜你喜欢

转载自blog.csdn.net/cheng_cuo_tuo/article/details/79996986