第七次作业

本文通过四个具体实例介绍了Java编程中抽象类、接口的应用及多态性的体现。包括几何图形面积计算、动物类属性输出、打印机类型定义及实现PCI接口的各种功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一题

求几何图形的面积


/*
author:Kuo
 */
abstract class Shape {                            //几何图形
    public abstract double getArea();
}
class Square extends Shape{
    private double height=0;                      //正方形的边长
    public Square(double height){
        this.height=height;
    }
    public double getArea(){
        return (this.height=this.height);
    }
}
class Circle extends Shape{
    private double r = 0;                          //圆的半径
    private final static double PI = 3.14;         //圆周率
    public Circle(double r){
        this.r = r;
    }
    public double getArea(){
        return (PI*r*r);
    }
}

测试入口

class TestShape{
    public static void main(String[] args) {
        Shape square=new Square(3);
        Shape circle = new Circle(2);
        System.out.println(square.getArea());
        System.out.println(circle.getArea());
        Shape sq=(Shape) circle;
        System.out.println(sq.getArea());
    }
}

运行结果

3.0
12.56
12.56

第二题

定义动物类,输出种类的属性


/*
author:Kuo
 */
abstract class Animal {                   //抽象类动物
    private String color;
    private String type;
    private int age;
    private int weight;
    public String getColor(){
        return color;
    }
    public void setColor(String color){
        this.color=color;
    }
    public String getType(){
        return type;
    }
    public void setType(String type){
        this.type=type;
    }
    public int getAge(){
        return age;
    }
    public void setAge(int age){
        this.age=age;
    }
    public int getWeight(){
        return weight;
    }
    public void setWeight(int weight){
        this.weight=weight;
    }
    public abstract void info();
}
class Bird extends Animal {                      //小鸟继承动物
    @Override
    public String getColor(){
        return super.getColor();
    }
    @Override
    public void setColor(String color){
        super.setColor(color);
    }
    @Override
    public String getType(){
        return super.getType();
    }
    @Override
    public void setType(String type){
        super.setType(type);
    }
    @Override
    public int getAge(){
        return super.getAge();
    }
    @Override
    public void setAge(int age){
        super.setAge(age);
    }
    @Override
    public int getWeight(){
        return super.getWeight();
    }
    @Override
    public void setWeight(int weight) {
        super.setWeight(weight);
    }
    @Override
    public void info(){                                              //方法
        System.out.println("我是一只"+getColor()+"的"+getType());
        System.out.println("今年"+getAge()+"岁了");
    }
}
class Fish extends Animal {                                //鱼继承动物
    @Override
    public String getColor(){
        return super.getColor();
    }
    @Override
    public void setColor(String color){
        super.setColor(color);
    }
    @Override
    public String getType(){
        return super.getType();
    }
    @Override
    public void setType(String type){
        super.setType(type);
    }
    @Override
    public int getAge(){
        return super.getAge();
    }
    @Override
    public void setAge(int age){
        super.setAge(age);
    }
    @Override
    public int getWeight(){
        return super.getWeight();
    }
    @Override
    public void setWeight(int weight) {
        super.setWeight(weight);
    }
    @Override
    public void info(){                                                   //方法
        System.out.println("我是一条"+getWeight()+"重的"+getType());
        System.out.println("今年"+getAge()+"岁了");
    }
}

测试输出

 */
class Test {
    public static void main(String[] args) {
        Animal animal=new Bird();
        animal.setColor("红色");
        animal.setType("鸟");
        animal.setAge(4);
        animal.info();
        Animal animal1=new Fish();
        animal1.setWeight(5);
        animal1.setType("鱼");
        animal1.setAge(2);
        animal1.info();
    }
}

运行结果

我是一只红色的鸟
今年4岁了
我是一条5重的鱼
今年2岁了

进程已结束,退出代码为 0

第三题

定义抽象打印机类,输出三种打印机

/*
author:kuo
 */
public abstract class Printer {                  //抽象类打印机
    abstract void print();
}
class DotMatrixtPrinter extends Printer{         //针式打印机继承打印机类
    void print(){                                //输出针式打印
        System.out.println("针式打印");
    }
}
class InKpetPrinter extends Printer{              //喷墨打印继承打印机类
    void print(){
        System.out.println("喷墨打印");
    }
}
class LaserPrinter extends Printer{                //激光打印继承打印机类
    void print(){
        System.out.println("激光打印");
    }
}

测试输出

class example {
    public static void main(String[] args) {
        Printer pre = new DotMatrixtPrinter();
        pre.print();
        Printer prePrinter=new InKpetPrinter();
        prePrinter.print();
        Printer pre1=new LaserPrinter();
        pre1.print();
    }
}

运行结果

针式打印
喷墨打印
激光打印

进程已结束,退出代码为 0

第四题

定义接口PCI,实现各种功能输出

/*
author:kuo
*/
interface PCI {
    void start();
    void stop();
}
class NetworkCard implements PCI{
    public void start(){
        System.out.println("Sending data!");
    }
    public void stop()
    { System.out.println("Network Stop!");
    }
}
class DisplayCard implements PCI{
    public void start(){
        System.out.println("DisplayCard images!");
    }
    public void stop(){
        System.out.println("Stop DisplayCard!");
    }
}
class SoundCard implements PCI {
    public void start() {
        System.out.println("Sound Start!");
    }
    public void stop(){
        System.out.println("Sound Stop!");
    }
}

测试输出

public class Test{
    public static void usePCICard(PCI p){
        p.start();
        p.stop();
    }
    public static void main(String [] args) {
        PCI dc=new DisplayCard();
        usePCICard(dc);
        PCI sc=new SoundCard();
        usePCICard(sc);
        PCI nc=new NetworkCard();
        usePCICard(nc);
    }
}

运行结果
DisplayCard images!
Stop DisplayCard!
Sound Start!
Sound Stop!
Sending data!
Network Stop!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值