当前位置: 首页 > news >正文

球类(继承和多态)

父类Ball,设置为抽象类,调用get和set方法创建对象,将子类重写的功能函数抽象化。

// 抽象球类
abstract class Ball {
    private String name;
    private double radius; // 半径
    private double weight; // 重量
    private double price; // 价格

    // 构造方法
    public Ball(String name, double radius, double weight, double price) {
        setName(name);
		setRadius(radius);
		setWeight(weight);
		setPrice(price);
    }

    // 抽象play方法,由子类实现
    public abstract void play();

    // 获取球的信息
    public void getInfo() {
        System.out.println("球类名称: " + getName());
        System.out.println("半径: " + getRadius() + " cm");
        System.out.println("重量: " + getWeight() + " g");
        System.out.println("价格: ¥" + getPrice());
    }

    // get set
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

 子类Basketball,继承父类属性,用super关键词调用父类的构造,重写play方法(多态)。

// 篮球类
class Basketball extends Ball {
    public Basketball(String name, double radius, double weight, double price) {
		// 调用父类的构造
        super(name, radius, weight, price);
    }
	// 重写
    @Override
    public void play() {
        System.out.println("投" + getName() + " - 用手投篮");
    }
}

  子类Football,继承父类属性,用super关键词调用父类的构造,重写play方法(多态)。

// 足球类
class Football extends Ball {
    // 构造方法
    public Football(String name, double radius, double weight, double price) {
		// 调用父类的构造方法
        super(name, radius, weight, price);
    }
	// 重写
    @Override
    public void play() {
        System.out.println("踢" + getName() + " - 用脚玩足球");
    }
}

   子类PingPang,继承父类属性,用super关键词调用父类的构造,重写play方法(多态)。

// 乒乓球类
class PingPong extends Ball {
    public PingPong(String name, double radius, double weight, double price) {
		// 调用父类的构造
        super(name, radius, weight, price);
    }
	// 重写
    @Override
    public void play() {
        System.out.println("击" + getName() + " - 用拍子击打乒乓球");
    }
}

主函数,调用父类创建子类对象。

public class Main {
    public static void main(String[] args) {
        // 创建各种球类实例
		// 调用父类创建子类对象
        Ball football = new Football("足球", 11, 450, 120);
        Ball basketball = new Basketball("篮球", 12, 600, 350);
        Ball pingPong = new PingPong("乒乓球", 2, 2.7, 5);

        // 测试足球
        System.out.println("===== 足球信息 =====");
        football.getInfo();
        football.play();

        // 测试篮球
        System.out.println("\n===== 篮球信息 =====");
        basketball.getInfo();
        basketball.play();

        // 测试乒乓球
        System.out.println("\n===== 乒乓球信息 =====");
        pingPong.getInfo();
        pingPong.play();
    }
}

 运行结果:

 

 

 

 

 

相关文章:

  • configMAX_SYSCALL_INTERRUPT_PRIORITY和configKERNEL_INTERRUPT_PRIORITY
  • 力扣刷题DAY10(动态规划-线性DP)
  • rcore day6
  • [ctfshow web入门] web23
  • cdw2: TypeScript
  • 牛客网:树的高度 ← 根节点为 0 号节点
  • 脚本启动 Java 程序
  • 工程师 - FTDI SPI converter
  • async/await 异步编程
  • 将飞帆制作的网页作为 div 集成到自己的网页中
  • C语言之九九乘法表
  • PCL拟合空间3D圆周 fit3DCircle
  • 数智孪生:制造业转型的驱动力
  • 4月8日日记
  • YOLOv11改进 | YOLOv11引入MobileNetV4
  • I/O进程3
  • 【STL】list介绍(附与vector的比较)
  • 硅谷甄选项目笔记
  • 递归实现排列型枚举 Java
  • Vue框架的编译器优化