程序要求
实现一个宠物商店模型,一个宠物商店可以保存多个宠物信息(主要属性:名字,年龄),可以实现宠物的上架,下架,模糊查询功能。
程序分析
首先应该有一个宠物的标准,具体的宠物比如猫,狗应该去实例化宠物,所以:定义一个宠物的接口,具体的实例要去实现这个接口。接下来宠物商店和宠物的关系是一对多,也就是:一个宠物商店有多个宠物,至于具体的宠物会有增删改查的操作,所以应该选择链表去存储宠物这个抽象概念
程序实现
1:定义一个宠物的标准
interface Pet{
public String getName();//得到宠物姓名
public int getAge();//得到宠物年龄
}
2:定义宠物商店
宠物商店并不关心具体的宠物是什么,它只在乎对宠物可以进行操作。
class PetShop{
private Link pets=new Link();//保存宠物信息(实例化)
public void add(Pet pet) {//实现:上架功能
this.pets.add(pet);
}
public void delete(Pet pet) {//实现:下架功能
this.pets.remove(pet);
}
/*难点:模糊查询*/
//由于:返回的一定是多个结果,所以返回Link即可
public Link search(String keyword) {
Link result=new Link();//result作为返回结果
Object obj[]=this.pets.toArray();//真正返回的是具体实例化的getName()
for(int x=0;x<obj.length;x++) {
Pet p=(Pet) obj[x];//向下转型
if(p.getName().contains(keyword)) {
result.add(p);//保存满足模糊查询的结果
}
}
return result;
}
}
3:实现接口
猫:
class Cat implements Pet{
private String name;
private int age;
public Cat(String name,int age) {
this.name=name;
this.age=age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
/*Object的两个方法*/
public String toString(){
return "猫名:"+this.name+",猫年龄:"+this.age;
}
public boolean equals(Object obj) {
if(this==obj) return true;
if(obj==null) return false;
if(!(obj instanceof Cat)) {
Cat c=(Cat) obj;
if(this.age==c.age&&this.name.equals(c.name)) {
return true;
}
}
return false;
}
}
狗:
class Dog implements Pet{
private String name;
private int age;
public Dog(String name,int age) {
this.name=name;
this.age=age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
/*Object的两个方法*/
public String toString(){
return "狗名:"+this.name+",狗年龄:"+this.age;
}
public boolean equals(Object obj) {
if(this==obj) return true;
if(obj==null) return false;
if(!(obj instanceof Dog)) {
Dog c=(Dog) obj;
if(this.age==c.age&&this.name.equals(c.name)) {
return true;
}
}
return false;
}
}
4:主程序实现
public class transfer {
public static void main(String[] args) {
PetShop shop=new PetShop();
shop.add(new Cat("狸花猫",3));
shop.add(new Cat("英短猫",7));
shop.add(new Cat("缅因猫",9));
shop.add(new Dog("哈士奇",13));
shop.add(new Dog("萨摩耶",8));
shop.add(new Dog("藏獒",5));
Link all=shop.search("哈");
Object obj[]=all.toArray();
for(int x=0;x<obj.length;x++) {
System.out.println(obj[x]);
}
}
}
输出结果:
完整程序
(链表类:可以直接使用)
class Link{
private class Node{//内部类Node
private Object data;
private Node next;
public Node(Object data) {
this.data=data;
}
public void addNode(Node newNode) {//增加节点
if(this.next==null) {
this.next=newNode;
}
else {
this.next.addNode(newNode);
}
}
public Object getNode(int index) {//查询节点
if(Link.this.foot++==index) {
return this.data;
}
else {
return this.next.getNode(index);
}
}
public void setNode(int index,Object data) {//修改节点
if(Link.this.foot++==index) {
this.data=data;
}
else {
this.next.setNode(index, data);
}
}
public boolean containsNode(Object data) {//判断指定节点是否存在
if(data.equals(this.data))
return true;
else {
if(this.next!=null) {
return this.next.containsNode(data);
}
else {
return false;
}
}}
public void removeNode(Node pre,Object data) {//删除节点
if(data.equals(this.data)) {
pre.next=this.next;
}
else {
this.next.removeNode(this, data);
}
}
public void toArrayNode() {//访问数据
Link.this.ret[Link.this.foot++]=this.data;
if(this.next!=null) {
this.next.toArrayNode();
}
}
}
private Node root;//根节点
private int count=0;
private int foot;
private Object[] ret;
public void add(Object data) {//增
Node newNode=new Node(data);
if(this.root==null) {
this.root=newNode;
}
else {
this.root.addNode(newNode);
}
this.count++;
}
public Object get(int index) {//查
if(this.count<index) return null;
this.foot=0;
return this.root.getNode(index);
}
public void set(int index,Object data) {//改
if(this.count<index) return;
this.foot=0;
this.root.setNode(index, data);
}
public boolean contains(Object data) {//判断存在与否
if (data==null||this.root==null) {
return false;
}
else {
return this.root.containsNode(data);
}
}
public void remove(Object data) {//删
if(this.contains(data)) {
if(data.equals(this.root.data)) {//Link直接处理根节点的删除
this.root=this.root.next;
}
else {
this.root.next.removeNode(this.root,data);
}
this.count--;
}}
public Object[] toArray(){
if(this.root==null) {
return null;
}
this.foot=0;
this.ret=new Object[this.count];
this.root.toArrayNode();
return this.ret;
}
public int size() {
return this.count;
}
}//链表类:直接用
interface Pet{
public String getName();//得到宠物姓名
public int getAge();//得到宠物年龄
}
class PetShop{
private Link pets=new Link();//保存宠物信息(实例化)
public void add(Pet pet) {//实现:上架功能
this.pets.add(pet);
}
public void delete(Pet pet) {//实现:下架功能
this.pets.remove(pet);
}
/*难点:模糊查询*/
//由于:返回的一定是多个结果,所以返回Link即可
public Link search(String keyword) {
Link result=new Link();//result作为返回结果
Object obj[]=this.pets.toArray();//真正返回的是具体实例化的getName()
for(int x=0;x<obj.length;x++) {
Pet p=(Pet) obj[x];//向下转型
if(p.getName().contains(keyword)) {
result.add(p);//保存满足模糊查询的结果
}
}
return result;
}
}
class Cat implements Pet{
private String name;
private int age;
public Cat(String name,int age) {
this.name=name;
this.age=age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
/*Object的两个方法*/
public String toString(){
return "猫名:"+this.name+",猫年龄:"+this.age;
}
public boolean equals(Object obj) {
if(this==obj) return true;
if(obj==null) return false;
if(!(obj instanceof Cat)) {
Cat c=(Cat) obj;
if(this.age==c.age&&this.name.equals(c.name)) {
return true;
}
}
return false;
}
}
class Dog implements Pet{
private String name;
private int age;
public Dog(String name,int age) {
this.name=name;
this.age=age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
/*Object的两个方法*/
public String toString(){
return "狗名:"+this.name+",狗年龄:"+this.age;
}
public boolean equals(Object obj) {
if(this==obj) return true;
if(obj==null) return false;
if(!(obj instanceof Dog)) {
Dog c=(Dog) obj;
if(this.age==c.age&&this.name.equals(c.name)) {
return true;
}
}
return false;
}
}
public class transfer {
public static void main(String[] args) {
PetShop shop=new PetShop();
shop.add(new Cat("狸花猫",3));
shop.add(new Cat("英短猫",7));
shop.add(new Cat("缅因猫",9));
shop.add(new Dog("哈士奇",13));
shop.add(new Dog("萨摩耶",8));
shop.add(new Dog("藏獒",5));
Link all=shop.search("哈");
Object obj[]=all.toArray();
for(int x=0;x<obj.length;x++) {
System.out.println(obj[x]);
}
}
}
总结
一个停车场停放多辆车(轿车,自行车)
一个公园里面多种树木(松树,柏树)
接口的目的:不同实例化对象之间的解耦合。