最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實現(xiàn)寵物商店管理

 更新時間:2020年10月29日 14:33:22   作者:龍雅  
這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)寵物商店管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java實現(xiàn)寵物商店管理的具體代碼,供大家參考,具體內(nèi)容如下

第一種實現(xiàn)方式:抽象類和對象數(shù)組

public abstract class AbstractPet //定義寵物模板
{
 private String name;  //名稱
 private String color;  //顏色
 private int age;   //年齡

 public AbstractPet(){}
 public AbstractPet(String name, String color, int age){
  this.setName(name);
  this.setColor(color);
  this.setAge(age);
 }

 public String getName(){
  return this.name;
 }
 public String getColor(){
  return this.color;
 }
 public int getAge(){
  return this.age;
 }
 public void setName(String name){
  this.name = name;
 }
 public void setColor(String color){
  this.color = color;
 }
 public void setAge(int age){
  if (age > 0)
  {
   this.age = age;
  }else{
   this.age = 1;
  }
 } 

 //定義抽象方法
 public abstract void printInfo();   //自我介紹
}
public class Dog extends AbstractPet
{
 public Dog(String name, String color, int age){
  super(name, color, age);
 }
 //實現(xiàn)抽象方法
 public void printInfo(){ //自我介紹
  System.out.println("狗: " + super.getName() + ",年齡 " + super.getAge() + "歲,顏色:" + super.getColor());
 }
}
public class Cat extends AbstractPet
{
 public Cat(String name, String color, int age){
  super(name, color, age);
 }
 //實現(xiàn)抽象方法
 public void printInfo(){ //自我介紹
  System.out.println("狗: " + super.getName() + ",年齡 " + super.getAge() + "歲,顏色:" + super.getColor());
 }
}
public class PetShop
{
 private AbstractPet[] pets;
 private int foot; //定義下標(biāo)

 public PetShop(int len){ //寵物數(shù)量由用戶確定
  if (len > 0)
  {
   this.pets = new AbstractPet[len];
  }else{
   this.pets = new AbstractPet[1];
  }
 }

 //添加寵物的方法
 public boolean add(AbstractPet pet){
  if (this.foot < this.pets.length)
  {
   this.pets[foot] = pet;
   this.foot ++;
   return true;
  }else{
   return false;
  }
 }

 //定義查詢寵物的方法[提供按照種類查詢和按照姓名查詢兩種方法]
 public AbstractPet[] search(String keyword){
  int n = 0; //定義查詢到的寵物數(shù)量
  AbstractPet[] searchPets = null;
  for (int i = 0; i < this.pets.length; i++)
  {
   if (this.pets[i] != null)
   {
    if (this.pets[i].getName().indexOf(keyword) != -1 || this.pets[i].getColor().indexOf(keyword) != -1)
    {
     n++;
    }
   }   
  }
  searchPets = new AbstractPet[n];
  n = 0;
  for (int i = 0; i < this.pets.length; i++)
  {
   if (this.pets[i] != null)
   {
    if (this.pets[i].getName().indexOf(keyword) != -1 || this.pets[i].getColor().indexOf(keyword) != -1)
    {
     searchPets[n] = this.pets[i];
     n ++;
    }
   }   
  }
  return searchPets;
 }
}
public class testPetShop
{
 public static void main(String[] args){
  PetShop p = new PetShop(5);
  p.add(new Dog("狗1", "黑色的", 3));
  p.add(new Dog("狗2", "紅色的", 2));
  p.add(new Cat("貓1", "褐色的", 3));
  p.add(new Cat("貓2", "黃色的", 3));
  p.add(new Cat("貓3", "黑色的", 5));
  p.add(new Dog("狗3", "棕色的", 4));
  print(p.search("黑"));
 }
 public static void print(AbstractPet pets[]){
  for (int i = 0; i < pets.length; i++)
  {
   if (pets[i] != null)
   {
    pets[i].printInfo();
   }   
  }
 }
}

第二種實現(xiàn)方式:接口和對象數(shù)組

interface IPet
{
 String getName(); //取得寵物姓名
 String getColor(); //取得寵物顏色
 int getAge();  //取得寵物年齡
 void show();   //顯示寵物信息
}
public class Dog implements IPet
{
 private String name;
 private String color;
 private int age;

 public Dog(String name, String color, int age){
  this.setName(name);
  this.setColor(color);
  this.setAge(age);
 }
 public String getName(){
  return this.name;
 }
 public String getColor(){
  return this.color;
 }
 public int getAge(){
  return this.age;
 }
 public void setName(String name){
  this.name = name;
 }
 public void setColor(String color){
  this.color = color;
 }
 public void setAge(int age){
  if (age < 0 || age > 50)
  {
   this.age = 1; //默認(rèn)值
  }else{
   this.age = age;
  }
 }
 public void show(){
  System.out.println(this.toString());
 }
 public String toString(){
  return "狗:" + this.getName() + " " + this.getColor() + " " + this.getAge();
 }
}
public class Cat implements IPet
{
 private String name;
 private String color;
 private int age;

 public Cat(String name, String color, int age){
  this.setName(name);
  this.setColor(color);
  this.setAge(age);
 }
 public String getName(){
  return this.name;
 }
 public String getColor(){
  return this.color;
 }
 public int getAge(){
  return this.age;
 }
 public void setName(String name){
  this.name = name;
 }
 public void setColor(String color){
  this.color = color;
 }
 public void setAge(int age){
  if (age < 0 || age > 50)
  {
   this.age = 1; //默認(rèn)值
  }else{
   this.age = age;
  }
 }
 public void show(){
  System.out.println(this.toString());
 }
 public String toString(){
  return "貓:" + this.getName() + " " + this.getColor() + " " + this.getAge();
 }
}
public class PetShop
{
 private IPet[] pets;
 private int foot;

 public PetShop(int len){ //寵物店的寵物數(shù)量由用戶決定
  if (len > 0)
  {
   pets = new IPet[len];
  }else{
   pets = new IPet[1]; //默認(rèn)最小數(shù)量為1
  }
 }
 public boolean add(IPet pet){
  if (this.foot < this.pets.length)
  {
   this.pets[this.foot] = pet;
   this.foot ++;
   return true;
  }else{
   return false;
  }
 }
 public IPet[] search(String keyword){
  //定義一個新的寵物對象數(shù)組,用來存儲符合查詢條件的寵物
  IPet[] resultPet = null; //不確定數(shù)量,要通過循環(huán)得到
  int count = 0; //用來存儲符合查詢條件的寵物數(shù)量
  for (int i = 0; i < this.pets.length; i++)
  {
   if (this.pets[i] != null)
   {
    if (this.pets[i].getName().indexOf(keyword) != -1 || this.pets[i].getColor().indexOf(keyword) != -1)
    {
     count ++;
    }
   }
  }
  resultPet = new IPet[count];
  int n = 0;
  for (int i = 0; i < this.pets.length; i++)
  {
   if (this.pets[i] != null)
   {
    if (this.pets[i].getName().indexOf(keyword) != -1 || this.pets[i].getColor().indexOf(keyword) != -1)
    {
     resultPet[n] = this.pets[i];
     n ++;
    }
   }
  }
  return resultPet;
 }
}
public class TestPetShop
{
 public static void main(String[] args){
  //創(chuàng)建一個寵物商店
  PetShop ps = new PetShop(7); //假設(shè)可以放置5只寵物
  ps.add(new Dog("旺旺", "黑色的",4));
  ps.add(new Dog("旺財", "白色的",6));
  ps.add(new Dog("小黑", "黃色的",3));
  ps.add(new Cat("波波", "褐色的",7));
  ps.add(new Cat("咪咪", "黑色的",8));
  ps.add(new Cat("小云", "灰色的",2));
  ps.add(new Dog("仔仔", "黃色的",5));
  print(ps.search("色"));
 }
 public static void print(IPet[] pet){
  for (int i = 0; i < pet.length; i++)
  {
   if (pet[i] != null)
   {
    pet[i].show();
   }
  }
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • idea 多模塊項目依賴父工程class找不到問題的方法

    idea 多模塊項目依賴父工程class找不到問題的方法

    這篇文章主要介紹了idea 多模塊項目依賴父工程class找不到問題的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • java保證對象在內(nèi)存中唯一性的實現(xiàn)方法

    java保證對象在內(nèi)存中唯一性的實現(xiàn)方法

    這篇文章主要介紹了java如何保證對象在內(nèi)存中的唯一性,如果創(chuàng)建多個對象的話,可能會引發(fā)出各種各樣的問題,這時,就需要我們保證這個對象在內(nèi)存中的唯一性,需要的朋友可以參考下
    2019-06-06
  • 詳解如何將springboot項目導(dǎo)出成war包

    詳解如何將springboot項目導(dǎo)出成war包

    這篇文章主要介紹了詳解如何將springboot項目導(dǎo)出成war包,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • java金額數(shù)字轉(zhuǎn)中文工具類詳解

    java金額數(shù)字轉(zhuǎn)中文工具類詳解

    這篇文章主要為大家詳細(xì)介紹了java金額數(shù)字轉(zhuǎn)中文工具類的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Runtime.getRuntime().exec 路徑包含空格的解決

    Runtime.getRuntime().exec 路徑包含空格的解決

    這篇文章主要介紹了Runtime.getRuntime().exec 路徑包含空格的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Redisson公平鎖的源碼解讀分享

    Redisson公平鎖的源碼解讀分享

    Redisson是一個在Redis的基礎(chǔ)上實現(xiàn)的Java駐內(nèi)存數(shù)據(jù)網(wǎng)格(In-Memory?Data?Grid)。這篇文章主要通過源碼和大家聊聊Redisson公平鎖,需要的可以了解一下
    2022-11-11
  • Java中valueOf和parseInt的區(qū)別詳解

    Java中valueOf和parseInt的區(qū)別詳解

    這篇文章主要介紹了Java中valueOf和parseInt的區(qū)別詳解,在編程中,遇到類型轉(zhuǎn)換,好像會經(jīng)常用到 parseInt 和 valueOf,當(dāng)然這里只拿 Integer 類型進行陳述,其他類型也是雷同的,需要的朋友可以參考下
    2024-01-01
  • SpringBoot整合Quartz及異步調(diào)用的案例

    SpringBoot整合Quartz及異步調(diào)用的案例

    Quartz是一個完全由java編寫的開源作業(yè)調(diào)度框架、它的簡單易用受到業(yè)內(nèi)人士的一致好評,這篇文章主要介紹了SpringBoot整合Quartz及異步調(diào)用,需要的朋友可以參考下
    2023-03-03
  • 無感NullPointerException的值相等判斷方法

    無感NullPointerException的值相等判斷方法

    當(dāng)我們需要去判斷一個?入?yún)?查庫?返回的開關(guān)變量(通常是個Integer類型的)時,常常會寫如下的if-else判斷語句。但又會為在生產(chǎn)環(huán)境看到的「NullPointerException」感到困擾,遇到這個問題如何處理呢,下面小編通過本文給大家詳細(xì)講解,需要的朋友參考下吧
    2023-02-02
  • 詳解Swagger接口文檔和常用注解的使用

    詳解Swagger接口文檔和常用注解的使用

    Swagger是一款遵循?Restful?風(fēng)格的接口文檔開發(fā)神器,支持基于?API?自動生成接口文檔。本文將為大家講講Swagger接口文檔和常用注解的使用方法,需要的可以參考一下
    2022-08-08

最新評論

阿荣旗| 延津县| 宿迁市| 岳普湖县| 韶关市| 田林县| 松潘县| 通山县| 莒南县| 浑源县| 莒南县| 旬阳县| 芜湖县| 兴城市| 湖北省| 漳州市| 博湖县| 五河县| 新泰市| 洱源县| 双峰县| 宜州市| 龙陵县| 伊宁县| 吉水县| 大同市| 三都| 亚东县| 宝坻区| 台江县| 黎川县| 临夏县| 县级市| 东源县| 常德市| 太仓市| 河源市| 渭南市| 石城县| 铜梁县| 紫阳县|