一、代码思路
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(“提示内容”);