2017-04-05-JavaSE整体复习 知识点1:
static修饰类:当且仅当内部类中的成员(属性或方法)中为static的。
知识点2:
{ }代码块先于当前类的构造方法执行,并且每次构造对象时都会加载,故又被称为构造代码块。
调用方法都是对象调用方法,在本类中可以不写,但是仍然有默认的“this.”。
延迟绑定只存在方法中,和父类属性重名的属性是不能够被访问的。
程序阅读题代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class SuperClass { public SuperClass () { show(); System.out.println("SuperClass Constructor " ); } public void show () { System.out.println("SuperClass show()" ); } { System.out.println("SuperClass function block" ); } static { System.out.println("SuperClass static function block " ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class SubClass extends SuperClass { public int i; public SubClass () { System.out.println("SubClass Constructor i= " + i); } @Override public void show () { System.out.println("SubClass show()" + i); } { System.out.println("enter function block i= " + i); i = 1 ; System.out.println("before out function block i= " + i); } static { System.out.println("SubClass static function block " ); } }
1 2 3 4 5 6 7 8 9 public class Test { public static void main (String[] args) { SubClass sub=new SubClass (); sub.show(); System.out.println("------------------------" ); sub=new SubClass (); sub.show(); } }
答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 SuperClass static function block SubClass static function block SuperClass function block SubClass show () 0 SuperClass Constructor enter function block i= 0 before out function block i= 1 SubClass Constructor i= 1 SubClass show () 1 ------------------------ SuperClass function block SubClass show () 0 SuperClass Constructor enter function block i= 0 before out function block i= 1 SubClass Constructor i= 1 SubClass show () 1
知识点3:
封装性:
1.访问属性控制
2.更改方便,如果改变变量名,只需要改变get/set方法中的变量就行了,不用改变get/set方法的方法名,适用于大批量修改。
在get/set方法中对属性进行控制,不直接对数据操作避免了误操作,保证了数据的安全。
构造方法的初始化对象,不仅是将成员变量赋初值,更重要的是创建成员变量的空间。
this()在第一行,因为在调用this()的构造方法中失去了初始化对象即创建成员变量空间的能力。
判断方法加载的次数很费空间,所以不用判定加载的判断方法来限制。
不能直接调用super,因为子类创建的过程中只是将父类的属性加载了一遍,并没有创建父类的对象。
实例1:
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 package com.share.demo04_05_JavaSE整体复习;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Scanner;public class Game { private Scanner sc = new Scanner (System.in); private User user = new User (); private Career career = null ; public Scanner getSc () { return sc; } public void setSc (Scanner sc) { this .sc = sc; } public User getUser () { return user; } public void setUser (User user) { this .user = user; } public Career getCareer () { return career; } public void setCareer (Career career) { this .career = career; } public static void main (String[] args) { Game game = new Game (); game.StreamControl(); } public void StreamControl () { System.out.println("欢迎来到xxx游戏" ); System.out.println("请选择:1、登录, 2(或其他)、注册" ); if (!"1" .equals(sc.next())) { regist(); } login(); System.out.println("请选择:1、读档;2、新游戏" ); if ("1" .equals(sc.next())) { loadFile(); } else { newGame(); } startGame(); } public void login () { String name = "" ; String password = "" ; boolean flag = true ; do { System.out.println("欢迎来到登录界面:" ); System.out.println("请输入用户名:" ); name = sc.next(); System.out.println("请输入密码:" ); password = sc.next(); if (name.equals(user.getName())) { if (password.equals(user.getPassword())) { System.out.println("登录成功" ); flag = false ; } else { System.out.println("密码输入错误,请重新登录" ); } } else { System.out.println("用户名输入错误,请重新登录" ); } } while (flag); } public void regist () { System.out.println("欢迎来到注册界面:" ); System.out.println("请输入用户名:" ); user.setName(sc.next()); System.out.println("请输入密码:" ); user.setPassword(sc.next()); System.out.println("注册成功" ); } public void loadFile () { File file = new File ("E:\\1GameSave" ); File[] fileArray=file.listFiles(); for (int i=0 ;i<fileArray.length;i++){ System.out.println(i+"、 " +fileArray[i].getName()); } System.out.println("请输入要加载的存档:" ); file=new File (file,fileArray[sc.nextInt()].getName()); ObjectInputStream ois = null ; try { ois = new ObjectInputStream (new FileInputStream (file)); career = (Career) ois.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (ois != null ) { ois.close(); } } catch (IOException e) { e.printStackTrace(); } } System.out.println("读档完成" ); } public void saveFile () { File file = new File ("E:\\1GameSave" ); SimpleDateFormat sdf=new SimpleDateFormat ("HH-mm-ss" ); file=new File (file,sdf.format(new Date ())+".txt" ); ObjectOutputStream oos = null ; try { oos = new ObjectOutputStream (new FileOutputStream (file)); oos.writeObject(career); } catch (IOException e) { e.printStackTrace(); } finally { try { if (oos != null ) { oos.flush(); oos.close(); } } catch (IOException e) { e.printStackTrace(); } } System.out.println("存档成功" ); } public void newGame () { System.out.println("选择你要选择的角色:1、Role01;2、Role02;3、Role03(乱输则默认为此选项)" ); String input = sc.next(); switch (input) { case "1" : career = new Role01 (user.getName()); break ; case "2" : career = new Role02 (user.getName()); break ; default : career = new Role03 (user.getName()); break ; } MyThread myThread=new MyThread (); myThread.start(); } public void look () { System.out.println(career); } public void buyMedicine () { System.out.println("请选择:1、红药;2、蓝药(默认)" ); if ("1" .equals(sc.next())) { career.getList().add(new Medicine ("红药" )); } else { career.getList().add(new Medicine ("蓝药" )); } } public void changeRoleName () { System.out.println("请输入名字" ); career.setName(sc.next()); } public void startGame () { while (true ) { System.out.println("请输入指令:" ); switch (sc.next()) { case "help" : System.out.println("输入help可以查看帮助文档(指令),输入quit可以退出游戏," ); System.out.println("输入save可以进行存档,输入read可以进行读档," ); System.out.println("输入lookme可以查看自身属性( 包括背包),输入xiugai可以改名,输入shop可以购物" ); break ; case "quit" : return ; case "save" : saveFile(); break ; case "read" : loadFile(); break ; case "lookme" : look(); break ; case "xiugai" : changeRoleName(); break ; case "shop" : buyMedicine(); break ; default : System.out.println("输入help可以查看帮助文档(指令),输入quit可以退出游戏," ); System.out.println("输入save可以进行存档,输入read可以进行读档," ); System.out.println("输入lookme可以查看自身属性( 包括背包),输入xiugai可以改名,输入shop可以购物" ); break ; } } } class MyThread extends Thread { @Override public void run () { while (true ){ saveFile(); try { sleep(30 *1000 ); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
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 package com.share.demo04_05_JavaSE整体复习;import java.io.Serializable;import java.util.ArrayList;import java.util.List;public class Career implements Serializable { private static final long serialVersionUID = -9067957490114301061L ; private String name; private final int age = 16 ; private double HP; private List<Medicine> backpack=new ArrayList <Medicine>(); public Career () { } public Career (String name) { this .name = name; } public String getName () { return name; } public List<Medicine> getList () { return backpack; } public void setList (List<Medicine> list) { this .backpack = list; } public void setName (String name) { this .name = name; } public double getHP () { return HP; } public void setHP (double hP) { HP = hP; } public int getAge () { return age; } @Override public String toString () { return "名字: " +name+" 年龄: " +age+" HP:" +HP+" 背包物品:" +backpack; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.share.demo04_05_JavaSE整体复习;public class Role01 extends Career { private static final long serialVersionUID = -4126401490917966688L ; public Role01 (String name) { super (name); this .setHP(1000.0 ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.share.demo04_05_JavaSE整体复习;public class Role02 extends Career { private static final long serialVersionUID = 762115351289469620L ; public Role02 (String name) { super (name); this .setHP(900.0 ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.share.demo04_05_JavaSE整体复习;public class Role03 extends Career { private static final long serialVersionUID = -8516365521781098755L ; public Role03 (String name) { super (name); this .setHP(800.0 ); } }
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 package com.share.demo04_05_JavaSE整体复习;public class User { private String name; private String password; public User () { this .name = "asd" ; this .password = "12345" ; } public User (String name, String password) { this .name = name; this .password = password; } public String getName () { return name; } public void setName (String name) { this .name = name; } public String getPassword () { return password; } public void setPassword (String password) { this .password = password; } }
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 package com.share.demo04_05_JavaSE整体复习;import java.io.Serializable;public class Medicine implements Serializable { private static final long serialVersionUID = 7054650668973818462L ; private String name; public Medicine (String name) { super (); this .name = name; } public String getName () { return name; } public void setName (String name) { this .name = name; } @Override public String toString () { return name; } }