java面向对象特性
封装
继承
多态(方法重载-参数不同,方法重写)
java类的继承,代码执行顺序重新熟悉一下
对象的初始化顺序:首先执行父类静态的内容,父类静态的内容执行完毕后,接着去执行子类的静态的内容,当子类的静态内容执行完毕之后,再去看父类有没有非静态代码块,如果有就执行父类的非静态代码块,父类的非静态代码块执行完毕,接着执行父类的构造方法;父类的构造方法执行完毕之后,它接着去看子类有没有非静态代码块,如果有就执行子类的非静态代码块。子类的非静态代码块执行完毕再去执行子类的构造方法。总之一句话,静态代码块内容先执行,接着执行父类非静态代码块和构造方法,然后执行子类非静态代码块和构造方法。
package com.test.redis;
class Parent {
static String name = "hello";
{
System.out.println("parent block");
}
static {
System.out.println("parent static block");
}
public Parent() {
System.out.println("parent constructor");
}
void meth(){
System.out.println("parent method");
}
}
package com.test.redis;
class Child extends Parent {
static String childName = "hello";
{
System.out.println("child block");
}
static {
System.out.println("child static block");
}
public Child() {
System.out.println("child constructor");
}
void meth(){
System.out.println("child method");
}
}
package com.test.redis;
public class Inhereritance {
public static void main(String chars[]) {
Parent pc = new Child();
pc.meth();
}
}
执行结果
parent static block
child static block
parent block
parent constructor
child block
child constructor
child method
原文:http://www.cnblogs.com/govoid/p/5054972.html