zoukankan      html  css  js  c++  java
  • 【0711作业】使用封装实现企鹅

    宠物类

     1 package pet;
     2 /**
     3  * 使用封装实现宠物系统
     4  * 宠物类
     5  * @author L
     6  *
     7  */
     8 public class Pet {
     9     private int health;// 健康值
    10     private int love;// 亲密度
    11     private String name;// 名字
    12     private String sex;// 性别
    13 
    14     public int getHealth() {
    15         return health;
    16     }
    17 
    18     public void setHealth(int health) {
    19         if (health < 0 || health > 100) { // "健康值应该在0至100之间,默认值为60。";
    20             this.health = 60;
    21             return;
    22         }
    23         this.health = health;
    24     }
    25 
    26     public int getLove() {
    27         return love;
    28     }
    29 
    30     public void setLove(int love) {
    31         this.love = love;
    32 
    33     }
    34 
    35     public String getName() {
    36         return name;
    37     }
    38 
    39     public void setName(String name) {
    40         this.name = name;
    41     }
    42 
    43     public String getSex() {
    44         return sex;
    45     }
    46 
    47     public void setSex(String sex) {
    48         this.sex = sex;
    49     }
    50 
    51     public Pet() {
    52     }
    53 
    54     public Pet(int health, int love, String name, String sex) {
    55         if (health < 0 || health > 100) {
    56             System.out.println("健康值应该在0至100之间,默认值为60。");
    57             this.health = 60;
    58         } else {
    59             this.health = health;
    60         }
    61         this.love = 60;
    62         this.name = name;
    63         this.sex = sex;
    64     }
    65 
    66     // 宠物自白
    67     public String showInfo() {
    68         System.out.println("宠物的自白:");
    69         return "我的名字叫" + this.getName() + ",我是" + this.getSex() + ",健康值是" + this.getHealth() + ",初始亲密度为"
    70                 + this.getLove();
    71     }
    72 }

    企鹅类

    1 package pet;
    2 
    3 public class Qie extends Pet {
    4 
    5     public Qie(int health, int love, String name, String sex) {
    6         super(health, love, name, sex);
    7     }
    8 }

    狗类

    1 package pet;
    2 
    3 public class Dog extends Pet {
    4 
    5     public Dog(int health, int love, String name, String sex) {
    6         super(health, love, name, sex);
    7     }
    8 
    9 }

    测试

     1 package pet;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Main {
     6     static Scanner sc=new Scanner(System.in);
     7     public static void main(String[] args) {
     8             String sex;
     9             int love = 60;
    10             System.out.println("欢迎来到宠物商店!");
    11             System.out.println("请输入要领养的宠物的名字:");
    12             String name = sc.next();
    13             System.out.print("请选择要领养的宠物类型:(1、狗狗  2、企鹅)");
    14             int choose = sc.nextInt();
    15             switch (choose) {
    16             case 1:
    17                 System.out.println("请选择狗狗的性别:(1、雄性   2、雌性)");
    18                 int choose1 = sc.nextInt();
    19                 if(choose1 == 1) {
    20                     sex = "雄性";
    21                 }else {
    22                     sex = "雌性";
    23                 }
    24                 System.out.println("请输入狗狗的健康值:(0~100)");
    25                 int health = sc.nextInt();
    26                 System.out.println("我的亲密度初始值为60。");
    27                 Dog dog = new Dog(health, love, name, sex);
    28                 String str = dog.showInfo();
    29                 System.out.println(str);
    30                 break;
    31             case 2:
    32                 System.out.println("请选择企鹅的性别:(1、Q仔   2、Q妹)");
    33                 int choose2 = sc.nextInt();
    34                 if(choose2 == 1) {
    35                     sex = "Q仔";
    36                 }else {
    37                     sex = "Q妹";
    38                 }
    39                 System.out.println("请输入企鹅的健康值:(0~100)");
    40                 int health1 = sc.nextInt();
    41                 System.out.println("我的亲密度初始值为60。");
    42                 Qie Qie = new Qie(health1, love, name, sex);
    43                 String str1 = Qie.showInfo();
    44                 System.out.println(str1);
    45                 break;
    46 
    47             default:
    48                 System.out.println("欢迎使用宠物商店!");
    49                 break;
    50             }
    51         }
    52     }
  • 相关阅读:
    倒排索引压缩
    记一次java内存溢出的解决过程
    [译]ES读写文档时shard-replication模型
    [转载]抓包工具Charles乱码解决办法
    Mac 快捷键整理(不定期更新)
    高效能人士执行的四原则(2017-12-15)
    scala sbt 添加国内镜像
    maven工程小红叉处理方法
    系统管理中 bash shell 脚本常用方法总结
    scala 2.11报错error: not found: type Application
  • 原文地址:https://www.cnblogs.com/yanglanlan/p/11171566.html
Copyright © 2011-2022 走看看