改写Java程序:1. 将本题的抽象类改写为接口,对程序其余部分做相应修改。2.将接口放入自定义包,并修改
abstractclassShape{//抽象类abstractprotecteddoublearea();//抽象方法,计算几何图形的面积abstractprotect...
abstract class Shape{ //抽象类
abstract protected double area(); //抽象方法,计算几何图形的面积
abstract protected void girth(); //抽象方法,计算机和图形的周长
}
class Rectangle extends Shape{ //抽象类的子类
float width,length;
Rectangle(float w,float 1){
width=w;
length=1;
}
public double area(){ //此方法是对超类抽象方法的具体实现
return width*length;
}
public void girth(){}; //此方法是对超类抽象方法的具体实现
}
public class Shape_ex{
public static void main(String args[]){
Rectangle rc=new Rectangle(6,12);
System.out.println("The area of rectangle :"+rc.area());
}
} 展开
abstract protected double area(); //抽象方法,计算几何图形的面积
abstract protected void girth(); //抽象方法,计算机和图形的周长
}
class Rectangle extends Shape{ //抽象类的子类
float width,length;
Rectangle(float w,float 1){
width=w;
length=1;
}
public double area(){ //此方法是对超类抽象方法的具体实现
return width*length;
}
public void girth(){}; //此方法是对超类抽象方法的具体实现
}
public class Shape_ex{
public static void main(String args[]){
Rectangle rc=new Rectangle(6,12);
System.out.println("The area of rectangle :"+rc.area());
}
} 展开
1个回答
展开全部
Shape.java
package com.examples.demo1.shape;
public interface Shape {
float area(); //计算几何图形的面积
float girth(); //计算机和图形的周长
}
Shape_ex.java
package com.examples.demo1;
import com.examples.demo1.shape.Shape;
class Rectangle implements Shape {
float width, length;
public Rectangle(float w, float l) {
width = w;
length = l;
}
public float area() {
return width * length;
}
public float girth() {
return (width + length) * 2;
}
}
public class Shape_ex {
public static void main(String[] args) {
Rectangle rc = new Rectangle(6, 12);
System.out.println("The area of rectangle :" + rc.area());
System.out.println("The girth of rectangle :" + rc.girth());
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询