书摘
在此处输入文本
- 你将读到的是北野武的出身、父母、兄弟和家庭的故事。
- 但请放心,这绝非一个自我感觉良好的人写的那种“优良课外读物”。很喜欢这句←方便打标签的状态卡片
- 相反,北野武用搞笑到甚至刻薄方式描绘这些人和事,让我们在笑与泪的交织中,看到真情和真实,看到那么多情、柔软的心。
数组
- 声明语法
DataType[] name 或 DataType name[]。
- 初始化语法
DataType[] name = { element1, element2, …elementn }。
String[] strs = { "段", "光", "伟" };
for (String item : strs) {
System.out.print(item);
}
StringBuffer sb = new StringBuffer().append("段").append("光")
枚举
public final class Program {
public static void main(String[] args) {
System.out.println(State.ON);
System.out.println(State.OFF);
for (State item : State.values()) {
System.out.println(item);
System.out.println(State.valueOf(item.name()));
}
}
}
enum State {
ON(1), OFF(0);
int value = 1;
State(int value) {
this.value = value;
}
}
类
//简单的类
class Point {
private int x = 0;
private int y = 0;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point(int x) {
this(x, x);
}
public String toString() {
return "(x:" + this.x + ",y:" + this.y + ")";
// System.out.print(point);
}
}
//继承
public class Program {
/**
* @param args
*/
public static void main(String[] args) {
Animal animal = new Animal();
Animal dog = new Dog();
animal.say();
dog.say();
animal.eat(animal);
dog.eat(dog);
System.out.println(animal.info());
System.out.println(dog.info());
}
}
class Animal {
private String name = "Animal";
protected void say() {
System.out.println("Animal" + " " + this.name);
}
public void eat(Animal food) {
System.out.println("Animal eat " + food);
}
public Object info() {
return "Animal";
}
@Override
public String toString() {
return "Animal";
}
}
class Dog extends Animal {
private String name = "Dog";
@Override
public final void say() {
System.out.println("Dog" + " " + this.name);
}
@Override
public final void eat(Animal food) {
super.eat(food);
System.out.println("Dog eated");
}
@Override
public final String info() {
return "Dog";
}
@Override
public final String toString() {
return "Dog";
}
}
异常
public final class Program {
public static void main(String[] args) {
try {
test();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void test() throws Exception {
throw new Exception("I am wrong!");
}
}
泛型
泛型方法
static <T> void puts(T msg) {
println(msg);
}
static void println(Object msg) {
System.out.println("Object:" + msg);
}
static void println(String msg) {
System.out.println("String:" + msg);
}
puts("hello");
Program.<String> puts("hello");
泛型类
class TestGenericClass<T> {
T value;
void setValue(T value) {
this.value = value;
}
}
Properties
写入文件
public void writeProperties() {
Properties properties = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream("config.properties");
properties.setProperty("url", "jdbc:mysql://localhost:3306/");
properties.setProperty("username", "root");
properties.setProperty("password", "root");
properties.setProperty("database", "users");//保存键值对到内存
properties.store(output, "Steven1997 modify" + new Date().toString());
// 保存键值对到文件中
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
加载
private static String basePath = "src/main/java/cn/habitdiary/prop.properties";
private static String path = "";
public static String getPath1() {
try {
InputStream in = new BufferedInputStream(new FileInputStream(
new File(basePath)));
Properties prop = new Properties();
prop.load(in);
path = prop.getProperty("path");
} catch (FileNotFoundException e) {
System.out.println("properties文件路径书写有误,请检查!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return path;
}
/**
* 二、 使用java.util.ResourceBundle类的getBundle()方法
* 注意:这个getBundle()方法的参数只能写成包路径+properties文件名,否则将抛异常
*
* @return
*/
public static String getPath2() {
ResourceBundle rb = ResourceBundle
.getBundle("cn/habitdiary/prop");
path = rb.getString("path");
return path;
}
/**
* 三、 使用java.util.PropertyResourceBundle类的构造函数
*/
public static String getPath3() {
InputStream in;
try {
in = new BufferedInputStream(new FileInputStream(basePath));
ResourceBundle rb = new PropertyResourceBundle(in);
path = rb.getString("path");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return path;
}
/**
* 四、 使用class变量的getResourceAsStream()方法
* 注意:getResourceAsStream()方法的参数按格式写到包路径+properties文件名+.后缀
*/
public static String getPath4() {
InputStream in = LoadPropertiesFileUtil.class
.getResourceAsStream("cn/habitdiary/prop.properties");
//五、使用class.getClassLoader()所得到的java.lang.ClassLoader的
InputStream in = LoadPropertiesFileUtil.class.getClassLoader()
.getResourceAsStream("cn/habitdiary/prop.properties");
//六、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
InputStream in = ClassLoader
.getSystemResourceAsStream("cn/habitdiary/prop.properties");
Properties p = new Properties();
try {
p.load(in);
path = p.getProperty("path");
} catch (IOException e) {
e.printStackTrace();
}
return path;
}
遍历
public static void printProp(Properties properties) {
System.out.println("---------(方式一)------------");
for (String key : properties.stringPropertyNames()) {
System.out.println(key + "=" + properties.getProperty(key));
}
System.out.println("---------(方式二)------------");
Set<Object> keys = properties.keySet();//返回属性key的集合
for (Object key : keys) {
System.out.println(key.toString() + "=" + properties.get(key));
}
System.out.println("---------(方式三)------------");
Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();
//返回的属性键值对实体
for (Map.Entry<Object, Object> entry : entrySet) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
System.out.println("---------(方式四)------------");
Enumeration<?> e = properties.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = properties.getProperty(key);
System.out.println(key + "=" + value);
}
}