首页 > 编程语言 > 详细

java面向对象

时间:2015-12-17 19:02:52      阅读:119      评论:0      收藏:0      [点我收藏+]

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

 

java面向对象

原文:http://www.cnblogs.com/govoid/p/5054972.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!