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("宠物的自白:\n" + "我的名字叫豆豆,健康值是" + 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("欢迎来到宠物店!\n"+"请输入要领养宠物的名字:"); 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(); } } }