【问题标题】:For some reason I cannot access a method of a class, derived from another class由于某种原因,我无法访问从另一个类派生的类的方法
【发布时间】:2020-08-28 08:36:23
【问题描述】:

我正在尝试在 Processing 中创建自定义 Matrix 类以处理任意数量的维度。类 RotationMatrix 派生自它。但我无法访问 RotationMatrix 的方法,就好像它们不存在一样。 我的矩阵类:

class Matrix{
  float[][] list;
  int[] dims;
  public Matrix(int cols, int rows){
    list = new float[cols][rows];
    dims = new int[] {cols, rows};
    
  }
  void Identity(){
    list = new float[dims[0]][dims[1]];
    for(int v=0;v<min(dims[0], dims[1]); v++){
      list[v][v] = 1;
    }
  }
  float[] mult(float[] vector){
    if(vector.length!=dims[0]) return vector;
    float[] result = new float[dims[0]];
    for (int i = 0; i<result.length;i++){
      float d = dot(vector, list[i]);
      result[i] = d;
    }
    return result;    
  }
  float dot(float[] v1, float[] v2){
    float result=0;
    for(int i = 0; i<v1.length;i++){
      result+=v1[i]*v2[i];
    }
    return result;
  }
  String toString(){
    String result="{";
    for(int y = 0; y<dims[1]; y++){
      if(y>0)result+=" ";
      result+="(";
      for(int x=0; x<dims[0];x++){
        result+=list[x][y];
        if(x+1<dims[0]) result+=",  ";
      }
      result+=")";
      if(y+1<dims[1]) result+=",\n\n"; 
    }
    result+="}";
    return result;
  }
}


class RotationMatrix extends Matrix {
  public float angle, scale;
  int ax1, ax2;
  int dim;
  public RotationMatrix(int dim){
    super(dim, dim);
    ax1=0;
    ax2=1;
    this.dim=dim;

  }
  
  public RotationMatrix(int dim, int a1, int a2){
    super(dim, dim);
    ax1=min(a1, a2);
    ax2=max(a1, a2);
    this.dim=dim;
    
  }

 
  void Update(){
    for(int y = 0; y<dim;y++){
      for(int x = 0; x<dim;x++){
        if(x==y) {
          
          if(x==ax1||y==ax2) {
            list[x][y] = cos(angle)*scale;
          }
          else {
            list[x][y]=1;
          }
        }
        else{
          if(x==ax2&&y==ax1){
            list[x][y]=-sin(angle)*scale;
          }
          if(x==ax1&&y==ax2) {
            list[x][y] = sin(angle)*scale;
          }
        }
      }
    }
  }
}

现在使用旋转矩阵:

m=new RotationMatrix(3);
m.Update();

并且此 Update 方法调用会引发错误“函数“Update()”不存在”。我究竟做错了什么?我对处理比较陌生。 编辑:将“更新”标记为公开并没有帮助,重新启动处理。

【问题讨论】:

  • m 不应声明为 Matrix(我猜是),但应声明为 RotationMatrixMatrix 没有 Update
  • 如果问题“已解决”,那么您应该接受解决问题的答案,编辑您的问题以包含“已解决”。
  • @Mikhail Dolgopolov 值得了解的是 Processing 提供了 PMatrix2DPMatrix3D 用于坐标转换,与 PVector 结合使用

标签: java processing


【解决方案1】:

首先,您可能将m 声明为Matrix 而不是RotationMatrix。但是Matrix 没有Update 方法,因此出现错误。您需要将m 的类型声明为RotationMatrix 来解决此问题。

接下来,由于您没有将 Update 声明为 public,因此它目前是包私有的。如果您尝试在声明 RotationMatrix 的包之外访问它,您将收到您所看到的错误。将方法声明为 public 以解决此问题。

Matrix 中声明的大多数方法可能也是如此。)

【讨论】:

  • 我已经清楚地写了,我如何声明 m: m=new RotationMatrix(3); m.Update();
  • @MikhailDolgopolov 在 Java 中,声明需要在变量名的左侧有一个 type;您发布的不是声明,而是分配。您使用哪种处理模式允许声明没有类型的变量?你在 REPL 中输入了吗?
  • 谢谢!我忘了改变左边的类型,你是对的。问题解决了。
猜你喜欢
  • 2017-11-28
  • 2014-04-16
  • 2014-06-22
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多