本文主要是介绍1)封装一个西游记人物类Person:包含私有属性:姓名(name)、身高(height)、武器(weapon)、种族(race),并提供访问器(get方法)和修改器(set方法)(2)在测试类中实,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//1)封装一个西游记人物类Person:包含私有属性:姓名(name)、身高(height)、武器(weapon)、种族(race),并提供访问器(get方法)和修改器(set方法)
//2)在测试类中实例化三个对象:tangseng(唐僧)、sunwukong(孙悟空)、baigujing(白骨精),分别设置他们的种族为:人族、仙族、妖族
//3)在打印对象时,按照例如:“姓名:孙悟空;种族:仙族”的格式在控制台输出信息。
//4)提供一个技能方法void skill(),这个方法根据当前对象name属性判断技能,如果是孙悟空,就打印“七十二变”,如果是唐僧,就打印“紧箍咒”,如果是白骨精,
//就打印“九阴白骨爪”,在测试类中调用这个方法。
//5)重写equals方法进行判断,如果种族属性是人族或者仙族,则返回true,如果是妖族,则返回false,在测试类中对孙悟空和唐僧进行比较,打印结果,对唐僧和白骨金进行比较,并打印结果。
public class main1
{public static void main(String[] args){Person1 person1 = new Person1("孙悟空", 185, "金箍棒", "仙族", "七十二变");Person1 person2 = new Person1("唐僧", 178, "权杖", "人族", "紧箍咒");Person1 person3 = new Person1("白骨精", 177, "骨头", "妖族", "九阴白骨爪");person1.show();person1.skill();System.out.println("******************");person2.show();person2.skill();System.out.println("******************");person3.show();person3.skill();System.out.println("******************");System.out.println(person1.getRaceString().equals(person2.getRaceString()));}
}class Person1
{private String nameString;private double height;private String weaponString;private String raceString;private String skillString;public boolean equals(Object object){if (this == object)return true;if (object instanceof Person1){Person1 person1 = (Person1) object;if (this.raceString.equals(person1.raceString))return true;else{return false;}}return false;}protected Person1(String nameString, double height, String weaponString, String raceString, String skillString){super();this.nameString = nameString;this.height = height;this.weaponString = weaponString;this.raceString = raceString;this.skillString = skillString;}public void skill(){System.out.println("技能:" + skillString);}public void show(){System.out.println("姓名:" + nameString + "\n种族:" + raceString);}public String getNameString(){return nameString;}public void setNameString(String nameString){this.nameString = nameString;}public double getHeight(){return height;}public void setHeight(double height){this.height = height;}public String getWeaponString(){return weaponString;}public void setWeaponString(String weaponString){this.weaponString = weaponString;}public String getRaceString(){return raceString;}public void setRaceString(String raceString){this.raceString = raceString;}}
这篇关于1)封装一个西游记人物类Person:包含私有属性:姓名(name)、身高(height)、武器(weapon)、种族(race),并提供访问器(get方法)和修改器(set方法)(2)在测试类中实的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!