Java设计:将下面的抽象类方法改为接口,然后基于接口派生出若干子类,以分别计算三角形、长方形、 20
和椭圆的面积。小弟对接口不是很熟悉,用抽象类的方法实现了三个形状的计算,求高手将其修改成为接口实现同样的功能。/*文件1*/packagepackageChapter4;...
和椭圆的面积。小弟对接口不是很熟悉,用抽象类的方法实现了三个形状的计算,求高手将其修改成为接口实现同样的功能。
/*文件1*/
package packageChapter4;
public abstract class PlaneGraphics {
private String shape;
public PlaneGraphics(String shape){
this.shape=shape;
}
public abstract double area();
public void print(){
System.out.println(this.shape+"面积为"+this.area());
}
}
/*文件2*/
package packageChapter4;
public class PlaneGraphics_ex {
public static void main(String[] args) {
PlaneGraphics g=new Rectangle(10,20);
g.print();
g=new Ellipse(10,20);
g.print();
g=new Triangle(10,20);
g.print();
}
}
/*文件3—三角形计算*/
package packageChapter4;
public class Triangle extends PlaneGraphics {
protected double height;
protected double bottom;
protected int p=2;
public Triangle(double height,double bottom){
super("三角形");
this.height=height;
this.bottom=bottom;
}
public double area(){
return height*bottom/p;
}
}
/*文件4——长方形*/
package packageChapter4;
public class Rectangle extends PlaneGraphics {
protected double length;
protected double width;
public Rectangle(double length,double width){
super("长方形");
this.length=length;
this.width=width;
}
public double area(){
return width*length;
}
}
/*文件5——椭圆形*/
package packageChapter4;
public class Ellipse extends PlaneGraphics {
protected double radius_a;
protected double radius_b;
Ellipse(double radius_a,double radius_b){
super("椭圆");
this.radius_a=radius_a;
this.radius_b=radius_b;
}
public double area(){
return Math.PI*radius_a*radius_b;
}
}
额,自己做出来了
贴一个长方形的代码,剩下的都一样修改一下就行
先定义一个接口其中有2个抽象方法,area()和 print(),其中print为输出。字数超出限
package package1接口类方法;
public class Rectangle implements Main {
protected double length;
protected double width;
protected String shape="长方形";
public Rectangle(double length,double width){
this.length=length;
this.width=width;
}
public double area(){
return width*length;
}
public void print(){
System.out.println(this.shape+ "面积为: "+this.area() );
}
public static void main(String[] args){
Rectangle R=new Rectangle(10,20);
R.print();
}
} 展开
/*文件1*/
package packageChapter4;
public abstract class PlaneGraphics {
private String shape;
public PlaneGraphics(String shape){
this.shape=shape;
}
public abstract double area();
public void print(){
System.out.println(this.shape+"面积为"+this.area());
}
}
/*文件2*/
package packageChapter4;
public class PlaneGraphics_ex {
public static void main(String[] args) {
PlaneGraphics g=new Rectangle(10,20);
g.print();
g=new Ellipse(10,20);
g.print();
g=new Triangle(10,20);
g.print();
}
}
/*文件3—三角形计算*/
package packageChapter4;
public class Triangle extends PlaneGraphics {
protected double height;
protected double bottom;
protected int p=2;
public Triangle(double height,double bottom){
super("三角形");
this.height=height;
this.bottom=bottom;
}
public double area(){
return height*bottom/p;
}
}
/*文件4——长方形*/
package packageChapter4;
public class Rectangle extends PlaneGraphics {
protected double length;
protected double width;
public Rectangle(double length,double width){
super("长方形");
this.length=length;
this.width=width;
}
public double area(){
return width*length;
}
}
/*文件5——椭圆形*/
package packageChapter4;
public class Ellipse extends PlaneGraphics {
protected double radius_a;
protected double radius_b;
Ellipse(double radius_a,double radius_b){
super("椭圆");
this.radius_a=radius_a;
this.radius_b=radius_b;
}
public double area(){
return Math.PI*radius_a*radius_b;
}
}
额,自己做出来了
贴一个长方形的代码,剩下的都一样修改一下就行
先定义一个接口其中有2个抽象方法,area()和 print(),其中print为输出。字数超出限
package package1接口类方法;
public class Rectangle implements Main {
protected double length;
protected double width;
protected String shape="长方形";
public Rectangle(double length,double width){
this.length=length;
this.width=width;
}
public double area(){
return width*length;
}
public void print(){
System.out.println(this.shape+ "面积为: "+this.area() );
}
public static void main(String[] args){
Rectangle R=new Rectangle(10,20);
R.print();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询