cannot find symbol
写有三角类如下代码:class triangle {
double a;
double b;
double c;
public triangle(double a ,double b,double c){
if((a+b>c)&&(a+c>b)&&(b+c>a)&&(a-c<b)&&(a-b<c)&&(b-c<a)){
this.a=a;
this.b=b;
this.c=c;
}
else {
System.out.println("输入边长有错");
}
}
public double GetCircle(){
return (a+b+c);
}
public double GetArea(){
double p;
p=(a+b+c)/2.0;
return (Math.sqrt(p*(p-a)*(p-b)*(p-c)));
}
}
class Tprism extends triangle{
double height;
public Tprism (double a ,double b,double c,double height){
if((a+b>c)&&(a+c>b)&&(b+c>a)&&(a-c<b)&&(a-b<c)&&(b-c<a)){
this.a=a;
this.b=b;
this.c=c;
this.height=height;
}
else {
System.out.println("输入边长有错");
}
}
}
class exp1{
public static void main(String [] args){
triangle r = new triangle(3.0,4.0,5.0);
Tprism c=new Tprism(3.0,4.0,5.0,6.0);
System.out.println("三角形的周长" + r.GetCircle());
System.out.println("三角形的面积"+r.GetArea());
System.out.println("三棱柱的底面积"+c.GetCircle());
System.out.println("三棱柱的底周长"+c.GetArea());
System.out.println("三棱柱的体积"+c.GetArea()*c.height);
}
}
但是编译如下:
--------------------Configuration: question1 - JDK version 1.5.0_02 <Default>--------------------
E:\question1\exp1.java:29: cannot find symbol
symbol : constructor triangle()
location: class triangle
public Tprism (double a ,double b,double c,double height){
^
1 error
Process completed.
求正解,请赐教
----------------解决方案--------------------------------------------------------
class Tprism extends triangle{
double height;
public Tprism (double a ,double b,double c,double height){
super(a,b,c);
if((a+b>c)&&(a+c>b)&&(b+c>a)&&(a-c<b)&&(a-b<c)&&(b-c<a)){
this.a=a;
this.b=b;
this.c=c;
this.height=height;
}
else {
System.out.println("输入边长有错");
}
}
}
public class exp1{
public static void main(String [] args){
triangle r = new triangle(3.0,4.0,5.0);
Tprism c=new Tprism(3.0,4.0,5.0,6.0);
System.out.println("三角形的周长" + r.GetCircle());
System.out.println("三角形的面积"+r.GetArea());
System.out.println("三棱柱的底面积"+c.GetCircle());
System.out.println("三棱柱的底周长"+c.GetArea());
System.out.println("三棱柱的体积"+c.GetArea()*c.height);
}
}
修改以上几点就行了。。。
----------------解决方案--------------------------------------------------------
要用super调用父类的变量
----------------解决方案--------------------------------------------------------
回复 2楼 legend817
谢谢 正解不过 还要请教一下 下面那个代码为何就不存在上述问题呢?请赐教
class rectangle{
double width;
double length;
public rectangle(){
width = 10;
length = 10;
}
public rectangle(int width, int length){
this.width = width;
this.length = length;
}
public rectangle(double width, double length){
this.width = width;
this.length = length;
}
public double getCircle(){
return 2*(width + length);
}
public double getArea(){
return width * length;
}
}
class cube extends rectangle{
double height;
public cube(){
width = 10;
length = 10;
height = 10;
}
public cube(double width1, double length1, double height){
width = width1;
length = length1;
this.height = height;
}
public double getVol(){
return width * length * height;
}
public double getArea(){
return 2*(width * length + width * height + length * height);
}
}
class example{
public static void main(String [] args){
rectangle r = new rectangle(20, 20);
cube c = new cube(11, 22, 33);
System.out.println("矩形的面积" + r.getArea());
System.out.println("长方体的体积" + c.getVol());
System.out.println("长方体的面积" + c.getArea());
}
}
----------------解决方案--------------------------------------------------------
回复 3楼 爱心流沙
谢谢 ----------------解决方案--------------------------------------------------------
class example 改成public class example 就行了。。。。
----------------解决方案--------------------------------------------------------
子类构造函数要调用父类构造函数
上边的子类构造函数默认调用的是super()
也就是
public rectangle(){
width = 10;
length = 10;
}
----------------解决方案--------------------------------------------------------
回复 7楼 legend817
又学到了些东西哦,谢谢赐教 ----------------解决方案--------------------------------------------------------
回复 8楼 Annie5
呵呵,不客气,共同学习。。。。。 ----------------解决方案--------------------------------------------------------
class triangle {
double a;
double b;
double c;
public triangle(double a ,double b,double c){
if((a+b>c)&&(a+c>b)&&(b+c>a)&&(a-c<b)&&(a-b<c)&&(b-c<a)){
this.a=a;
this.b=b;
this.c=c;
}
else {
System.out.println("输入边长有错");
}
}
public double GetCircle(){
return (a+b+c);
}
public double GetArea(){
double p;
p=(a+b+c)/2.0;
return (Math.sqrt(p*(p-a)*(p-b)*(p-c)));
}
}
class Tprism extends triangle{
double height;
public Tprism (double a ,double b,double c,double height){
super(a,b,c);
if((a+b>c)&&(a+c>b)&&(b+c>a)&&(a-c<b)&&(a-b<c)&&(b-c<a)){
this.a=a;
this.b=b;
this.c=c;
this.height=height;
}
else {
System.out.println("输入边长有错");
}
}
public double volume(){
return (super.GetArea()*height);
}
}
class pramid extends rectangle{
double height;
public pramid(double a, double b, double c,double height){
super(a,b,c);
if((a+b>c)&&(a+c>b)&&(b+c>a)&&(a-c<b)&&(a-b<c)&&(b-c<a)){
this.a=a;
this.b=b;
this.c=c;
this.height=height;
}
else {
System.out.println("输入边长有错");
}
}
public double volume(){
return (super.GetArea()*height*(1/3));
}
}
class exp1{
public static void main(String [] args){
triangle r = new triangle(3.0,4.0,5.0);
Tprism c=new Tprism(3.0,4.0,5.0,6.0);
pramid d=new pramid(3.0,4.0,5.0,6.0);
System.out.println("三角形的周长" + r.GetCircle());
System.out.println("三角形的面积"+r.GetArea());
System.out.println("三棱柱的底面积"+c.GetCircle());
System.out.println("三棱柱的底周长"+c.GetArea());
System.out.println("三棱柱的体积"+c.volume());
System.out.println("三棱锥的底面积"+d.GetArea());
System.out.println("三棱锥的体积"+d.volume());
}
}
--------------------Configuration: hh - JDK version 1.5.0_02 <Default>--------------------
G:\java\hh\exp1.java:50: cannot find symbol
symbol: class rectangle
class pramid extends rectangle{
^
G:\java\hh\exp1.java:57: cannot find symbol
symbol : variable a
location: class pramid
this.a=a;
^
G:\java\hh\exp1.java:58: cannot find symbol
symbol : variable b
location: class pramid
this.b=b;
^
G:\java\hh\exp1.java:59: cannot find symbol
symbol : variable c
location: class pramid
this.c=c;
^
G:\java\hh\exp1.java:68: cannot find symbol
symbol : variable super
location: class pramid
return (super.GetArea()*height*(1/3));
^
G:\java\hh\exp1.java:85: cannot find symbol
symbol : method GetArea()
location: class pramid
System.out.println("三棱锥的底面积"+d.GetArea());
^
6 errors
Process completed.
那这个问题又出在哪呢?菜鸟级别,请大家指教
----------------解决方案--------------------------------------------------------