java继承矩形例子代码 java中继承的用法

请编写 Java程序,设计一个“形状”基类,经由“形状”继承出3个子类分别是“圆形”、 “矩形”和“三角形就在这直接写了 。。
public class Shape{
//...这里可以定义父类的属性变量,例如:
int length;
int width;
}
public class Round extends Shape{// 圆形
}
public class Rectangle extends Shape{//矩形
}
public class Triangle extends Shape{//三角形
}
JAVA声明圆柱体类和圆锥体类,继承矩形类rectangle并实现volume借口,计算表面积和体积和圆锥体类
public class Du {
public static void main(String[] args) {
}
}
class Rectangle{
protected double length;
protected double width;
public Rectangle(double length, double width){
this.length = length;
this.width = width;
}
}
interface Volume{
public double volume();
}
class YuanZhu extends Rectangle implements Volume{//圆柱体类
private double r;
public YuanZhu(double length, double width) {
super(length, width);
this.r = length / 2 / Math.PI;
}
public double volume() {
return Math.PI * Math.pow(r, 2) * width;
}
}
class YuanZhui extends Rectangle implements Volume{//圆锥体类
private double r;
public YuanZhui(double length, double width) {
super(length, width);
this.r = length /2 / Math.PI;
}
public double volume() {
return Math.PI * Math.pow(r, 2) * width / 3;
}
}
简单的java 编程题 关于继承package javaapplication4;
public class Rect {
protected int length;/////这个地方不能变成私有属性 , 因为后面继承的类也需要继承它 。
protectedint width;
public Rect(int length,int width)
{
this.length=length;
this.width=width;
}
public void setLength(int length)
{this.length=length;br}
public void setWidth(int width)
{this.width=width;br}
public int getLength()
{return length;br}
public int getWidth()
{return width;br}
public int getArea()
{return length*width;br}
public int getCycle()
{return (length+width)*2;br}}
////////////////////////////////////////////////////////////////////////////////////////////////////////
package javaapplication4;
public class PlainRect extends Rect
{///此类并未继承父类的长宽属性,所以父类的设计是不合理的 。应将其属性改为protected.
protected int startX,startY;
public PlainRect(int length,int width,int startx,int starty)
{
super(length,width);
startX=startx;
startY=starty;
}
public PlainRect()
{
super(0,0);
startX=0;
startY=0;
}
【java继承矩形例子代码 java中继承的用法】public boolean isInside(double x,double y)
{
boolean b=false;
if(xstartXxstartX+length)
if(ystartYystartY+width)
b=true;
return b;}}
////////////////////////////////////////////////////////////////////////////////////////////////////////
package javaapplication4;
public class Main {
public static void main(String[] args) {
PlainRect Pr1=new PlainRect(20,10,10,10);
System.out.println("面积为:"+Pr1.getArea());
System.out.println("周长为:"+Pr1.getCycle());
System.out.println("点(25.5,13)"+(Pr1.isInside(25.5, 13)?"在":"不在")+"此方形内");
} }
关于java继承矩形例子代码和java中继承的用法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读