首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java 构造函数(抽象类中的构造函数) 跟 加载

2012-11-03 
Java 构造函数(抽象类中的构造函数) 和 加载?1. Java 的构造函数 与初始化块:?a. 抽象类的构造函数若果在

Java 构造函数(抽象类中的构造函数) 和 加载

?

1. Java 的构造函数 与初始化块:

?

a. 抽象类的构造函数

若果在父类中(也就是抽象类)中显示的写了有参数的构造函数,在子类是就必须写一个构造函数来调用父类的构造函数

?

?abstract class Person { ? //定义一个抽象类,必须被继承

?

? ? Person(int i) {

?

? ?}

?}

?

?public class Student extends Person {

?

? ? Student() {

? ? ?super(int i)://必须显示的调用父类构造方法//super代表父类对象

? }

?}

?

b. 构造函数

?

public class Sample{?

? // 第一:在这a=1

? static int a?

// 第二:a=2?

? static{ a=2;}?

? // 第三:a=4?

? static{ ?a=4;}?

? ?public static void main(String[] args)...{?

? ? ? ?// 第四:a=4,执行之后a=5

? ? ? ?a++;?

? ? ? ?// 第五:a=5?

? ? ? ?System.out.println("a="+a);?

? }?

}

引段网友对类的加载及生命周期的说明:?

当类不被用到的时候,什么都不提前加载,?

(1)一旦类被使用到(import时),首先加载的是它的静态变量,然后是静态初始化块,静态方法(不调用时不执行)。?

(2)当此类要被构造一个对象(new 时)的时候,即被new,或者用反射生成其对象的时候,加载其成员部分。依次为成员变量(即非静态变量)、非静态初始化块、非静态方法、最后才是构造器(不创建实例时不执行)。?

成员部分除方法外,每个对象都有其一份副本,其中,非静态方法并不是每个对象都有一个,而是所有对象共享一个副本,这点要注意。

静态部分一旦被加载,就不会被销毁,直到程序结束,关闭虚拟机。?

非静态的,即,被各个对象所拥有的东西,当对象不被引用时,这个类就结束了他的生命周期。?

但是,它还要在内存中驻留一段时间,等垃圾处理器来将它清除。?

在失去引用后,被垃圾处理前的这段时间,虽然它驻留在内存中,但是没法被再次引用。?

请注意上面说明的加载顺序:

首先加载 静态变量 然后是静态初始化块,最后是静态方法。

为了验证这个顺序,将上面的代码稍微改了下,添加了个静态方法。

如下?

package test1;

public class ClassLoadTest

{ ?

? ? static ?int a=2; ? ?

? ? static{ a=3;}

? ? static{a=4;}

? ? static void init() {a=10;} ? ?

? ? /** * @param args

? ? ?*/

public static void main(String[] args)?

{

? ? ? ? System.out.println("a="+a);//+为连字符

? ? }

}

结果输出为:a=4

?

1.构造函数

(1)任何一个类不管它是抽象的还是具体的,都拥有一个构造函数,即使程序员不键入它,Java也会提供一个默认的无参的构造函数。构造函数必须要与类同名,构造函数一定不能够有返回类型,切记void也是一种返回类型!?

如果在类中没有创建任何构造函数,那么系统将使用默认的构造函数,如果程序员定义了一个构造函数,那么默认的构造函数将不存在!

?public class Book {?

private String id;

private String title;

private String author;

? ? ? ? //我们自己定义一个构造函数

public Book(String idIn,String titleIn,String authorIn){

id=idIn;

title=titleIn;

author=authorIn;

}

public String toString(){

return "The info of the book:\n"+

? ? ? "Title:"+title+"\n"+

? ? ? "Author:"+author+"\n";

}

}?

public class Test {?

public static void main(String[]args){

//Book book=new Book(); ? ?使用默认的构造函数将出现编译错误?

Book book=new Book("0101001","Thinking in Java","Bruce Eckel");

System.out.println(book);

}

}

?

?

(2)构造函数的执行方式:?

首先调用其超类的构造函数,超类构造函数又调用其超类构造函数,直至到达Object构造函数为止,然后Object()构造函数执行,直到所有的构造函数完成?

public class Animal {?

public Animal(){

? ? ? ? ? ? ? ? ? ?System.out.println("This is the animal constructor");

? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

public class Snake extends Animal{?

public Snake(){

System.out.println("This is snake constructor");

}

}

public class Cobra extends Snake{

?

public Cobra(){

System.out.println("This is the cobra constructor");

}

}

public class Test {

?

public static void main(String[]args){

new Cobra(); ?

}

}

执行结果:

This is the animal constructor

This is snake constructor

This is the cobra constructor

?

(3)默认构造函数是一个无变元的构造函数,隐式的包含了一个对super()的调用?

如果一个子类的超类没有无参的构造函数,那么其子类必须程序员实现构造函数,而无法调用默认的构造函数

?public class Rpg {?

private int hp;

private int mp;

private int grade;

private int exp;

public Rpg(int hpIn,int mpIn,int gradeIn,int expIn){

hp=hpIn;

mp=mpIn;

grade=gradeIn;

exp=expIn;

}

}

public class Magician extends Rpg{

?

//public Magician(){ ? 不可以使用默认的构造函数!

//}

public Magician(int hpIn,int mpIn,int gradeIn,int expIn){

super(hpIn,mpIn,gradeIn,expIn);

}

}?

(4)构造函数可以重载,如果在同一个类中一个构造函数需要调用另一个重载的构造函数,可以使用this(),this()的变元列表决定了调用哪个具体的构造函数

注意:this()和super()必须出现在构造函数的第一行!!!而且this()和super()函数不能位于同一个构造函数中!!!

?抽象类的构造函数在实例化具体子类时被调用

?接口是没有构造函数的!

?2.初始化块:

Java类中执行操作的地方有三个:

构造函数、方法和初始化块

?Java初始化块分为静态初始化块和实例初始化块:

?

首次加载类时,会运行一次静态初始化块,每次创建一个新实例时,都会运行一次实例初始化块,类中允许出现多个初始化块,它们所执行的顺序与它们在代码中所出现的顺序相同(程序执行时默认是从上到下的)

?

总体的执行顺序:静态初始化块->super()->实例初始化块->构造函数的其它部分,通过一个例子来说明:

?

public class Father {

?

public Father(){

System.out.println("This is super class!");

}

}

public class Test extends Father{

?

static{

System.out.println("This is static block!");//静态块

}

public Test(){

System.out.println("This is test constructor");//构造函数

}

public static void main(String[]args){

System.out.println("Hello,Java!");

Test test=new Test();

}

{

System.out.println("Common init block!");

}

}

上面的例子的输出结果是:

This is static block!

Hello,Java!

This is super class!

Common init block!

This is test constructor

热点排行