如何解决由于某种原因,我无法访问从另一个类派生的一个类的方法
我正在尝试在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()”不存在”。我究竟做错了什么?我是Processing的新手。 编辑:将“更新”标记为公共无济于事,也可以重新启动“处理”。
解决方法
首先,您可能将m
声明为Matrix
,而不是RotationMatrix
。但是Matrix
没有Update
方法,因此会出现错误。您需要将m
的类型声明为RotationMatrix
才能解决此问题。
接下来,由于您没有将Update
声明为public
,因此它目前是私有程序包。如果您尝试在声明为RotationMatrix
的程序包之外访问它,则会收到您看到的错误。将方法声明为public
来解决此问题。
(Matrix
中声明的大多数方法也可能如此。)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。