zoukankan      html  css  js  c++  java
  • 作业1 2

    package oop5;
    
    public class Penguin {
    
        // 实现封装
    
        // 隐藏属性
        private String name;
        private int leiXin;
        private String gender;
        private int health;
        private int love;
        // 方法公开
        // 1 set 2 get
    
        public void setName(String name) {
    
            this.name = name;
        }
    
        public String getName() {
    
            return this.name;
        }
    
        public void setLeiXin(int leiXin) {
            this.leiXin = leiXin;
    
        }
    
        public int setLeiXin() {
            return this.leiXin;
    
        }
    
        public void setGender(String gender) {
    
            if (gender.equals("1")) {
    
                this.gender = "Q仔";
    
            } else if (gender.equals("2")) {
                this.gender = "Q妹";
            }
    
        }
    
        public String getGender() {
    
            return this.gender;
        }
    
        public void setHealth(int health) {
    
            if (health < 0) {
    
                this.health = 60;
                System.out.println("健康值应该再0至100之间,默认值为" + this.health);
            }
    
            this.health = health;
        }
    
        public int getHealth() {
            return this.health;
        }
    
        public void setLove(int love) {
    
            this.love = love;
        }
    
        public int getLove() {
    
            return this.love;
    
        }
    
        public void show() {
    
            System.out.println("宠物的自白:
    " + "我的名字叫豆豆,健康值是" + this.health + ",和主人的亲密度是" + this.love + "我的性别是" + this.gender);
        }
    
    }
    package oop5;
    
    import java.util.Scanner;
    
    public class Testpenguin {
            static Scanner sc =  new Scanner(System.in);
        public static void main(String[] args) {
            
            Penguin t = new Penguin();
            System.out.print("欢迎来到宠物店!
    "+"请输入要领养宠物的名字:");
            String name = sc.next();
            t.setName(name);
            System.out.print("请选择要领养的宠物类型:(1.狗狗  2. 企鹅)");
            int leiXin = sc.nextInt();
            t.setLeiXin(leiXin);
            System.out.print("请选择企鹅的性别(1.Q仔  2.Q妹):");
            String gender = sc.next();
            t.setGender(gender);
            System.out.print("请输入企鹅的健康值(1~100之间):");
            int health = sc.nextInt();
            t.setHealth(health);
            t.show();
            
            
        }
    }

    package FuXi;
    
    public class Voter {
    
        
        private String name;
        
        
        private static int count;
        
        private static final int MAX_COUNT = 100;
        
        //有参的构建方法
        public Voter(String name) {
            
            this.name = name;
        }
        
        public void setName(String name) {
            
            this.name = name;
        }
        
        public String getname() {
    
            return this.name;
            
        }
        
        public static void setCount(int count) {
            Voter.count = count;
        }
        public static int getCount() {
            return count;
        }
    
        
        public static int getMaxCount() {
            return MAX_COUNT;
        }
        
    
        //投票
        public void voter() {
            
            if(count==MAX_COUNT) {
                
                System.out.println("投票已经结束!");
                return;
                
            }else {
                
                count++;
                System.out.println(this.name+"投票成功!");
                return;
            }
            
        }
         public static void main(String[] args) {
            
             for(int i = 0; i<100; i++) {
             Voter s = new Voter("v"+(i+1));
             s.voter();
             }
             
             
             
        }
        
        
    }

  • 相关阅读:
    The Network Adapter could not establish the connection问题研究
    条件更新或插入
    如何解决Oracle临时表空间过大
    oracle的临时表空间写满磁盘空间解决改问题的步骤
    如何在oracle中缩小临时表空间?ORA-01652无法在表空间中扩展temp
    oracle mysql sql 根据一张表更新另一张表
    Don’t Repeat Yourself,Repeat Yourself
    mysql oracle sql获取树 父级 子级 及自己
    hutool BigExcelWriter 下的autoSizeColumnAll异常问题
    Java手写数组栈
  • 原文地址:https://www.cnblogs.com/bichen-01/p/11173333.html
Copyright © 2011-2022 走看看