我正在一个项目中,其中几个类必须继承信息。我目前正在尝试让一个子类继承并使用超类的数组。当我尝试访问数组时,子类没有执行任何操作。从文本文件中读取信息并将其放入数组后,我可以返回数组,但是如果不返回该子类,它将不会对其进行任何处理。
超类:
package Objects;
import java.io.*;
import java.util.*;
public abstract class Shape{
public static void main(String[] args) {
String[] shape = new String[12];
int i;
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(
"src/Data.txt"));
String line = reader.readLine();
while (line != null) {
for (i=0; i < 13; i+=2) {
shape[i] = line.substring(0, line.indexOf(" "));
shape[i+1] = line.substring(line.length() - 1, line.length());
line = reader.readLine();
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
catch(NullPointerException e)
{
}
}//end main
}//end shape
子类:
package Objects;
import java.io.*;
import java.util.*;
public abstract class TwoDShape extends Shape{
System.out.println(Arrays.toString(shape));
public double getArea() {
return 0.0;
}
}//end 2D
答案 0 :(得分:1)
main
是double randDouble(double precision, double lowerBound, double upperBound) {
double random;
random = static_cast<double>(((rand()%(static_cast<int>(std::pow(10,precision)*(upperBound - lowerBound) + 1))) + lowerBound*std::pow(10,precision)))/std::pow(10,precision);
return random;
}
内部的局部变量。局部变量不会被继承。您令人困惑的局部变量和实例变量。
答案 1 :(得分:0)
我看到一种违反命名约定和一种语法错误的情况。
根据命名约定,package
的名称应为objects
,而不是Objects
。
语法错误在以下代码中:
public abstract class TwoDShape extends Shape{
System.out.println(Arrays.toString(shape));
public double getArea() {
return 0.0;
}
}//end 2D
您不能在System.out.println(Arrays.toString(shape));
内部直接使用class
。这句话需要放在一些思路上。
除此之外,您确定TwoDShape
是abstract
类吗?
注意:如果您想在子类shape
中访问TwoDShape
,则需要在main
方法外部声明它(例如,在{{ 1}}方法)中的类main
。