简体   繁体   English

实现接口Java新手

[英]implementing an interface java newbie

I am a newbie in java and I am trying to implement an interface. 我是Java的新手,正在尝试实现接口。

So main class 所以主要课程

public interface Matrix{
    //returns number of rows
    int numRows();
    //returns number of columns
    int numColumns();

    int addRows(...);
    ....
}

Now basically what I am trying to solve is lets say.. I have two matrices matrixa and matrixb of type Matrix . 现在基本上我试图解决的是让说..我有两个矩阵matrixamatrixb类型的Matrix

I want to basically extend the matrix row wise. 我想基本上扩展矩阵行。 So if matrixa had 10 rows and matrixb has 2 rows. 因此,如果matrixa有10行而matrixb有2行。 Then I want to return matrixa+=matrixb 然后我想返回matrixa+=matrixb

(offcourse assuming that number of columns are same.) (假设列数相同,则脱机。)

What is the right way to do this? 这样做的正确方法是什么?

You could add another method to your interface like: 您可以在界面中添加其他方法,例如:

   public interface Matrix{
             ....
           Matrix add( Matrix b );
    }

and check for the necessity to expand rows in the implementation. 并检查是否有必要在实现中扩展行。

Why You can't do exactly what You want? 为什么你不能做你想要的?

Java has no operator overloading. Java没有运算符重载。 You can not use += with your objects. 你不能对你的对象使用+= It is generally believed in java world that operator overloading decreases readablility. 人们普遍认为,在java世界中,运算符重载会降低可读性。

What You can do 你可以做什么

However you can use methods such as addMatrix(Matrix m) . 但是,您可以使用addMatrix(Matrix m)

public interface Matrix{
    int getNumberOfRows();
    int getNumberOfColumns();

    Matrix addMatrix(Matrix m);
}

The easy answer is: you implement an interface by reading this http://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html 简单的答案是:通过阅读http://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html来实现一个接口

I would start with java tutorials since you are a beginner. 因为你是初学者,我会从java教程开始。 You can't instantiate an interface, an interface (typically) only contains method header definitions. 您无法实例化接口,接口(通常)仅包含方法头定义。 The way you use interfaces is you implement them by creating a normal java class that implements code for each of the methods found in the interface. 使用接口的方法是通过创建一个普通的Java类来实现它们,该类为接口中找到的每个方法实现代码。

Btw, I would highly recommend reading tutorials rather than relying on SO for questions like this. 顺便说一下,我强烈建议阅读教程,而不是依靠SO来解决这样的问题。

I'm trying to make sense of the question. 我试图弄清楚这个问题。 I think what you are trying to do is define: 我认为您想要做的是定义:

int addRows(Matrix b);

In that method implementation, you would: 在该方法实现中,您将:

  1. expand your matrix 扩展你的矩阵
  2. copy the b rows into the newly expanded matrix 将b行复制到新扩展的矩阵中
  3. return the new number of rows 返回新的行数

All of this implementation is dependent on how you implement the Matrix interface. 所有这些实现都取决于您实现Matrix接口的方式。

To implement an interface you create a class that implements the interface and all of it's methods. 要实现接口,您需要创建一个实现接口及其所有方法的类。

public class MatrixImpl implements Matrix{

    private List<List<Integer>> elements = new ArrayList<List<Integer>>();
    private int rowSize;

    ...

    /** appends rows from provided matrix to this matrix */
    public Matrix addRows(Matrix b){
        List<List<Integer>> rows;
        if (b == this){
            rows = new ArrayList(b.elements);
        }else {
            rows = b.elements;
        }
        for (List<Integer> row : rows){
           if(row.size() == rowSize){
              elements.add(new ArrayList(row));
           }else{
              // do some error handling
           }
        }
        return this;// you may want to return a clone instead
    }

    ...

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM