慕粉3235270
2016-06-01 18:47
package com.imooc; public abstract class Shape { public abstract void perimeter();//求周长 public abstract void area();//求面积 }
package com.imooc; import java.util.Scanner; public class Rectangle extends Shape { public void perimeter() { Scanner input = new Scanner(System.in); System.out.println("请输入矩形的宽:"); float width = input.nextFloat(); System.out.println("请输入矩形的长:"); float length=input.nextFloat(); System.out.println("矩形的周长为:"+2*(width+length)); } public void area() { Scanner input = new Scanner(System.in); System.out.println("请输入矩形的宽:"); float width=input.nextFloat(); System.out.println("请输入矩形的长:"); float length=input.nextFloat(); System.out.println("矩形的面积为:"+width*length); } }
package com.imooc; import java.util.Scanner; public class Circle extends Shape { public void perimeter() { Scanner input = new Scanner(System.in); System.out.println("请输入圆形的半径:"); float radius=input.nextFloat(); System.out.println("圆形的周长为:"+radius*2*3.14); } public void area() { Scanner input = new Scanner(System.in); System.out.println("请输入圆形的半径:"); float radius=input.nextFloat(); System.out.println("圆形的面积:"+3.14*radius*radius); } }
package com.imooc; public class Initial { public static void main(String[] args) { Shape s1=new Rectangle(); s1.perimeter(); s1.area(); Shape s2=new Circle(); s2.perimeter(); s2.area(); } }
可以使用一个方法简写直接输出周长和面积
public abstract class Shape {
public abstract void get();//求周长和面积
}
import java.util.Scanner;
public class Rectangle extends Shape {
public void get() {
Scanner input = new Scanner(System.in);
System.out.println("请输入矩形的宽:");
float width = input.nextFloat();
System.out.println("请输入矩形的长:");
float length=input.nextFloat();
System.out.println("矩形的周长为:"+2*(width+length));
System.out.println("矩形的面积为:"+width*length);
}
}
import java.util.Scanner;
public class Circle extends Shape {
public void get() {
Scanner input = new Scanner(System.in);
System.out.println("请输入圆形的半径:");
float radius=input.nextFloat();
System.out.println("圆形的周长为:"+radius*2*3.14);
System.out.println("圆形的面积:"+3.14*radius*radius);
}
}
public class Initial {
public static void main(String[] args) {
Shape s1=new Rectangle();
s1.get();
Shape s2=new Circle();
s2.get();
}
}
哥们感觉你少了input.close();这个语句,没有这个语句会报错在我这
为什么我打 public class Rectangle extends Shape 这个代码时系统开始报错啊
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题