Java构造方法

来源:互联网 发布:前端编程思想 编辑:程序博客网 时间:2024/06/06 01:36

初学java,如果有写的不对的地方,欢迎指正。微笑

java构造方法的作用:

用来对类成员变量进行初始化。

构造方法的规则:

1.构造方法的名称必须与类名相同

2.构造方法没有返回值,也不需要些void

语法:

[访问修饰符] 类名( ){

//构造方法体

}


【例子】通过构造方法初始化成员变量

<span style="white-space:pre"></span><span style="font-size:18px;">package test;<span style="white-space: pre;"></span>public class This {<span style="white-space: pre;"></span>int i;<span style="white-space: pre;"></span>int j;<span style="white-space: pre;"></span>public This(){<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>public This(int m,int n){<span style="white-space: pre;"></span>i=m;<span style="white-space: pre;"></span>j=n;<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>package test;<span style="white-space: pre;"></span>public class ThisTest {<span style="white-space: pre;"></span>public static void main(String[] args) {<span style="white-space: pre;"></span>This th=new This(5,6);<span style="white-space: pre;"></span>System.out.println(th.i+" "+th.j);<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>}</span>


this关键字的使用

this关键字在方法中,用于指向调用该方法的当前对象,简单的说:哪个对象调用方法,this指的就是哪个对象。严格来讲在方法中需要通过this关键字指明当前的对象。

【例子】没有构造方法的类对象使用

<span style="white-space:pre"></span>package test;<span style="white-space:pre"></span>public class This {<span style="white-space:pre"></span>int i;<span style="white-space:pre"></span>int j;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>package test;<span style="white-space:pre"></span>public class ThisTest {<span style="white-space:pre"></span>public static void main(String[] args) {<span style="white-space:pre"></span>//类This只有无参构造方法时输出对象<span style="white-space:pre"></span>This th=new This();<span style="white-space:pre"></span>th.i=5;<span style="white-space:pre"></span>th.j=6;<span style="white-space:pre"></span>System.out.println(th.i+" "+th.j);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}


【例子】有构造方法的类对象使用

<span style="white-space:pre"></span><span style="font-size:18px;">package test;<span style="white-space:pre"></span>public class This {<span style="white-space:pre"></span>int i;<span style="white-space:pre"></span>int j;<span style="white-space:pre"></span>public This(){<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public This(int i,int j){//有参构造方法<span style="white-space:pre"></span>this.i=i;<span style="white-space:pre"></span>this.j=j;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>package test;<span style="white-space:pre"></span>public class ThisTest {<span style="white-space:pre"></span>public static void main(String[] args) {<span style="white-space:pre"></span>//类This有 有参构造方法时输出对象<span style="white-space:pre"></span>This th=new This(5,6);<span style="white-space:pre"></span>System.out.println(th.i+" "+th.j);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}</span>

需要注意的是:

1.任何一个类都必须有一个无参构造方法;

2.如果源程序中没有定义构造方法,编译器在编译时会自动加入一个无参构造方法(即默认构造方法)。

3.当源程序中自己定义了构造方法之后,java编译器便不再自动添加无参构造方法,那么程序会报错。这时需要手动添加一个无参构造方法。



0 0