zoukankan      html  css  js  c++  java
  • 第9次Java作业+LSYang

    【P270】宠物商店代码(各个不同功能的类放在不同的包中定义)

    org.lsy.demo.a里的Pet.java

    1 package org.lsy.demo.a;
    2 
    3 public interface Pet{    // 定义宠物接口
    4     public String getName() ;
    5     public String getColor() ;
    6     public int getAge() ;
    7 }

    org.lsy.demo.b里的Cat.java

     1 package org.lsy.demo.b;
     2 
     3 import org.lsy.demo.a.Pet ;
     4 
     5  public class Cat implements Pet{    // 猫是宠物,实现接口
     6     private String name ;    // 宠物名字
     7     private String color ;    // 宠物颜色
     8     private int age ;        // 宠物年龄
     9     public Cat(String name,String color,int age){
    10         this.setName(name) ;
    11         this.setColor(color) ;
    12         this.setAge(age) ;
    13     }
    14     public void setName(String name){
    15         this.name = name ;
    16     }
    17     public void setColor(String color){
    18         this.color = color;
    19     }
    20     public void setAge(int age){
    21         this.age = age ;
    22     }
    23     public String getName(){
    24         return this.name ;
    25     }
    26     public String getColor(){
    27         return this.color ;
    28     }
    29     public int getAge(){
    30         return this.age ;
    31     }
    32 };

    org.lsy.demo.b里的Dog.java

     1 package org.lsy.demo.b;
     2 
     3 import org.lsy.demo.a.Pet;
     4 
     5 public class Dog implements Pet{    // 狗是宠物,实现接口
     6     private String name ;    // 宠物名字
     7     private String color ;    // 宠物颜色
     8     private int age ;        // 宠物年龄
     9     public Dog(String name,String color,int age){
    10         this.setName(name) ;
    11         this.setColor(color) ;
    12         this.setAge(age) ;
    13     }
    14     public void setName(String name){
    15         this.name = name ;
    16     }
    17     public void setColor(String color){
    18         this.color = color;
    19     }
    20     public void setAge(int age){
    21         this.age = age ;
    22     }
    23     public String getName(){
    24         return this.name ;
    25     }
    26     public String getColor(){
    27         return this.color ;
    28     }
    29     public int getAge(){
    30         return this.age ;
    31     }
    32 };

    org.lsy.demo.c里的PetShop.java

     1 package org.lsy.demo.c;
     2 
     3 import org.lsy.demo.a.Pet;
     4 
     5 public class PetShop{    // 宠物商店
     6     private Pet[] pets ;    // 保存一组宠物
     7     private int foot ;
     8     public PetShop(int len){
     9         if(len>0){
    10             this.pets = new Pet[len] ;    // 开辟数组大小
    11         }else{
    12             this.pets = new Pet[1] ;    // 至少开辟一个空间
    13         }
    14     }
    15     public boolean add(Pet pet){    // 增加的是一个宠物
    16         if(this.foot<this.pets.length){
    17             this.pets[this.foot] = pet ;    // 增加宠物
    18             this.foot ++ ;
    19             return true ;
    20         }else{
    21             return false ;
    22         }
    23     }
    24     public Pet[] search(String keyWord){
    25         // 应该确定有多少个宠物符合要求
    26         Pet p[] = null ;
    27         int count = 0 ;    // 记录下会有多少个宠物符合查询结果
    28         for(int i=0;i<this.pets.length;i++){
    29             if(this.pets[i]!=null){        // 表示此位置有宠物
    30                 if(this.pets[i].getName().indexOf(keyWord)!=-1
    31                     ||this.pets[i].getColor().indexOf(keyWord)!=-1){
    32                     count++ ;    // 修改查找到的记录数
    33                 }
    34             }
    35         }
    36         p = new Pet[count] ;    // 开辟指定的大小空间
    37         int f = 0 ;    // 增加元素的位置标记
    38         for(int i=0;i<this.pets.length;i++){
    39             if(this.pets[i]!=null){        // 表示此位置有宠物
    40                 if(this.pets[i].getName().indexOf(keyWord)!=-1
    41                     ||this.pets[i].getColor().indexOf(keyWord)!=-1){
    42                     p[f] = this.pets[i] ;
    43                     f++ ;
    44                 }
    45             }
    46         }
    47         return p ;
    48 
    49     }
    50 };

    org.lsy.demo.c里的PetShopDemo.java

     1 package org.lsy.demo.d;
     2 
     3 import org.lsy.demo.b.Cat;
     4 import org.lsy.demo.b.Dog;
     5 import org.lsy.demo.a.Pet;
     6 import org.lsy.demo.c.PetShop;
     7 
     8 public class PetShopDemo{
     9     public static void main(String args[]){
    10         PetShop ps = new PetShop(5) ;    // 五个宠物
    11         ps.add(new Cat("白猫","白色的",2)) ;    // 增加宠物,成功
    12         ps.add(new Cat("黑猫","黑色的",3)) ;    // 增加宠物,成功
    13         ps.add(new Cat("花猫","花色的",3)) ;    // 增加宠物,成功
    14         ps.add(new Dog("拉步拉多","黄色的",3)) ;    // 增加宠物,成功
    15         ps.add(new Dog("金毛","金色的",2)) ;    // 增加宠物,成功
    16         ps.add(new Dog("黄狗","黑色的",2)) ;    // 增加宠物,失败
    17         print(ps.search("黑")) ;
    18     }
    19     public static void print(Pet p[]){
    20         for(int i=0;i<p.length;i++){
    21             if(p[i]!=null){
    22                 System.out.println(p[i].getName() + "," + p[i].getColor()
    23                     +"," + p[i].getAge()) ;
    24             }
    25         }
    26     }
    27 };
  • 相关阅读:
    网页图表控件Highcharts选项配置参数
    网页无法复制粘贴怎么办
    PHP #2003
    IE下有没有类似于Firebug的调试工具
    常用HTML标签的全称及描述
    [Angular] Improve Server Communication in Ngrx Effects with NX Data Persistence in Angular
    [Webpack] Detect Unused Code with Webpack and unused-files-webpack-plugin
    [Functional Programming Monad] Combine Stateful Computations Using Composition
    [Functional Programming Monad] Combine Stateful Computations Using A State Monad
    [Algorithm] Find Max Items and Max Height of a Completely Balanced Binary Tree
  • 原文地址:https://www.cnblogs.com/liusiyang1126/p/5465527.html
Copyright © 2011-2022 走看看