- class Monster
- {
- private String skill;//private修饰隐藏起来
- private String skill2;
- private int num;
- //构造代码块是给所有对象进行初始化的,他表达的是不用对象的共性内容,优先于构造函数执行
- {
- System.out.println("Cry");//无论是哪个怪物诞生都会哭
- }
- Monster(String skill)
- {
- System.out.println("monster1只会"+skill);
- }
- Monster(String skill,String skill2)
- {
- System.out.println("Monster2不仅会"+skill+"还会"+skill2);
- }
- Monster(String skill, int num)
- {
- System.out.println("Monster3不仅会"+skill+"还有"+num+"只脚");
- }
- //存够技能点是可以升级的,二话不说,对外提供访问方式升级技能;
- public void setNum(int num)
- {
- if(num>0)
- this.num=num;
- else
- System.out.println("非法的输入");
- }
- public int getNum()
- {
-
- return num;
- }
- public void setNewSkill(String skill)
- {
- this.skill=skill;
- }
- public String getNewSkill()
- {
- return skill;
- }
-
- public void setNewSkill_2( String skill2)
- {
- this.skill2=skill2;
- }
- public String getNewSkill_2()
- {
- return skill2;
- }
-
- }
- class GouZao
- {
- public static void printHr()
- {
- System.out.println("------------------------");
- }
- public static void main(String[] args)
- {
- Monster m1= new Monster("喷火");
- Monster m2= new Monster("喷火","飞天");
- Monster m3= new Monster("喷火",6);
- printHr();
- //如果monster1以后又学到新技能,大招怎么办?这时就需要调用一般方法
- m1.setNewSkill("大招:范围群火");
- System.out.println("monster1习得新技能:"+m1.getNewSkill());
- }
- }
复制代码
|
|