zoukankan      html  css  js  c++  java
  • Document

    Dog类

     1 /***
     2  * 宠物狗狗类,使用权限修饰符private和public进行封装
     3  * @author chenyanlong
     4  * 日期:2017/10/15
     5  */
     6 package com.hp.test07;
     7 
     8 public class Dog {
     9 
    10      //定义变量
    11     private String name;//昵称
    12     private int health;//健康值
    13     private int love;//亲密度
    14     private String  strain;//设置狗狗的品种
    15     
    16     
    17     /*通过吃饭增加健康值*/
    18     public void eat(){
    19         if(health>=100){
    20             System.out.println("狗狗"+name+"需要运动了");
    21         }else{
    22             health=health+5;
    23             System.out.println("主人,"+name+"吃饱了!");
    24         }
    25     }
    26     
    27     /*通过与主人玩游戏增加亲密度,但同时减少健康值*/
    28     public void play(){
    29         if(health<20){
    30             System.out.println("主人,"+name+"生病了");
    31         }else{
    32             health=health-3;
    33             love=love+5;
    34         }
    35     }
    36     public String getName() {
    37         return name;
    38     }
    39     public void setName(String name) {
    40         this.name = name;
    41     }
    42     public int getHealth() {
    43         return health;
    44     }
    45     public void setHealth(int health) {
    46         this.health = health;
    47     }
    48     public int getLove() {
    49         return love;
    50     }
    51     public void setLove(int love) {
    52         this.love = love;
    53     }
    54     
    55 
    56     public String getStrain() {
    57         return strain;
    58     }
    59 
    60     public void setStrain(String strain) {
    61         this.strain = strain;
    62     }
    63 
    64     @Override
    65     public String toString() {
    66         return "Dog [name=" + name + ", health=" + health + ", love=" + love + ", Strain=" + strain + "]";
    67     }
    68     
    69     
    70     
    71 }

    DogTest类:

    package com.hp.test07;
    
    import java.util.Scanner;
    
    public class DogTest {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Dog dog=new Dog();
            
            System.out.println("----------------欢迎来到宠物店--------------");
            System.out.println("请选择需要领养的宠物品种:1.哈巴狗  2.牧羊犬");
            Scanner input=new Scanner(System.in);
            dog.setStrain(input.nextLine()); 
            
            System.out.println("请给你的爱犬起个好听的名字:");      
            Scanner input1=new Scanner(System.in);
            dog.setName(input1.next());
            
            System.out.println("设定健康值:eg:80");
            Scanner input2=new Scanner(System.in);
            dog.setHealth(input2.nextInt());
            
            System.out.println("设定爱心值:eg:80");
            Scanner input3=new Scanner(System.in);
            dog.setLove(input3.nextInt());
           
            System.out.println(dog.toString());
        }
    
    }

    运行效果:

  • 相关阅读:
    Textarea自适应文字内容调整高度
    把页面某内容放入粘贴板中
    通过javamail发送电子邮件
    Jrebel+tomcat实现热部署
    Eclipse启动Tomcat时,45秒超时解决方式
    mybatis 多对多 处理
    单例模式
    Centos6安装mysql5.7
    maven手动导入jar包到本地仓库
    Jsp与servlet本质上的区别
  • 原文地址:https://www.cnblogs.com/chenyanlong/p/7673929.html
Copyright © 2011-2022 走看看