java父类shape_为什么该父类无法调用其子类.__ShapeCircle_public_perimeter_getType_shapej__169IT.COM...
子類: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());
}
當(dāng)編譯到shapej=sc總報(bào)錯(cuò).為什么,請(qǐng)高手指教.
|
public?static?void?main(String?args[])
{
System.out.println(sc.getType()+","+sc.perimeter());
shapej=sc;
System.out.println(shapej.getType()+","+shapej.perimeter());
}
|
你的語(yǔ)句不能直接寫在類里,如果寫的話可寫為
static?{
shapej=sc;
System.out.println(shape.getType()+","+shape.perimeter());
}
或者寫成: karma(無(wú)為)的方式
|
I?guess?dongjh?forgot?to?put?}?after
System.out.println(shape.getType()+","+shape.perimeter());
總結(jié)
以上是生活随笔為你收集整理的java父类shape_为什么该父类无法调用其子类.__ShapeCircle_public_perimeter_getType_shapej__169IT.COM...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10 利用安装包 安装 ssh
- 下一篇: cudnn.deterministic