JAVA中的继承和多态问题,问题解决再多加50财富,谢谢! 要求利用继承和多态的知识设计一组用于图形处理的
答案:3 悬赏:10 手机版
解决时间 2021-03-24 14:07
- 提问者网友:川水往事
- 2021-03-24 10:34
JAVA中的继承和多态问题,问题解决再多加50财富,谢谢! 要求利用继承和多态的知识设计一组用于图形处理的
最佳答案
- 五星知识达人网友:骨子里都是戏
- 2021-03-24 11:13
MyCircle文件
import static java.lang.Math.*;
public class MyCircle extends MyShape {
private double radius;
@Override
public double getS() {
return PI * pow(radius, 2);
}
@Override
public double getL() {
return 2 * PI * radius;
}
public double get_radius() {
return radius;
}
public void set_radius(double radius) {
this.radius = radius;
}
public static void main(String[] args) {
}
@Override
public void showInfo() {
System.out.println("this is a MyCircle!");
}
}
MyRectangle文件
public class MyRectangle extends MyShape {
private double length;
private double width;
@Override
public double getS() {
return length * width;
}
@Override
public double getL() {
return (length + width) * 2;
}
public double get_length() {
return length;
}
public void set_length(double width) {
this.width = width;
}
public double get_width() {
return width;
}
public void set_width(double width) {
this.width = width;
}
@Override
public void showInfo() {
System.out.println("this is a MyRectangle!");
}
}
MyShape文件
public abstract class MyShape implements ShowShape {
public abstract double getS();
public abstract double getL();
}
ShowShape文件
public interface ShowShape {
public void showInfo();
}
TestMyShape文件
public class TestMyShape {
public static void main(String[] args) {
MyCircle myCircle=new MyCircle();
MyRectangle myRectangle=new MyRectangle();
myCircle.showInfo();
myCircle.set_radius(10);
myCircle.getL();
myCircle.getS();
myRectangle.showInfo();
myRectangle.set_length(10);
myRectangle.set_width(10);
myRectangle.getL();
myRectangle.getS();
}
}
import static java.lang.Math.*;
public class MyCircle extends MyShape {
private double radius;
@Override
public double getS() {
return PI * pow(radius, 2);
}
@Override
public double getL() {
return 2 * PI * radius;
}
public double get_radius() {
return radius;
}
public void set_radius(double radius) {
this.radius = radius;
}
public static void main(String[] args) {
}
@Override
public void showInfo() {
System.out.println("this is a MyCircle!");
}
}
MyRectangle文件
public class MyRectangle extends MyShape {
private double length;
private double width;
@Override
public double getS() {
return length * width;
}
@Override
public double getL() {
return (length + width) * 2;
}
public double get_length() {
return length;
}
public void set_length(double width) {
this.width = width;
}
public double get_width() {
return width;
}
public void set_width(double width) {
this.width = width;
}
@Override
public void showInfo() {
System.out.println("this is a MyRectangle!");
}
}
MyShape文件
public abstract class MyShape implements ShowShape {
public abstract double getS();
public abstract double getL();
}
ShowShape文件
public interface ShowShape {
public void showInfo();
}
TestMyShape文件
public class TestMyShape {
public static void main(String[] args) {
MyCircle myCircle=new MyCircle();
MyRectangle myRectangle=new MyRectangle();
myCircle.showInfo();
myCircle.set_radius(10);
myCircle.getL();
myCircle.getS();
myRectangle.showInfo();
myRectangle.set_length(10);
myRectangle.set_width(10);
myRectangle.getL();
myRectangle.getS();
}
}
全部回答
- 1楼网友:蕴藏春秋
- 2021-03-24 12:21
abstract class MyShape{
abstract double area();//求面积
abstract double perimeter();//求参数
}
interface ShowShape{
void show();
}
class MyCircle extends MyShape implements ShowShape{
double radius;
double get_radius(){return radius;}
void set_radius(double r){radius=r;}
double area() {return 3.1415926*radius*radius;}
double perimeter() {return 2*3.1415926*radius;}
public void show(){System.out.println(
"圆的半径为"+radius+",面积为"+area()+",周长为"+perimeter());}
}
class MyRectangle extends MyShape implements ShowShape{
double length,width;
double get_length(){return length;}
void set_length(double l){length=l;}
double get_width(){return width;}
void set_width(double w){width=w;}
double area() {return length*width;}
double perimeter() {return 2*(length+width);}
public void show(){System.out.println(
"矩形的长为"+length+",宽为"+width+",面积为"+area()+",周长为"+perimeter());}
}
public class TestMyShape {
public static void main(String[] args) {
MyCircle circle=new MyCircle();
MyRectangle rect=new MyRectangle();
circle.set_radius(10);
System.out.println(circle.get_radius());
rect.set_length(15);
rect.set_width(10);
System.out.println(rect.get_length());
System.out.println(rect.get_width());
circle.show();
rect.show();
}
}
abstract double area();//求面积
abstract double perimeter();//求参数
}
interface ShowShape{
void show();
}
class MyCircle extends MyShape implements ShowShape{
double radius;
double get_radius(){return radius;}
void set_radius(double r){radius=r;}
double area() {return 3.1415926*radius*radius;}
double perimeter() {return 2*3.1415926*radius;}
public void show(){System.out.println(
"圆的半径为"+radius+",面积为"+area()+",周长为"+perimeter());}
}
class MyRectangle extends MyShape implements ShowShape{
double length,width;
double get_length(){return length;}
void set_length(double l){length=l;}
double get_width(){return width;}
void set_width(double w){width=w;}
double area() {return length*width;}
double perimeter() {return 2*(length+width);}
public void show(){System.out.println(
"矩形的长为"+length+",宽为"+width+",面积为"+area()+",周长为"+perimeter());}
}
public class TestMyShape {
public static void main(String[] args) {
MyCircle circle=new MyCircle();
MyRectangle rect=new MyRectangle();
circle.set_radius(10);
System.out.println(circle.get_radius());
rect.set_length(15);
rect.set_width(10);
System.out.println(rect.get_length());
System.out.println(rect.get_width());
circle.show();
rect.show();
}
}
- 2楼网友:梦中风几里
- 2021-03-24 11:26
abstract class MyShape implements ShowShape {
abstract public double getArray();
abstract public double getPerimeter();
abstract public void showPic();
}
class MyCircle extends MyShape {
private double radius;
public double getArray() {
return Math.PI * Math.pow(get_radius(),2);
}
public double getPerimeter() {
return 2 * Math.PI * get_radius();
}
public double get_radius() {
return this.radius;
}
public void set_radius(double radius) {
this.radius = radius;
}
public void showPic() {
System.out.println("圆半径:"+get_radius()+",周长:"+getPerimeter()+",面积:"+getArray());
}
}
class MyRectangle extends MyShape {
private double length;
private double width;
public double get_length() {
return this.length;
}
public void set_length(double length) {
this.length = length;
}
public double get_width() {
return this.width;
}
public void set_width(double width) {
this.width = width;
}
public double getArray() {
return get_width() * get_length();
}
public double getPerimeter() {
return 2 * (get_width() + get_length());
}
public void showPic() {
System.out.println("长方形长:"+get_length()+",宽:"+get_width()+",周长:"+getPerimeter()+",面积:"+getArray());
}
}
interface ShowShape {
public void showPic();
}
public class TestMyShape {
public static void main(String[] args) {
MyRectangle mr = new MyRectangle();
MyCircle mc = new MyCircle();
mr.set_width(1);
mr.set_length(2);
mc.set_radius(3);
mr.showPic();
mc.showPic();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯