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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,让我们来进行一道综合练习。 假设我们正在设计一个游戏,游戏中有多种角色,每种角色都有自己的属性和行为。我们来考虑如何使用Java继承和多来实现这个游戏。 首先,我们可以定义一个抽象类`Character`,表示所有角色的基类,其中包含角色的基本属性和方法: ```java public abstract class Character { protected String name; // 角色名 protected int level; // 等级 protected int health; // 生命值 protected int mana; // 法力值 public Character(String name, int level, int health, int mana) { this.name = name; this.level = level; this.health = health; this.mana = mana; } public abstract void attack(); // 攻击方法 public abstract void defend(); // 防御方法 // getter和setter方法 // ... } ``` 然后,我们可以定义具体的角色类,例如战士`Warrior`和法师`Mage`,它们分别继承自`Character`类,并实现自己的攻击和防御方法: ```java public class Warrior extends Character { private int strength; // 力量属性 public Warrior(String name, int level, int health, int mana, int strength) { super(name, level, health, mana); this.strength = strength; } @Override public void attack() { System.out.println("战士" + name + "使用大剑攻击敌人!"); } @Override public void defend() { System.out.println("战士" + name + "使用盾牌防御敌人的攻击!"); } // getter和setter方法 // ... } public class Mage extends Character { private int intelligence; // 智力属性 public Mage(String name, int level, int health, int mana, int intelligence) { super(name, level, health, mana); this.intelligence = intelligence; } @Override public void attack() { System.out.println("法师" + name + "释放火球术攻击敌人!"); } @Override public void defend() { System.out.println("法师" + name + "使用魔法盾防御敌人的攻击!"); } // getter和setter方法 // ... } ``` 最后,我们可以在游戏中创建不同的角色对象,并进行攻击和防御操作: ```java public class Game { public static void main(String[] args) { Character warrior = new Warrior("张三", 10, 100, 50, 20); Character mage = new Mage("李四", 10, 80, 100, 30); warrior.attack(); mage.defend(); } } ``` 以上就是一个简单的继承和多的综合练习,通过这个例子,我们可以发现继承和多能够很好地实现代码的复用和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

再见以前说再见

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

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

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

打赏作者

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

抵扣说明:

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

余额充值