当前位置: 技术问答>java相关
为什么该父类无法调用其子类.
来源: 互联网 发布时间:2015-03-01
本文导语: 子类:public class ShapeCircle extends Shape { protected double r; public ShapeCircle() { setside(0.0); } public ShapeCircle(double r) { setside(r); } public void setside(double r) { this.r=r; } public double perimeter() { return Math.PI*2*r; } p...
子类:public class ShapeCircle extends Shape
{
protected double r;
public ShapeCircle()
{
setside(0.0);
}
public ShapeCircle(double r)
{
setside(r);
}
public void setside(double r)
{
this.r=r;
}
public double perimeter()
{
return Math.PI*2*r;
}
public String getType()
{
return "Circle";
}
}
父类:
import ShapeCircle;
import Shape;
public class Myshape
{
private static ShapeCircle sc=new ShapeCircle(4);
private static Shape shapej=new ShapeCircle(5);
public static void main(String args[])
{
System.out.println(sc.getType()+","+sc.perimeter());
}
shapej=sc;
System.out.println(shape.getType()+","+shape.perimeter());
}
当编译到shapej=sc总报错.为什么,请高手指教.
{
protected double r;
public ShapeCircle()
{
setside(0.0);
}
public ShapeCircle(double r)
{
setside(r);
}
public void setside(double r)
{
this.r=r;
}
public double perimeter()
{
return Math.PI*2*r;
}
public String getType()
{
return "Circle";
}
}
父类:
import ShapeCircle;
import Shape;
public class Myshape
{
private static ShapeCircle sc=new ShapeCircle(4);
private static Shape shapej=new ShapeCircle(5);
public static void main(String args[])
{
System.out.println(sc.getType()+","+sc.perimeter());
}
shapej=sc;
System.out.println(shape.getType()+","+shape.perimeter());
}
当编译到shapej=sc总报错.为什么,请高手指教.
|
public static void main(String args[])
{
System.out.println(sc.getType()+","+sc.perimeter());
shapej=sc;
System.out.println(shapej.getType()+","+shapej.perimeter());
}
{
System.out.println(sc.getType()+","+sc.perimeter());
shapej=sc;
System.out.println(shapej.getType()+","+shapej.perimeter());
}
|
你的语句不能直接写在类里,如果写的话可写为
static {
shapej=sc;
System.out.println(shape.getType()+","+shape.perimeter());
}
或者写成: karma(无为)的方式
static {
shapej=sc;
System.out.println(shape.getType()+","+shape.perimeter());
}
或者写成: karma(无为)的方式
|
I guess dongjh forgot to put } after
System.out.println(shape.getType()+","+shape.perimeter());
System.out.println(shape.getType()+","+shape.perimeter());