书摘

在此处输入文本

  • 你将读到的是北野武的出身、父母、兄弟和家庭的故事。
  • 但请放心,这绝非一个自我感觉良好的人写的那种“优良课外读物”。很喜欢这句←方便打标签的状态卡片
  • 相反,北野武用搞笑到甚至刻薄方式描绘这些人和事,让我们在笑与泪的交织中,看到真情和真实,看到那么多情、柔软的心。

数组

  • 声明语法

DataType[] name 或 DataType name[]。

  • 初始化语法

DataType[] name = { element1, element2, …elementn }。

  1. String[] strs = { "段", "光", "伟" };
  2. for (String item : strs) {
  3. System.out.print(item);
  4. }
  5. StringBuffer sb = new StringBuffer().append("段").append("光")

枚举

  1. public final class Program {
  2. public static void main(String[] args) {
  3. System.out.println(State.ON);
  4. System.out.println(State.OFF);
  5. for (State item : State.values()) {
  6. System.out.println(item);
  7. System.out.println(State.valueOf(item.name()));
  8. }
  9. }
  10. }
  11. enum State {
  12. ON(1), OFF(0);
  13. int value = 1;
  14. State(int value) {
  15. this.value = value;
  16. }
  17. }

  1. //简单的类
  2. class Point {
  3. private int x = 0;
  4. private int y = 0;
  5. public Point(int x, int y) {
  6. this.x = x;
  7. this.y = y;
  8. }
  9. public Point(int x) {
  10. this(x, x);
  11. }
  12. public String toString() {
  13. return "(x:" + this.x + ",y:" + this.y + ")";
  14. // System.out.print(point);
  15. }
  16. }
  17. //继承
  18. public class Program {
  19. /**
  20. * @param args
  21. */
  22. public static void main(String[] args) {
  23. Animal animal = new Animal();
  24. Animal dog = new Dog();
  25. animal.say();
  26. dog.say();
  27. animal.eat(animal);
  28. dog.eat(dog);
  29. System.out.println(animal.info());
  30. System.out.println(dog.info());
  31. }
  32. }
  33. class Animal {
  34. private String name = "Animal";
  35. protected void say() {
  36. System.out.println("Animal" + " " + this.name);
  37. }
  38. public void eat(Animal food) {
  39. System.out.println("Animal eat " + food);
  40. }
  41. public Object info() {
  42. return "Animal";
  43. }
  44. @Override
  45. public String toString() {
  46. return "Animal";
  47. }
  48. }
  49. class Dog extends Animal {
  50. private String name = "Dog";
  51. @Override
  52. public final void say() {
  53. System.out.println("Dog" + " " + this.name);
  54. }
  55. @Override
  56. public final void eat(Animal food) {
  57. super.eat(food);
  58. System.out.println("Dog eated");
  59. }
  60. @Override
  61. public final String info() {
  62. return "Dog";
  63. }
  64. @Override
  65. public final String toString() {
  66. return "Dog";
  67. }
  68. }

异常

  1. public final class Program {
  2. public static void main(String[] args) {
  3. try {
  4. test();
  5. } catch (Exception e) {
  6. System.out.println(e.getMessage());
  7. }
  8. }
  9. public static void test() throws Exception {
  10. throw new Exception("I am wrong!");
  11. }
  12. }

泛型

泛型方法

  1. static <T> void puts(T msg) {
  2. println(msg);
  3. }
  4. static void println(Object msg) {
  5. System.out.println("Object:" + msg);
  6. }
  7. static void println(String msg) {
  8. System.out.println("String:" + msg);
  9. }
  10. puts("hello");
  11. Program.<String> puts("hello");

泛型类

  1. class TestGenericClass<T> {
  2. T value;
  3. void setValue(T value) {
  4. this.value = value;
  5. }
  6. }

Properties

写入文件

  1. public void writeProperties() {
  2. Properties properties = new Properties();
  3. OutputStream output = null;
  4. try {
  5. output = new FileOutputStream("config.properties");
  6. properties.setProperty("url", "jdbc:mysql://localhost:3306/");
  7. properties.setProperty("username", "root");
  8. properties.setProperty("password", "root");
  9. properties.setProperty("database", "users");//保存键值对到内存
  10. properties.store(output, "Steven1997 modify" + new Date().toString());
  11. // 保存键值对到文件中
  12. } catch (IOException io) {
  13. io.printStackTrace();
  14. } finally {
  15. if (output != null) {
  16. try {
  17. output.close();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. }
  23. }

加载

  1. private static String basePath = "src/main/java/cn/habitdiary/prop.properties";
  2. private static String path = "";
  3. public static String getPath1() {
  4. try {
  5. InputStream in = new BufferedInputStream(new FileInputStream(
  6. new File(basePath)));
  7. Properties prop = new Properties();
  8. prop.load(in);
  9. path = prop.getProperty("path");
  10. } catch (FileNotFoundException e) {
  11. System.out.println("properties文件路径书写有误,请检查!");
  12. e.printStackTrace();
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }
  16. return path;
  17. }
  18. /**
  19. * 二、 使用java.util.ResourceBundle类的getBundle()方法
  20. * 注意:这个getBundle()方法的参数只能写成包路径+properties文件名,否则将抛异常
  21. *
  22. * @return
  23. */
  24. public static String getPath2() {
  25. ResourceBundle rb = ResourceBundle
  26. .getBundle("cn/habitdiary/prop");
  27. path = rb.getString("path");
  28. return path;
  29. }
  30. /**
  31. * 三、 使用java.util.PropertyResourceBundle类的构造函数
  32. */
  33. public static String getPath3() {
  34. InputStream in;
  35. try {
  36. in = new BufferedInputStream(new FileInputStream(basePath));
  37. ResourceBundle rb = new PropertyResourceBundle(in);
  38. path = rb.getString("path");
  39. } catch (FileNotFoundException e) {
  40. // TODO Auto-generated catch block
  41. e.printStackTrace();
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. return path;
  46. }
  47. /**
  48. * 四、 使用class变量的getResourceAsStream()方法
  49. * 注意:getResourceAsStream()方法的参数按格式写到包路径+properties文件名+.后缀
  50. */
  51. public static String getPath4() {
  52. InputStream in = LoadPropertiesFileUtil.class
  53. .getResourceAsStream("cn/habitdiary/prop.properties");
  54. //五、使用class.getClassLoader()所得到的java.lang.ClassLoader的
  55. InputStream in = LoadPropertiesFileUtil.class.getClassLoader()
  56. .getResourceAsStream("cn/habitdiary/prop.properties");
  57. //六、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
  58. InputStream in = ClassLoader
  59. .getSystemResourceAsStream("cn/habitdiary/prop.properties");
  60. Properties p = new Properties();
  61. try {
  62. p.load(in);
  63. path = p.getProperty("path");
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. }
  67. return path;
  68. }

遍历

  1. public static void printProp(Properties properties) {
  2. System.out.println("---------(方式一)------------");
  3. for (String key : properties.stringPropertyNames()) {
  4. System.out.println(key + "=" + properties.getProperty(key));
  5. }
  6. System.out.println("---------(方式二)------------");
  7. Set<Object> keys = properties.keySet();//返回属性key的集合
  8. for (Object key : keys) {
  9. System.out.println(key.toString() + "=" + properties.get(key));
  10. }
  11. System.out.println("---------(方式三)------------");
  12. Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();
  13. //返回的属性键值对实体
  14. for (Map.Entry<Object, Object> entry : entrySet) {
  15. System.out.println(entry.getKey() + "=" + entry.getValue());
  16. }
  17. System.out.println("---------(方式四)------------");
  18. Enumeration<?> e = properties.propertyNames();
  19. while (e.hasMoreElements()) {
  20. String key = (String) e.nextElement();
  21. String value = properties.getProperty(key);
  22. System.out.println(key + "=" + value);
  23. }
  24. }