Java抽象类和借口两种方法

java 使用抽象类和借口两种方法,定义图形然后分别基于抽象类,接口派生若干子类,分别计算三角形,长方形,椭圆面积。

//1、抽象类的方式
abstract class Shape {
    public abstract double shapeS();	//面积
}

public class Rectangle extends Shape {
    private double l;	//长
    private double w;	//宽

    public double shapeS(){
        return 0.5*l*w;
    }

    public Rectangle(double l,double w){
        this.l=l;
        this.w=w;
    }
}

public class Squrtz extends Shape {
    private double l;	//长
    private double w;	//宽

   
    public double shapeS(){
        return l*w;
    }

    public Squrtz(double l,double w){
        this.l=l;
        this.w=w;
    }
}


public class tuoyuan extends Shape {
    private double l;	//长半轴
    private double w;	//短半轴

   
    public double shapeS(){
        return Maht.PI*l*w;
    }

    public tuoyuan(double l,double w){
        this.l=l;
        this.w=w;
    }
}



//2、接口方式
public interface Shape {
	public double shapeS();
}

public class Rectangle implements Shape{
    private double l;	//长
    private double w;	//宽
    public Rectangle(double l,double w){
        this.l=l;
        this.w=w;
    }

	@Override
	public double shapeS() {
		
		 return 0.5*l*w;
	}

}

public class Squrtz implements Shape{
    private double l;	//长
    private double w;	//宽
    public Squrtz(double l,double w){
        this.l=l;
        this.w=w;
    }

	@Override
	public double shapeS() {
		
		 return l*w;
	}

}

public class tuoyuan implements Shape{
    private double l;	//长半轴
    private double w;	//短半轴
    public tuoyuan(double l,double w){
        this.l=l;
        this.w=w;
    }

	@Override
	public double shapeS() {
		  return Maht.PI*l*w;
	}

}

以作答。抽象类和接口方法。

如有帮助望采纳。

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632