java定义一个接口
答案:4 悬赏:30 手机版
解决时间 2021-02-04 15:48
- 提问者网友:捧腹剧
- 2021-02-04 03:08
java定义一个接口,用于计算和输出规则徒刑的面积和周长;然后定义Circle Rectangle类,实现该接口,分别用于求圆和长方形面积和周长,再实现一个使用Circle Rectangle类的Java Application
最佳答案
- 五星知识达人网友:三千妖杀
- 2021-02-04 04:34
将以下四个文件放在同一个目录下编译运行:
文件1:
public class MyApp {
public static void main(String[] args) {
IBaseGraphics c1 = new Circle(5);
IBaseGraphics r1 = new Rectangle(3,4);
c1.printData();
r1.printData();
}
}
文件2:
public interface IBaseGraphics {
public double computeCircumference();//计算周长
public double computeSquare ();//计算面积
public void printData();
}
文件3:
public class Circle implements IBaseGraphics {
private double r = 0.0;
public Circle(double r){
this.r = r;
}
public double computeCircumference() {
return 2*Math.PI*r;
}
public double computeSquare() {
return Math.PI*r*r;
}
public void printData() {
System.out.println("该圆形的周长:"+this.computeCircumference()+";面积:"+this.computeSquare());
}
}
文件4:
public class Rectangle implements IBaseGraphics {
private double width = 0;
private double length =0;
public Rectangle(double width, double length){
this.width = width;
this.length = length;
}
public double computeCircumference() {
return 2*(width+length);
}
public double computeSquare() {
return width * length;
}
public void printData() {
System.out.println("该长方形的周长:"+this.computeCircumference()+";面积:"+this.computeSquare());
}
}
文件1:
public class MyApp {
public static void main(String[] args) {
IBaseGraphics c1 = new Circle(5);
IBaseGraphics r1 = new Rectangle(3,4);
c1.printData();
r1.printData();
}
}
文件2:
public interface IBaseGraphics {
public double computeCircumference();//计算周长
public double computeSquare ();//计算面积
public void printData();
}
文件3:
public class Circle implements IBaseGraphics {
private double r = 0.0;
public Circle(double r){
this.r = r;
}
public double computeCircumference() {
return 2*Math.PI*r;
}
public double computeSquare() {
return Math.PI*r*r;
}
public void printData() {
System.out.println("该圆形的周长:"+this.computeCircumference()+";面积:"+this.computeSquare());
}
}
文件4:
public class Rectangle implements IBaseGraphics {
private double width = 0;
private double length =0;
public Rectangle(double width, double length){
this.width = width;
this.length = length;
}
public double computeCircumference() {
return 2*(width+length);
}
public double computeSquare() {
return width * length;
}
public void printData() {
System.out.println("该长方形的周长:"+this.computeCircumference()+";面积:"+this.computeSquare());
}
}
全部回答
- 1楼网友:几近狂妄
- 2021-02-04 06:41
package day0422;
public interface Shap {
public double getPerimeter();//计算周长
public double getArea();//计算面积
}
、、、、、、、、、、、、、、、、、、、、、、、、、、
package day0422;
public class Circle implements Shap {
public static double radius;//园的半径
public void setRadius(double d){
radius=d;
}
public double getRadius(){
return radius;
}
public double getArea() {
// TODO Auto-generated method stub
return Math.PI*radius*radius;
}
public double getPerimeter() {
// TODO Auto-generated method stub
return 2*Math.PI*radius;
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
package day0422;
public class Rectangle implements Shap{
public double longer;//长
public double wid;//宽
public Rectangle(){
}
public Rectangle(double longer,double wid){
this.longer=longer;
this.wid=wid;
}
public double getLonger() {
return longer;
}
public void setLonger(double longer) {
this.longer = longer;
}
public double getWid() {
return wid;
}
public void setWid(double wid) {
this.wid = wid;
}
public double getArea() {
// TODO Auto-generated method stub
return longer*wid;
}
public double getPerimeter() {
// TODO Auto-generated method stub
return 2*longer+2*wid;
}
}
- 2楼网友:大漠
- 2021-02-04 06:12
接口类:
public interface coursechooseimpl {
public string qcc(string spe);
}
stu方法类:
public class stu implements coursechooseimpl{
int id; // 学号
string name[];// 姓名
char sex; // 性别
string spe; // 专业
string cou; // 课程
public stu(int id, string[] name, char sex, string spe, string cou) {
id = id;
this.name = name;
this.sex = sex;
this.spe = spe;
this.cou = cou;
}
@override
public string tostring() {
return spe+","+cou;
}
public void showinfo(){//输出学生的基本信息
system.out.println("学号:"+this.id);
system.out.println("姓名:"+this.name);
system.out.println("性别:"+this.sex);
system.out.println("专业:"+this.spe);
system.out.println("课程:"+this.cou);
}
@override
public string qcc(string spe) {
return "welcome to you !";
}
public static void main(string[] args) {
string[] names = {"小明","小芳","小刚"};
stu student = new stu(101, names , '男', "电子信息", "模拟电路");
string str= student. qcc("模拟电路");
system.out.println(str);
system.out.println(student);
}
}
- 3楼网友:底特律间谍
- 2021-02-04 05:27
public interface Cal{
public double calArea(double args[]);
public double calCircleLen(double args[]);
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯