1.4 猫狗队列

本文介绍了如何使用Java实现一个猫狗队列的数据结构,详细阐述了代码思路,包括定义宠物类、子类猫和狗、队列元素类以及队列类。同时,讲解了Java语法中关于私有属性访问、主函数中成员变量赋值以及LinkedList作为队列的使用方法。此外,还提到了异常处理的技巧。

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

一、代码思路

1、定义宠物类,含私有属性类型,属性 --> 子类的不同;

2、定义猫类和狗类;

3、定义队列元素类,拥有宠物成员变量、时间戳属性;

4、定义队列类,拥有猫队列、狗队列、时间戳属性和一众成员方法;

5、在编写程序、调试程序时发现其他需求;

二、代码演示

1、定义宠物类
Pet.java

public class Pet {

    private String type;
    public String name; // 在主函数中要赋值的,所以public

    public Pet(String type){
        this.type = type;
    }

    public String getType() {
        return this.type;
    }
}

2、定义猫类和狗类
Dog.java

public class Dog extends Pet {

    public Dog(String name){
        super("dog"); // 调用父类构造方法,因为type 是 private 的,所以只能在父类操作
        this.name = name;
    }
}

3、定义队列元素类
APetOfQueue.java

public class APetOfQueue {

    private Pet pet;
    private long count;

    public APetOfQueue(Pet pet, long count){
        this.pet = pet;
        this.count = count;
    }

    public Pet getPet() {
        return this.pet;
    }

    public long getCount() {
        return this.count;
    }
}

4、定义队列类
DogCatQueue.java

import java.util.LinkedList;
import java.util.Queue;

public class DogCatQueue {
    private Queue<APetOfQueue> dogQueue;
    private Queue<APetOfQueue> catQueue;
    private long count;

    public DogCatQueue(){
        this.dogQueue = new LinkedList<APetOfQueue>();
        this.catQueue = new LinkedList<APetOfQueue>();
        this.count = 0;
    }

    public void add(Pet pet){
        if(pet.getType().equals("dog")){
            this.dogQueue.add(new APetOfQueue(pet,count++)); // 这里要有new
        }else if(pet.getType().equals("cat")){
            this.catQueue.add(new APetOfQueue(pet,count++));
        }
        else throw new RuntimeException("err,not dog or cat");
    }

    public Pet pollAll(){
        if(!this.dogQueue.isEmpty() && !this.catQueue.isEmpty()){
            if(this.dogQueue.peek().getCount() < this.catQueue.peek().getCount()){
                return this.dogQueue.poll().getPet(); // 这里用到了 getPet(),因为要返回的是宠物
            }else return this.catQueue.poll().getPet();
        }
        else if(!this.dogQueue.isEmpty())return this.dogQueue.poll().getPet();
        else if(!this.catQueue.isEmpty())return this.catQueue.poll().getPet();
        else throw new RuntimeException("err, queue is empty");
    }

    public Pet pollDog(){
        if(!this.dogQueue.isEmpty()){
            return this.dogQueue.poll().getPet();
        }
        else throw new RuntimeException("err, the dogQueue is empty");
    }

    public Pet pollCat(){
        if(!this.catQueue.isEmpty()){
            return this.catQueue.poll().getPet();
        }
        else throw new RuntimeException("err, the catQueue is empty");
    }

    public boolean isEmpty(){
        return this.dogQueue.isEmpty() && this.catQueue.isEmpty();
    }

    public boolean isDogEmpty(){
        return this.dogQueue.isEmpty();
    }

    public boolean isCatEmpty(){
        return this.catQueue.isEmpty();
    }
}

5、主函数

public class Test {
    public static void main(String[] args) {
        
        DogCatQueue q = new DogCatQueue();
        q.add(new Dog("dog1"));
        q.add(new Dog("dog2"));
        q.add(new Cat("cat1"));
        q.add(new Dog("dog3"));
        System.out.println(q.pollDog().getType());
        System.out.println(q.pollDog().name);
    }
}

三、Java 语法掌握

1、private 修饰的成员变量仅能在本类被访问;
子类继承父类的 private 成员变量只能用 super执行父类构造方法(作用在本类的实例上 访问或者 get 方法访问;

2、在主函数想赋值的类成员变量在类中设定 public 属性即可;

3、Pet pet 一个是引用数据类型,一个是变量名,变量名代表一个实例传入的参数应该是一个实例

4、使用队列,用 new LinkedList<>()
队列中的元素需要另外构造一个类;

5、在队列中使用时间戳
在队列元素中定义这个属性;
在队列中赋初值;

6、LinkedList 之于队列用法的使用函数 共4个
add() —— 在队尾增加元素
peek() —— 在队头返回元素
poll() —— 弹出对头元素并返回
isEmpty() —— 查看队列是否为空

7、主动抛出异常
throw new RuntimeException(“提示内容”);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值