java - 了解构造函数和 "this"

标签 java constructor this

我正在学习构造函数,我对它们的大部分了解,但我一定理解得不够。我也没有完全理解this。但下面的代码应该使用这些构造函数:

default constructor, which will be used to fill the matrix with random doubles

constructor which takes a File object, which points to a file containing a matrix,

constructor which takes a string, which contains the name of the file

constructor which takes a value of type Matrix, and makes a copy of it

constructor which takes a 2D array, and copies its values

还有更多,以及静态乘法方法。我应该使用 main 中找到的命令。但我不太明白使用 String 作为唯一参数将如何执行它所告知的构造函数,以及其他构造函数(例如用随机 double 值填充数组的默认构造函数)。我认为我应该在代码中更多地使用 this,但我不太确定。

我主要只需要能够设置一个矩阵,用随机 double 填充 m1 矩阵,用 m2 矩阵再次执行此操作,然后使用静态乘法方法将它们相乘,然后输出结果矩阵。谢谢。

(请注意,我使用的是 3 x 3 矩阵,最初我应该将矩阵的大小设置为文本文件中找到的大小,但我也可以指定我想要的大小,这我是的。对困惑的代码感到抱歉。当我试图弄清楚这些东西时,它变得困惑了,我害怕进一步改变它。)

public class Matrix {

        double A[][] = new double[3][3]


        // Matrix file name
        public Matrix(String name) {
            this(new File(name));
        }

        // Matrix random fill
        public Matrix() {
            for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                this.A[i][j] = (min + Math.random() * (max - min));
        }

        // Matrix copy
        public Matrix(double[][] A) {
            private double[][] arrcopy = new double[3][3];
            private double[][] array = new double[3][3];
            array = A;

            for(int i = 0; i < 3; ++i){
                for(int j = 0; j < array[i].length; ++j) {
                    arrcopy[i][j] = array[i][j];
                }
            }
        }

        // Set array from text file
        public Matrix(File a) {

            File f = a;
            Scanner inputStreamOne = null;

            try{
                inputStreamOne = new Scanner(f);
            }

            catch(FileNotFoundException e){
                System.out.printf("Error\n");
            }

            double arrayOne[][] = new double[3][3];

            while(inputStreamOne.hasNextInt()) {
                for(int i = 0; i < 3; ++i){
                    for(int j = 0; j < arrayOne[i].length; ++j){
                    arrayOne[i][j] = inputStreamOne.nextInt();
                    }
                }
            inputStreamOne.close();
            }
        }

        // Gets array in file from string name
        public Matrix(String a) {

            String inputOne = a;
            Scanner inputStreamOne = null;

            try{
                inputStreamOne = new Scanner(new File(inputOne));
            }

            catch(FileNotFoundException e){
                System.out.printf("Error\n");
            }



            while(inputStreamOne.hasNextInt()){
                for(int i = 0; i < size; ++i){
                    for(int j = 0; j < arrayOne[i].length; ++j){
                    arrayOne[i][j] = inputStreamOne.nextInt();
                    }
                }
            inputStreamOne.close();
            }

        }

        public static multiply

        public static void main(String args[]) {
            Matrix m = new Matrix("matrix1.txt");
            Matrix m2 = new Matrix("matrix2.txt");
            Matrix r = Matrix.multiply(m, m2);
            r.output(...);
        }
    }

最佳答案

在构造函数或方法中,this 指的是当前对象。在构造函数中,如果您只使用一个裸的 this (也就是说 this 后面没有 .method ),那么它引用另一个具有不同类型签名的同一对象的构造函数。

构造函数与 Java 中的任何函数一样,可以重载。也就是说,由于 Java 的类型系统,您可以拥有多个同名的函数,只要它们具有不同的类型签名即可。

class Foo {

    public Foo ( String arg ) {
        this(arg, "World");
    }

    public Foo ( String arg1, String arg2 ) {
        // do stuff with arg1 and arg2
    }

 }

在上面的示例中,一个构造函数采用单个字符串,另一个构造函数采用两个字符串。 Java 不会神奇地知道如何处理传递到不同构造函数的数据。您所要做的就是在每个方法中编写代码,以便生成的对象相同。 (从技术上讲,您不必这样做,但这是一种很好的形式(通常重载构造函数用于设置默认值或方便其他程序员使用))

关于java - 了解构造函数和 "this",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9543945/

相关文章:

java - 如何重写Java中构造函数中使用的方法?

c++ - 使用派生类的数据成员初始化基类

java - Java 构造函数内的声明

c++ - 用于提高回调注册可读性的宏

javascript - 使用此压缩多次点击和提交功能

java - 如何使用poi合并两个excel工作簿?

java - Jackson : json schema references other schemas, 可以禁用吗?

java - 修复记录: Cell information from worksheet created

java - Java 可以通过用户输入进行 self 修改吗?

jQuery 插件创作 : explanation for "this" keyword?