Java 继承和多态 课后练习

 (1)定义Point(点)、Circle(圆形)、Square(正方形)类。点信息包括x,y坐标。圆信息包括圆心坐标和半径。正方形信息包括中心坐标和边长。用关联(Point对象作为Circle和Square的成员)和继承(Circle和Square分别继承Point)两种方法实现并测试。

import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入p的x坐标:");
		double x = sc.nextDouble();
		System.out.println("请输入p的y坐标:");
		double y = sc.nextDouble();
		System.out.println("请输入r或a的值:");
		double a = sc.nextDouble();
		Circle1 c1 = new Circle1(x, y, a);
		System.out.println(c1);
		Square1 s1 = new Square1(x, y, a);
		System.out.println(s1);
		Circle2 c2 = new Circle2(x, y, a);
		System.out.println(c2);
		Square2 s2 = new Square2(x, y, a);
		System.out.println(s2);
	}
}

class Point { // 关联:Point对象作为Circle1和Square1的成员
				// 继承:Circle2和Square2分别继承Point
	public Point(double x, double y) {
		super();
		this.x = x;
		this.y = y;
	}

	double x, y;
}

class Circle1 { // Point对象作为Circle1和Square1的成员
	Point p;
	double r;

	Circle1(double x, double y, double r) {
		this.p = new Point(x, y);
		this.r = r;
	}

	@Override
	public String toString() {
		return "关联Circle1: p=" + p + ", r=" + r;
	}
}

class Square1 { // Point对象作为Circle1和Square1的成员
	Point p;
	double a;

	Square1(double x, double y, double a) {
		this.p = new Point(x, y);
		this.a = a;
	}

	@Override
	public String toString() {
		return "关联Square1: p=" + p + ", a=" + a;
	}
}

class Circle2 extends Point { // Circle2和Square2分别继承Point
	double r;

	Circle2(double x, double y, double r) {
		super(x, y);
		this.r = r;
	}

	@Override
	public String toString() {
		return "继承Square2: " + "p=(" + x + "," + y + "),r=" + r;
	}
}

class Square2 extends Point { // Circle2和Square2分别继承Point
	double a;

	Square2(double x, double y, double a) {
		super(x, y);
		this.a = a;
	}

	@Override
	public String toString() {
		return "继承Square2: " + "p=(" + x + "," + y + "),a=" + a;
	}
}

(2)定义抽象类Shape(形状),包含形状名字属性及相应构造方法。

定义两个接口,一个接口IArea定义求面积的方法,一个接口IVolume定义求体积的方法。

定义Shape类的子类矩形(Rectangle)类,实现IArea接口。

定义长方体类同时实现IArea(求表面积)和IVolume接口。

在main方法中测试。

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入形状:");
		String shape = sc.next();
		System.out.println("请分别输入矩形长、宽:");
		double x = sc.nextDouble();
		double y = sc.nextDouble();
		Rectangle rec1 = new Rectangle(shape, x, y);
		System.out.println("矩形的面积为:" + rec1.area());

		System.out.println("请分别输入长方体长、宽、高:");
		double l = sc.nextDouble();
		double w = sc.nextDouble();
		double h = sc.nextDouble();
		T t1 = new T("长方体", l, w, h);
		System.out.println("长方体的表面积为:" + t1.area());
		System.out.println("长方体的体积为:" + t1.v());
	}
}

abstract class Shape { // 定义抽象类Shape(形状),包含形状名字属性及相应构造方法。
	String name;

	public Shape(String name) { // 形状名字
		super();
		this.name = name;
	}
}

interface IArea { // 面积
	double area();
}

interface IVolume { // 体积
	double v();
}

class Rectangle extends Shape implements IArea { // Shape子类Rectangle矩形类
	double x, y;

	public Rectangle(String name, double x, double y) {
		super(name);
		this.x = x;
		this.y = y;
	}

	@Override
	public double area() {
		// TODO Auto-generated method stub
		return this.x * this.y;
	}
}

class T extends Shape implements IArea, IVolume { // T长方体
	double len, wid, h; // len长、wid宽、h高

	public T(String name, double len, double wid, double h) {
		super(name);
		this.len = len;
		this.wid = wid;
		this.h = h;
	}

	@Override
	public double area() { // 长方体求表面积
		// TODO Auto-generated method stub
		return (len * wid + len * h + h * wid) * 2;
	}

	@Override
	public double v() { // T长方体求体积
		// TODO Auto-generated method stub
		return len * wid * h;
	}
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

再见以前说再见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值