2017-02-27-this关键字和权限修饰符
学了这么久的Java,竟然不知道在非主方法中竟然可以互相调用而不用先创建对象再来调用,一直以为在面向对象中对方法的调用只能通过对象来调用,c和c++的后代还是支持这种神奇的流程控制。
实例1:
一个动物园 拥有不同的动物:
*
* 人可以选择参观什么动物(4种以上)
*
* 如果是熊猫 则显示 年龄,性别,名字,食物
*
* 如果是蛇 则显示 地区,是否有毒,名字,食物
*
* 如果是虎 则显示 地区,食物
思路:面向对象,流程控制,构造方法,this关键字
核心代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| public class Start1 { public static void main(String[] args) { System.out.println("请输入人的名字"); Scanner sc = new Scanner(System.in); String name = sc.next();
Person person = new Person(name);
Animal animal = null; while(true){ System.out.println("选择您要参观的动物类型:【1】熊猫【2】蛇【3】老虎【乱输】退出"); String inputAnimal=sc.next(); switch (inputAnimal) { case "1": animal = new Animal("熊猫", 2, "公", "竹子"); break; case "2": animal = new Animal("蛇", "有毒", "老鼠"); break; case "3": animal = new Animal("东北", "山羊"); break;
default: System.out.println("您已经退出"); return; } person.visit(animal); } } }
public class Animal { public String name; public int age; public String sex; public String food; public String poison; public String area; public Animal(){ } public Animal(String name,int age,String sex,String food){ this.name=name; this.age=age; this.sex=sex; this.food=food; } public Animal(String name,String posion,String food){ this.name=name; this.poison=posion; this.food=food; } public Animal(String area,String food){ this.name="老虎"; this.area=area; this.food=food; } }
public class Person { public String name; public Person(){ } public Person(String name){ this.name=name; } public void visit(Animal animal){ System.out.println(this.name+"参观了"+animal.name); } }
|
实例2:
泡茶
思路:同上例
this关键字的用途:【1】代指当前对象,【2】调用构造方法,this(无参或有参);
核心代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| public class Start { public static void main(String[] args) { System.out.println("请选择您要选择的杯子:【1】玻璃杯【2】陶瓷杯【3】盖碗【乱输】尿壶。"); Scanner sc=new Scanner(System.in); int inputCup=sc.nextInt(); Cup cup=null; switch (inputCup) { case 1: cup=new Cup("玻璃杯","大"); break; case 2: cup=new Cup("陶瓷杯","中"); break; case 3:
default: cup=new Cup("尿壶","特大"); break; } System.out.println("请选择你要选择的茶叶:【1】茉莉花茶【2】普洱【3】龙井【乱输】马尿"); int inputTea=sc.nextInt(); Tea tea=null; switch (inputTea) { case 1: tea=new Tea("茉莉花茶","清香的"); break; case 2: tea=new Tea("普洱","醇厚的"); break; case 3: tea=new Tea("龙井","芬芳的"); break;
default: tea=new Tea("马尿","刺鼻的"); break; } cup.loadTea(tea); }
} public class Cup { public String name; public Tea tea; public String size;
public Cup(){ } public Cup(String name){ this.name=name; } public Cup(String name,String size){ this.name=name; this.size=size; } public Cup(String name,Tea tea){ this(name); this.tea=tea; } public Cup(String name, Tea tea,String size){ this(name,tea); this.size=size; } public void loadTea(Tea tea){ this.tea=tea; System.out.println("您用"+this.size+this.name+"泡了一杯"+this.tea.taste+"的"+this.tea.name); } }
public class Tea { public String name; public String taste; public Tea(){ } public Tea(String name){ this.name=name; } public Tea(String name,String taste){ this(name); this.taste=taste; } }
|
实例2:
1.以面向对象的方式来写这个游戏;该封装的东西需要封装 2.用户至少可以有3种角色的选择,每种角色的初始值不一样; 血量,蓝量,攻击,防御,经验;
* 3.有怪物。。。。怪物要有boos 。。。。; 我们进入游戏以后, 让用户选择角色,创建角色; 然后问用户是否确定 如果确定:欢迎来到**游戏;
* 然后给一个提示:输入HELP可以查看帮助,输入quit可以退出游戏,输入rest可以休息 输入look可以查看自身属性
* 输入f可以战斗;跟怪物战斗:怪物是随机的; 战斗就是回合制战斗: 直达一方死亡战斗结束; 如果人胜利了那么经验增加。。。; 到了一定的值就升级;
* 升级以后属性变化;
核心代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
| public class Start3 { public static void main(String[] args) { System.out.println("欢迎来到【光明建设鬼】(致敬RPG神作《暗黑破坏神(Diablo)》)"); String inputCareer = ""; Scanner sc = new Scanner(System.in);
while (true) { System.out.println("请选择您要创建的角色的职业:【1】法师【2】刺客【3】战士【4】圣骑"); inputCareer = sc.next(); if (inputCareer.equals("1") || inputCareer.equals("2") || inputCareer.equals("3") || inputCareer.equals("4")) { break; } else { System.out.println("创建角色失败,请确认输入为1,2,3,4中的任意一个"); } } Player player = new Player(inputCareer);
boolean flag = true; while (flag) {
System.out.println("输入指令获取相应操作,【h】help:查看帮助;【q】quit:退出游戏;【r】rest:休息;【l】look:查看自身属性;【f】fight:进入战斗。"); String inputOrder = sc.next(); switch (inputOrder) { case "h": player.help(); break; case "q": flag = false; System.out.println("您已经退出游戏,欢迎再次光临!"); break; case "r": player.rest(); break; case "l": player.look(); break; case "f": Monster monster=new Monster(); System.out.println("您进入了光明的世界,然后遇到了【"+monster.getName()+"】"); if(player.fight(monster)){ System.out.println(player.getCareer()+"胜利了"); }else{ System.out.println(player.getCareer()+"死亡了"); } break;
default: break; } } } }
public class Player { private String career; private int hp; private int mp; private int at; private int de; private int xp; private int le;
public String getCareer() { return career; }
public void setCareer(String career) { this.career = career; }
public int getHp() { return hp; }
public void setHp(int hp) { this.hp = hp; }
public int getMp() { return mp; }
public void setMp(int mp) { this.mp = mp; }
public int getAt() { return at; }
public void setAt(int at) { this.at = at; }
public int getDe() { return de; }
public void setDe(int de) { this.de = de; }
public int getXp() { return xp; }
public void setXp(int xp) { this.xp = xp; }
public int getLe() { return le; }
public void setLe(int le) { this.le = le; }
public Player(String input) { switch (input) { case "1": this.career = "法师"; this.hp = 8; this.mp = 9; this.at = 8; this.de = 4; this.xp = 1; this.le = 1; break;
case "2": this.career = "刺客"; this.hp = 8; this.mp = 5; this.at = 7; this.de = 5; this.xp = 1; this.le = 1; break; case "3": this.career = "战士"; this.hp = 10; this.mp = 5; this.at = 3; this.de = 8; this.xp = 1; this.le = 1; break; case "4": this.career = "圣骑"; this.hp = 9; this.mp = 8; this.at = 4; this.de = 7; this.xp = 1; this.le = 1; break;
default: break; } }
public void help() { System.out.println("本游戏为回合制RPG(role play game ,角色扮演游戏),在dos命令窗口上输入相应命令进行操作,玩法为文字冒险RPG。"); }
public void rest() { System.out.println("进入休息状态"); System.out.println("......."); switch (this.career) { case "法师": this.hp=8+this.le-1; break; case "刺客": this.hp=8+this.le-1; break; case "战士": this.hp=10+this.le-1; break; case "圣骑": this.hp=9+this.le-1; break; default: break; } System.out.println("血量已经回满。"); System.out.println("休息完毕!"); }
public void look() { System.out.println("您的角色属性:"); System.out.println("血量:" + this.getHp()); System.out.println("蓝量:" + this.getMp()); System.out.println("攻击:" + this.getAt()); System.out.println("防御:" + this.getDe()); System.out.println("经验:" + this.getXp()); System.out.println("等级:" + this.getLe()); }
public boolean fight(Monster monster) { boolean flag=true; int count=0; boolean stop=true; while (stop) { count++; System.out.println("进入第"+count+"回合"); System.out.println(monster.getName() + "先发起了攻击"); System.out.println(monster.getName() + "对" + this.career + "造成了" + monster.getAt() + "点伤害"); this.hp = this.hp -monster.getAt(); System.out.println(this.career+"的血量变为"+this.hp); if (this.hp <= 0) { this.hp=0; System.out.println("对不起,怪物太凶残,您的等级不够,被虐了。请先休息回满血再战斗吧"); flag=false; stop=false; return flag; }
System.out.println("接下来" + this.career + "发起了攻击"); System.out.println(this.career + "对" + monster.getName() + "造成了" + this.at + "点伤害");
monster.setHp(monster.getHp() -this.at); System.out.println(monster.getName()+"的血量变为:"+monster.getHp()); if ((monster.getHp()) <= 0) { this.xp = this.xp + monster.getValue(); System.out.println("战斗胜利了" + this.career + "经验值增加了" + monster.getValue()+"当前的等级为"+this.le); if ((this.xp / 10) > this.le) { switch (this.career) { case "法师": this.hp=8+this.le-1; break; case "刺客": this.hp=8+this.le-1; break; case "战士": this.hp=10+this.le-1; break; case "圣骑": this.hp=9+this.le-1; break; default: break; } this.le++; this.hp++; this.mp++; this.at++; this.de++; System.out.println(this.career + "升到" + this.le + "级了,各属性都增加1"); } stop=false;
}
} return flag; }
}
public class Monster { private String name; private String type; private int hp; private int at; private int de; private int value;
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 int getHp() { return hp; }
public void setHp(int hp) { this.hp = hp; }
public int getAt() { return at; }
public void setAt(int at) { this.at = at; }
public int getDe() { return de; }
public void setDe(int de) { this.de = de; }
public int getValue() { return value; }
public void setValue(int value) { this.value = value; }
public Monster() { int input = (int) (Math.random() * 5); switch (input) { case 1: this.name = "骷髅"; this.type = "小怪"; this.hp = 1; this.at = 1; this.de = 1; this.value = 1; break; case 2: this.name = "僵尸"; this.type = "小怪"; this.hp = 1; this.at = 1; this.de = 3; this.value = 3; break; case 3: this.name = "食尸鬼"; this.type = "小怪"; this.hp = 1; this.at = 1; this.de = 4; this.value = 4; break; case 4: this.name = "野兽"; this.type = "小怪"; this.hp = 2; this.at = 2; this.de = 5; this.value = 5; break; case 5: this.name = "狼人"; this.type = "精英怪"; this.hp = 3; this.at = 3; this.de = 7; this.value = 7; break; case 0: this.name = "吸血鬼"; this.type = "boss怪"; this.hp = 7; this.at = 7; this.de = 16; this.value = 8; break; default: break; } }
}
|