使用封装实现企鹅类

package com.oned6z;
/**
* @program: com.oned6z
* @description:  狗狗类
* @author: Mr.Lin
* @create: 2019年7月15日
**/
public class Dog {
private String name ; //昵称,默认值是无名氏
private String type;
private String sex;
private int love ; //亲密度,默认值是100,健康值在1-100之间,小于60为不亲密
private int health ;//健康值,默认值是100,健康值在1-100之间,小于60为不健康

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//为private的属性加一对setter/getter
public void setHealth(int health) {

if(health>100||health<0) {
//如果健康值赋值不正确,强制赋值60,同时给用户一个提示,然后结束方法
System.out.println("健康值应该在0~100之间,默认值为60.");
health = 60;

}
//如果健康值赋值正确,则可直接赋值
this.health = health;
}
public int getHealth() {
return health;
}

public void setLove(int love) {
while(love<0||love>100) {
//如果健康值赋值不正确,强制赋值60,同时给用户一个提示,然后结束方法
System.out.println("亲密度应该在0~100之间,默认值为60.");
love = 60;

}
//如果健康值赋值正确,则可直接赋值
this.love = love;
}
public int getLove() {
return love;
}

/**
* 输出企鹅的信息
*/

public void confessions() {
System.out.println("宠物的自白: "+"我的名字叫"+getName()+",健康值是"+getHealth()
+",和主人的亲密度是"+getLove()+",我的性别是"+getSex());

}

}

package com.oned6z;
/**
* @program: com.oned6z
* @description: 企鹅类
* @author: Mr.Lin
* @create: 2019年7月11日
**/
public class Pg {
private String name ; //昵称,默认值是无名氏
private String type;
private String sex;
private int love ; //亲密度,默认值是100,健康值在1-100之间,小于60为不亲密
private int health ;//健康值,默认值是100,健康值在1-100之间,小于60为不健康

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//为private的属性加一对setter/getter
public void setHealth(int health) {

if(health>100||health<0) {
//如果健康值赋值不正确,强制赋值60,同时给用户一个提示,然后结束方法
System.out.println("健康值应该在0~100之间,默认值为60.");
health = 60;

}
//如果健康值赋值正确,则可直接赋值
this.health = health;
}
public int getHealth() {
return health;
}

public void setLove(int love) {
while(love<0||love>100) {
//如果健康值赋值不正确,强制赋值60,同时给用户一个提示,然后结束方法
System.out.println("亲密度应该在0~100之间,默认值为60.");
love = 60;

}
//如果健康值赋值正确,则可直接赋值
this.love = love;
}
public int getLove() {
return love;
}

/**
* 输出企鹅的信息
*/

public void confessions() {
System.out.println("宠物的自白: "+"我的名字叫"+this.getName()+",健康值是"+this.getHealth()
+",和主人的亲密度是"+this.getLove()+",我的性别是"+this.getSex());

}

}

package com.oned6z;

import java.util.Scanner;

/**
* @program: com.oned6z
* @description: 测试类
* @author: Mr.Lin
* @create: 2019年7月13日
**/
public class TestQq {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("欢迎来到宠物店!");
System.out.print("请输入要领养的宠物的名字:");
String name = sc.next();
Pg pg = new Pg();
pg.setName(name);
Dog dog = new Dog();
dog.setName(name);


System.out.print("请选择要领养的宠物类型:(1、狗狗 2、企鹅)");
String type = sc.next();
dog.setType(type);
pg.setType(type);

switch(type) {
case"1":
System.out.print("请选择狗狗的性别:(1、G仔 2、G妹)");
String choose = sc.next();
if("1".equals(choose)) {
dog.setSex("G仔");
}else {
dog.setSex("G妹");
}
System.out.print("请输入狗狗的健康值(1~100之间):");
int health= sc.nextInt();
dog.setHealth(health);
dog.confessions();
break;
default:
System.out.print("请选择企鹅的性别:(1、Q仔 2、Q妹)");
String gender = sc.next();
if("1".equals(gender)) {
pg.setSex("Q仔");
}else {
pg.setSex("Q妹");
}
System.out.print("请输入企鹅的健康值(1~100之间):");
int healthIndex= sc.nextInt();
pg.setHealth(healthIndex);
pg.confessions();
break;
}
sc.close();
}


}

原文地址:https://www.cnblogs.com/lpbk/p/11193590.html