应用实例:面向对象开发

 

1.陈述: 宠物商店,短期内都有一定的库存量,顾客可以查询特定的宠物信息,店主可以在库存不足时增加新的宠物到店中, 实现一个简单的系统满足以上需求!
2.分析: 设计到的对象:1.宠物商店:PetShop,2.宠物:Pet,;
一个宠物商店可以有一定的库存,而且有多种宠物,可以不时添加库存,而且可以查询特定的宠物信息;
宠物有共同的性质:就是 有特定名字和特定年龄,以及它的价格;
对PetShop 来说 宠物应该是一种标准,只有实现这种标准的才是宠物,;

实际开发中,接口表示标准,表示一种能力,称为标识接口,表示方法视图,以暴露给远程的客户端使用。

使用接口进行合操作,可以增加程序的灵活性;

PetShop 其实是一种数组,元素是实现Pet标准的宠物类;

————– 实现:

package com.mldn;

 

public class PetShopDemo // 主类

{

public static void main(String[] args)

{

PetShop ps = new PetShop(5); // 开一家petshop,库存需求为5只;

ps.add(new Cat(“波斯猫”, 1, 150.0f)); // 进一只猫

ps.add(new Cat(“机器猫”, 2, 250.3f)); // 第二只

ps.add(new Dog(“砂皮狗”, 2, 623.9f)); // third

ps.add(new Dog(“猎豹”, 1, 10000.5f)); // forth

ps.add(new Dog(“臧獒”, 2, 150000.0f )); // fifth

ps.add(new Cat(“”, 3, 150.3f)); // sixth

 

System.out.println(“店中当前存货清单如下:”);

for (int x = 0; x

{

System.out.println(ps.getPets()[x]); // 打印所有存货的清单

}

 

Pet[] result = ps.search(“猫”); // 查询店中有什么猫

 

System.out.println(“您查询的匹配信息如下:”);

for (int x = 0; x

{

System.out.println(result[x]); // 打印匹配信息

}

}

}

 

class PetShop // 宠物商店类

{

private Pet[] pets = null; // 所有的宠物

private int len = 0; // 允许库存

private int foot = 0; // 库存占用量

 

public PetShop(int len) // 初始化一个petshop

{

if (len > 0)

{

this.pets = new Pet[len]; // 设置库存,pet number

}

else

{

this.pets = new Pet[1]; // 至少有一只宠物

}

}

 

public boolean add(Pet pet) // 增加宠物到库存

{

if (this.foot

{

this.pets[this.foot] = pet; // 入库

this.foot++; // 库存消耗增1

return true;

}

else

{

return false; // 库存已满,不再进货

}

}

 

public Pet[] getPets() // get all the pet in shop

{

return this.pets;

}

 

/*

关键字搜索得到的信息 可以是多条! 返回的应该是数组

*/

public Pet[] search(String keyWords) // 按关键字搜索特定宠物信息,这里默认是petName

{

Pet[] result = null; // 声明数组,存放搜索结果

 

int count = 0; // 表示找到多少宠物匹配信息;

 

for (int x = 0; x

{

if (this.pets[x].getName() != null) // 防止空名字,

{

if (this.pets[x].getName().indexOf(keyWords) != -1)

{

count++; // 计算匹配个数

}

}

}

 

result = new Pet[count]; // 开辟空间,保存内容

 

count = 0;

 

for (int x = 0; x

{

if (this.pets[x].getName() != null) // 防止空名字,

{

if (this.pets[x].getName().indexOf(keyWords) != -1)

{

result[count] = this.pets[x]; // 保存内容

count++; // 计算匹配个数

}

}

}

 

return result;

 

}

}

 

interface Pet // public interface

{

public String getName(); // Pet name

 

public int getAge(); // Pet age

 

public float getPrice(); // pet price

}

 

class Cat implements Pet // 宠物猫类

{

private String name;

private int age;

private float price;

 

public Cat(String name, int age, float price)

{

this.name = name;

this.age = age;

this.price = price;

}

 

public String getName()

{

return this.name;

}

 

public int getAge()

{

return this.age;

}

 

public float getPrice()

{

return this.price;

}

 

public void setName(String name)

{

this.name = name;

}

 

public void setAge(int age)

{

this.age = age;

}

 

public void setPrice(float price)

{

this.price = price;

}

 

public String toString()

{

return “名字:” + this.name + “,年龄:” + this.age + “岁” + “,价格:” + “¥” + this.price;

}

}

 

class Dog implements Pet // 宠物狗类

{

private String name;

private int age;

private float price;

 

public Dog(String name, int age, float price)

{

this.name = name;

this.age = age;

this.price = price;

}

 

public String getName()

{

return this.name;

}

 

public int getAge()

{

return this.age;

}

 

public float getPrice()

{

return this.price;

}

 

public void setName(String name)

{

this.name = name;

}

 

public void setAge(int age)

{

this.age = age;

}

 

public void setPrice(float price)

{

this.price = price;

}

 

public String toString()

{

return “名字:” + this.name + “,年龄:” + this.age + “岁” + “,价格:” + “¥” + this.price;

}

}

 

/*

administrator@xu-desktop:~$ javac -d . ./work/PetShopDemo.

administrator@xu-desktop:~$ com.mldn.PetShopDemo

店中当前存货清单如下:

名字:波斯猫,年龄:1岁,价格:¥150.0

名字:机器猫,年龄:2岁,价格:¥250.3

名字:砂皮狗,年龄:2岁,价格:¥623.9

名字:猎豹,年龄:1岁,价格:¥10000.5

名字:臧獒,年龄:2岁,价格:¥150000.0

您查询的匹配信息如下:

名字:波斯猫,年龄:1岁,价格:¥150.0

名字:机器猫,年龄:2岁,价格:¥250.3

 

*/

 

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《应用实例:面向对象开发
本文地址:https://www.zhiletu.com/archives-65.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微