• 通过宠物商店理解java面向对象


    前言:本篇博客,适合刚刚学完java基础语法的但是,对于面向对象,理解不够深刻的读者,本文通过经典的宠物商店,来让读者深刻的理解,面向对象,IS-AHAS-A法则。本文不仅仅是简单的用java来模拟一个宠物商店,而是通过分析宠物商店的业务,来让大家理解,最简单的业务逻辑,即商店商品用户,这三者的逻辑,这个代码同样可以适用于,图书管理系统书店管理系统宿舍管理系统等等的入门理解。

    好了本文开始了。首先我们先把题目给出来。

    题目

    在这里插入图片描述

    业务逻辑分析

    首先是分析,宠物店,动物,小猫小狗,客户之间的关系,其中最为明显的就是,小猫小狗与动物之间的关系,很显然小猫小狗继承动物的属性,他们之间属于IS-A,什么是IS-A法则,就是这种继承关系,所以,我们可以构建Animal类,思考所有动物的公共属性,比如我这里写的这个Animal类中的姓名价格性别颜色年龄这些成员,因为是举例子,所以很多地方并不会写的很全,然后再分析行为,我这里设计的他们的共有行为便是,Shout()大叫属性。

    Animal这个基类成功之后,那么后续就是,设计猫类,狗类了,对于这两个类的话,为了方便理解,我们设置他们的新的成员是,对于猫来说为CatPara,对于狗来说的话是DogPara,然后他们会重写父类的Shout()方法,这里就体现了面向对象里面的多态,具体代码如下。

    动物类的编写

    代码如下

    /*
    * 讲解面向对象的三个基本特性
    * IS-A法则
    * HAS-A法则
    * */
    
    // 动物类
    class Animal{
        private String name;  // 姓名
        private Double price; // 价格
        private Boolean sex; // 性别
        private String color; // 颜色 
        private Integer age; // 年龄
    
        public Animal() {
        }
    
        public void Shout(){ // 大叫
            System.out.println("大声的叫");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Double getPrice() {
            return price;
        }
    
        public void setPrice(Double price) {
            this.price = price;
        }
    
        public Boolean getSex() {
            return sex;
        }
    
        public void setSex(Boolean sex) {
            this.sex = sex;
        }
    
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public String toString() { // 重写toString()方法 方便遍历类
            return "Animal{" +
                    "name='" + name + '\'' +
                    ", price=" + price +
                    ", sex=" + sex +
                    ", color='" + color + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    // 狗类
    class Dog extends Animal{ // Dog is a animal // 继承父类属性 这里体现了继承
        private Integer DogPara; // 狗参数
    
        public Integer getDogPara() {
            return DogPara;
        }
    
        public void setDogPara(Integer dogPara) {
            DogPara = dogPara;
        }
    
        public void WagTail(){ // 对于小狗添加了一个新的摇尾巴的方法
            System.out.println("摇尾巴");
        }
        public Dog() {
        }
        @Override
        public void Shout(){   // 重写父类的方法 这里体现了多态
            System.out.println("汪汪汪");
        }
    
        @Override
        public String toString() {
            return "Dog{" +
                    "DogPara=" + DogPara + " " + super.toString() +
                    '}';
        }
    }
    
    // 猫类
    class Cat extends Animal{
        private Integer CatPara; // 猫参数
    
        public Integer getCatPara() {
            return CatPara;
        }
    
        public void setCatPara(Integer catPara) {
            CatPara = catPara;
        }
    
        public void LickHair(){   // 对于小猫 添加一个舔毛的方法
            System.out.println("舔毛");
        }
        public Cat() {
        }
        @Override
        public void Shout(){ // 重写父类属性
            System.out.println("喵喵喵");
        }
    
        @Override
        public String toString() {  // 重写toString()方法 方便遍历类
            return "Cat{" +			// super()调用父类的toString()方法 可以简写很多。
                    "CatPara=" + CatPara + " " + super.toString() +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132

    宠物店类的编写

    对于宠物店类,和宠物的关系,这里属于的是HAS-A法则,HAS-A法则什么意思了,比如,汽车轮胎之间的关系,轮胎属于汽车,在这个例题中的情况就是,宠物属于宠物店,这就是HAS-A法则。

    /*
    * Has-A法则
    *
    * */
    // 宠物店类
    class PetShop{
    	// 这里体现了HAS-A法则 宠物店包含小猫,小狗这些宠物
        private ArrayList<Dog> dogs;  // 宠物店的小狗的集合
        private ArrayList<Cat> cats;  // 宠物店中小猫的集合
    
        public PetShop() {  // 构造方法中加上代码 随机的生成初始数据
            Random random = new Random();
            dogs = new ArrayList<>();
            cats = new ArrayList<>();
            for (int i = 0; i < 100; i++) {
                   Dog dog = new Dog();
                   Cat cat = new Cat();
                   dog.setName("dog" + i);
                   cat.setName("cat" + i);
                   dog.setPrice(random.nextDouble() * 400 + 100); // [0,1) * 400 = [0, 400) + 100 = [100, 500)
                   cat.setPrice(random.nextDouble() * 400 + 100); // [0,1) * 400 = [0, 400) + 100 = [100, 500)
                   dog.setSex(random.nextBoolean());
                   cat.setSex(random.nextBoolean());
    
                   dog.setDogPara(random.nextInt(1000));
                   cat.setCatPara(random.nextInt(1000));
    
                   dog.setColor(random.nextInt(1000) + ":" + random.nextInt(1000) + ":" + random.nextInt(1000));
                   cat.setColor(random.nextInt(1000) + ":" + random.nextInt(1000) + ":" + random.nextInt(1000));
    
                   dog.setAge(random.nextInt(10));
                   cat.setAge(random.nextInt(10));
    
                   dogs.add(dog);
                   cats.add(cat);
            }
        }
    
    
    
        public void Traversal(){  // 遍历现有数据
            for (int i = 0; i < dogs.size(); i++) {
                System.out.println(dogs.get(i));
            }
            System.out.println("----------------------------------");
            Iterator<Cat> iterator2 = cats.iterator();
            while(iterator2.hasNext())
            {
                System.out.println(iterator2.next());
            }
        }
    
        public int getNumDog(){
            return dogs.size();
        }
        public int getNumCat(){
            return cats.size();
        }
    
        public ArrayList<Dog> getDogs() {
            return dogs;
        }
    
        public void setDogs(ArrayList<Dog> dogs) {
            this.dogs = dogs;
        }
    
        public ArrayList<Cat> getCats() {
            return cats;
        }
    
        public void setCats(ArrayList<Cat> cats) {
            this.cats = cats;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    客户类的编写

    这时候我们就需要分析最后一种关系,客户宠物店之间的关系,它们之间的关系,是两个单独的个体,至于怎么联合起来,通过客户类中的Buy(PetShop petShop)行为就可以理解了。

    
    class Custom{
        private Boolean sex;
        private Integer age;
    
        private String name;
    
        private ArrayList<Dog> dogs;
        private ArrayList<Cat> cats;
    
        public Custom() {
        }
    
        public Custom(String name) {
            this.name = name;
        }
    
        public boolean Buy(PetShop petShop){ // 这是客户在宠物店中购买商品的情况 
        // 因为这是一个讲解面向对象的,所以这里作者就先不补全了,示意即可
            Scanner in = new Scanner(System.in);
            System.out.println("Y/N"); // 小狗就是Y 小猫就是N
            String flag = in.nextLine().toLowerCase();
            if (flag.equals("y")){
    
            }
            else {
    
            }
            return false;
        }
    
        public Boolean getSex() {
            return sex;
        }
    
        public void setSex(Boolean sex) {
            this.sex = sex;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public ArrayList<Dog> getDogs() {
            return dogs;
        }
    
        public void setDogs(ArrayList<Dog> dogs) {
            this.dogs = dogs;
        }
    
        public ArrayList<Cat> getCats() {
            return cats;
        }
    
        public void setCats(ArrayList<Cat> cats) {
            this.cats = cats;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    代码汇总

    通过该文章的这个例题的简介,基本上就讲解完了,面向对象的基础和IS-A,HAS-A法则。

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.Random;
    import java.util.Scanner;
    
    /*
    * 讲解面向对象的三个基本特性
    * IS-A法则
    * HAS-A法则
    * */
    class Animal{
        private String name;  // 姓名
        private Double price; // 价格
        private Boolean sex; // 性别
        private String color; // 颜色 
        private Integer age; // 年龄
    
        public Animal() {
        }
    
        public void Shout(){ // 大叫
            System.out.println("大声的叫");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Double getPrice() {
            return price;
        }
    
        public void setPrice(Double price) {
            this.price = price;
        }
    
        public Boolean getSex() {
            return sex;
        }
    
        public void setSex(Boolean sex) {
            this.sex = sex;
        }
    
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Animal{" +
                    "name='" + name + '\'' +
                    ", price=" + price +
                    ", sex=" + sex +
                    ", color='" + color + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    class Dog extends Animal{ // Dog is a animal
        private Integer DogPara;
    
        public Integer getDogPara() {
            return DogPara;
        }
    
        public void setDogPara(Integer dogPara) {
            DogPara = dogPara;
        }
    
        public void WagTail(){
            System.out.println("摇尾巴");
        }
        public Dog() {
        }
        @Override
        public void Shout(){
            System.out.println("汪汪汪");
        }
    
        @Override
        public String toString() {
            return "Dog{" +
                    "DogPara=" + DogPara + " " + super.toString() +
                    '}';
        }
    }
    
    class Cat extends Animal{
        private Integer CatPara;
    
        public Integer getCatPara() {
            return CatPara;
        }
    
        public void setCatPara(Integer catPara) {
            CatPara = catPara;
        }
    
        public void LickHair(){
            System.out.println("舔毛");
        }
        public Cat() {
        }
        @Override
        public void Shout(){
            System.out.println("喵喵喵");
        }
    
        @Override
        public String toString() {
            return "Cat{" +
                    "CatPara=" + CatPara + " " + super.toString() +
                    '}';
        }
    }
    
    /*
    * Has-A法则
    *
    * */
    
    class PetShop{
        private ArrayList<Dog> dogs;
        private ArrayList<Cat> cats;
    
        public PetShop() {
            Random random = new Random();
            dogs = new ArrayList<>();
            cats = new ArrayList<>();
            for (int i = 0; i < 100; i++) {
                   Dog dog = new Dog();
                   Cat cat = new Cat();
                   dog.setName("dog" + i);
                   cat.setName("cat" + i);
                   dog.setPrice(random.nextDouble() * 400 + 100); // [0,1) * 400 = [0, 400) + 100 = [100, 500)
                   cat.setPrice(random.nextDouble() * 400 + 100); // [0,1) * 400 = [0, 400) + 100 = [100, 500)
                   dog.setSex(random.nextBoolean());
                   cat.setSex(random.nextBoolean());
    
                   dog.setDogPara(random.nextInt(1000));
                   cat.setCatPara(random.nextInt(1000));
    
                   dog.setColor(random.nextInt(1000) + ":" + random.nextInt(1000) + ":" + random.nextInt(1000));
                   cat.setColor(random.nextInt(1000) + ":" + random.nextInt(1000) + ":" + random.nextInt(1000));
    
                   dog.setAge(random.nextInt(10));
                   cat.setAge(random.nextInt(10));
    
                   dogs.add(dog);
                   cats.add(cat);
            }
        }
    
    
    
        public void Traversal(){
            for (int i = 0; i < dogs.size(); i++) {
                System.out.println(dogs.get(i));
            }
            System.out.println("----------------------------------");
            Iterator<Cat> iterator2 = cats.iterator();
            while(iterator2.hasNext())
            {
                System.out.println(iterator2.next());
            }
        }
    
        public int getNumDog(){
            return dogs.size();
        }
        public int getNumCat(){
            return cats.size();
        }
    
        public ArrayList<Dog> getDogs() {
            return dogs;
        }
    
        public void setDogs(ArrayList<Dog> dogs) {
            this.dogs = dogs;
        }
    
        public ArrayList<Cat> getCats() {
            return cats;
        }
    
        public void setCats(ArrayList<Cat> cats) {
            this.cats = cats;
        }
    }
    
    class Custom{
        private Boolean sex;
        private Integer age;
    
        private String name;
    
        private ArrayList<Dog> dogs;
        private ArrayList<Cat> cats;
    
        public Custom() {
        }
    
        public Custom(String name) {
            this.name = name;
        }
    
        public boolean Buy(PetShop petShop){
            Scanner in = new Scanner(System.in);
            System.out.println("Y/N"); // 小狗就是Y 小猫就是N
            String flag = in.nextLine().toLowerCase();
            if (flag.equals("y")){
    
            }
            else {
    
            }
            return false;
        }
    
        public Boolean getSex() {
            return sex;
        }
    
        public void setSex(Boolean sex) {
            this.sex = sex;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public ArrayList<Dog> getDogs() {
            return dogs;
        }
    
        public void setDogs(ArrayList<Dog> dogs) {
            this.dogs = dogs;
        }
    
        public ArrayList<Cat> getCats() {
            return cats;
        }
    
        public void setCats(ArrayList<Cat> cats) {
            this.cats = cats;
        }
    }
    
    public class Main {
    
    
        public static void main(String[] args) {
            PetShop petShop = new PetShop(); // 创建一个宠物店
    //        petShop.Traversal();
            Custom custom = new Custom("LiHua");
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
  • 相关阅读:
    【阿里云】图像识别 摄像模块 语音模块
    (一)esp32开发环境搭建(VSCode+IDF实现单步调试)
    模拟实现memcpy memmove,字符类库函数的介绍,strerror,strtok的使用讲解。
    高并发编程:并发容器
    Cannot install powershell modules
    LLaMA Factory多卡微调的实战教程
    oracle 如何查死锁
    6.1 排列
    Github每日精选(第67期):go语言必须跳过的坑100-go-mistakes
    百度网盘的扩容
  • 原文地址:https://blog.csdn.net/qq_51447496/article/details/127853033