有参构造器(方法)和无参构造器(方法)的作用(区别)

2022/5/6 6:14:30

本文主要是介绍有参构造器(方法)和无参构造器(方法)的作用(区别),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、概念

java中构造方法指的是:
与类名相同,无返回类型的方法,参数可以为空,也可以带参数。
比如一个 Dao 类,private Dao(){}这个就是无参数的构造方法。
private Dao(String name){}这个就是带参数的构造方法。
作用:用来初始化 java 类,提供类的初始化和实例化,以便调用。

二、作用

new一个对象的时候要用到构造函数,例如Hello hello = new Hello();这时调用的是Hello的无参数构造方法;Hello hello = new Hello(“hi”);这个是调用Hello有参数构造方法,在JAVA中如果不写构造方法的话,会默认加上一个无参数的构造方法,但是如果已经有了一个有参数的构造方法,那么无参数的构造方法就不会默认被加上.如果Hello类中已经有了一个有参数的构造方法,这时再使用Hello hello = new Hello();来创建对象的时候就会出错,这就是为什么书上要强调写了有参数的构造方法就最好加一个无参数的构造方法。

三、区别

有参数的构造方法的主要目的是为类中的属性初始化的,而无参数的构造方法,可以控制new对象。
假设你的午餐构造方法不是public 修饰 而是project ,
那么别人将不只能直接new一个对象,这就起到了控制作用。
为什么要自动生成无参构造呢?
你没有构造方法就不能new出对象 所以就给你生成一个无参构造方法
一般构造方法就是来控制你new对象的 还有就是初始化属性

很显然,构造器的最大用处就是在创建对象时,对对象的实例进行初始化,那么构造器又分为有参构造和无参构造,下面我们就通过代码来理解有参和无参构造。

package sessionWork2;
/**
*默认的无参构造方法
*/
	public class Point {
			int x; 
			int y;
		<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">movePoint</span><span class="token punctuation">(</span><span class="token keyword">int</span> dx<span class="token punctuation">,</span><span class="token keyword">int</span> dy<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>

//				this.x += dx;

this.x = this.x+dx;

//				this.y += dy;

this.y = this.y+dy;

}
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>

//为Point类new一个对象,因为类里面有无参构造器,看不到,所以才能new出来,

Point p2 = new Point();

p2.movePoint(2, 6);

System.out.println("p2X轴的坐标为:"+p2.x+"\n" + "P2Y轴的坐标为 :" + p2.y);
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

输出结果如下,采用的是默认的无参构造,因为类里面有默认的无参构造器,所以
123456789101112131415161718192021222324

我们可以new出来对象

在这里插入图片描述

下面我们来看下,如果我们创建一个有参构造的话,是否可以new出来无参的对象

package sessionWork2;
<span class="token comment">/**
     *创建一个有参构造器,然后new一个无参对象
     */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Point</span> <span class="token punctuation">{<!-- --></span>
		<span class="token keyword">int</span> x<span class="token punctuation">;</span> 
		<span class="token keyword">int</span> y<span class="token punctuation">;</span>

//			创建一个有参构造器

public Point(int x0, int y0){

this.x = x0;

this.y = y0;

}

public void movePoint(int dx,int dy){

//				this.x += dx;

this.x = this.x+dx;

//				this.y += dy;

this.y = this.y+dy;

}
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
		
		Point p2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Point</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		p2<span class="token punctuation">.</span><span class="token function">movePoint</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"p2X轴的坐标为:"</span><span class="token operator">+</span>p2<span class="token punctuation">.</span>x<span class="token operator">+</span><span class="token string">"\n"</span> <span class="token operator">+</span> <span class="token string">"P2Y轴的坐标为 :"</span> <span class="token operator">+</span> p2<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
		
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

1234567891011121314151617181920212223242526272829

可以看到我们只有有参构造的时候是无法new出来无参的对象的
在这里插入图片描述

此时我们再创建一个无参构造器,发现我们new的无参对象不报错了,

package sessionWork2;
	public class Point {
			int x; 
			int y;
			//创建一个无参构造器
			public Point(){		
		<span class="token punctuation">}</span>
                    <span class="token comment">//创建一个有参构造器</span>
		<span class="token keyword">public</span> <span class="token function">Point</span><span class="token punctuation">(</span><span class="token keyword">int</span> x0<span class="token punctuation">,</span> <span class="token keyword">int</span> y0<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
			<span class="token keyword">this</span><span class="token punctuation">.</span>x <span class="token operator">=</span> x0<span class="token punctuation">;</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span>y <span class="token operator">=</span> y0<span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	
		<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">movePoint</span><span class="token punctuation">(</span><span class="token keyword">int</span> dx<span class="token punctuation">,</span><span class="token keyword">int</span> dy<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>

//				this.x += dx;

this.x = this.x+dx;

//				this.y += dy;

this.y = this.y+dy;

}
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
		Point p1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Point</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		p1<span class="token punctuation">.</span><span class="token function">movePoint</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"p1x轴的坐标为:"</span> <span class="token operator">+</span> p1<span class="token punctuation">.</span>x <span class="token operator">+</span><span class="token string">"\n"</span> <span class="token operator">+</span> <span class="token string">"p1y轴的坐标为:"</span><span class="token operator">+</span>p1<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
		
		Point p2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Point</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		p2<span class="token punctuation">.</span><span class="token function">movePoint</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"p2X轴的坐标为:"</span><span class="token operator">+</span>p2<span class="token punctuation">.</span>x<span class="token operator">+</span><span class="token string">"\n"</span> <span class="token operator">+</span> <span class="token string">"P2Y轴的坐标为 :"</span> <span class="token operator">+</span> p2<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
		
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

1234567891011121314151617181920212223242526272829303132

执行结果如下
在这里插入图片描述

来源:https://blog.csdn.net/whitek387/article/details/109205823

这篇关于有参构造器(方法)和无参构造器(方法)的作用(区别)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程