Java默认构造函数的作用

mac2024-11-23  29

Java默认构造函数的作用,有需要的朋友可以参考下。

class Person{ private String name=""; private int age=0; <span class="token keyword">public</span> <span class="token function">Person</span><span class="token punctuation">(</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">"person无参数构造函数"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token function">Person</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span><span class="token keyword">int</span> age<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token operator">=</span>name<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>age<span class="token operator">=</span>age<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">"person 2 参数的构造函数"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>

}

class Student extends Person{

<span class="token keyword">private</span> String school<span class="token punctuation">;</span> <span class="token keyword">private</span> String grade<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token function">Student</span><span class="token punctuation">(</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">"student 无参数的构造函数"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token function">Student</span><span class="token punctuation">(</span>String name <span class="token punctuation">,</span><span class="token keyword">int</span> age<span class="token punctuation">,</span>String school<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">"student 3 参数的构造函数"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token function">Student</span><span class="token punctuation">(</span>String name <span class="token punctuation">,</span><span class="token keyword">int</span> age<span class="token punctuation">,</span>String school<span class="token punctuation">,</span>String grade<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>school<span class="token operator">=</span>school<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>grade<span class="token operator">=</span>grade<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">"student 4 参数的构造函数,super()."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>

}

class Test{ public static void main(String [] args){ System.out.println(“st1:”); Student st2=new Student(); System.out.println("---------------------------");

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">"st2:"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Student st<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">"zhangshan"</span><span class="token punctuation">,</span><span class="token number">76</span><span class="token punctuation">,</span><span class="token string">"武大"</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">"---------------------------"</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">"st3:"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Student st3<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">"lisi"</span><span class="token punctuation">,</span><span class="token number">24</span><span class="token punctuation">,</span><span class="token string">"武大"</span><span class="token punctuation">,</span><span class="token string">"研究生"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>

}

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051

输出如下:

st1: person无参数构造函数 student 无参数的构造函数 --------------------------- st2: person无参数构造函数 student 3 参数的构造函数 --------------------------- st3: person 2 参数的构造函数 student 4 参数的构造函数,super(). 1234567891011

说明了创建一个子类的对象实例的时候,必先调用父类的无参数的构造函数(默认构造函数),假如父类有带参数的构造函数,那么系统将不会给它创建无参数的构造函数,这时,子类在实例化的时候,因为找不到父类的默认构造函数,编译器将会报错,但如果在子类的构造函数中指定用父类的带参数的构造函数的时候,或者在父类中加一个无参数的构造函数,就不会报错。

============================================

我们假设A是B的父类,B是A的子类。

1、如果程序员没有给类A没有提供构造函数,则编译器会自动提供一个默认的无参数的构造函数,如果用户提供了自己的构造函数,则编译器就不在提供默认的无参数构造函数。

2、子类B实例化时会自动调用父类A的默认构造函数,所以如果A的默认的无参数的构造函数为private,则编译器会报错,而如果A没有提供默认的无参数的构造函数,而提供了其他类型的构造函数,编译器同样会报错,因为B找不到A的默认无参数构造函数。所以,我们最好给父类A提供一个无参数的构造函数。

3、或者在B的构造函数中显示的调用父类A的有参构造函数。super(parameter)

————————————————————————————————————————————————————————————

问题:JAVA的无参构造方法是什么意思? 无参嘛,没有参数;需要注意以下几点:

你可显示为你的类定义无参构造方法、及有参构造方法;构造方法可重载。如果你的类没有定义任何构造方法,则它会存在一个默认的无参构造方法,这个是JVM在加载类时为它自动添加的,继承至超类Object。如果你的类只定义了其他的有参构造方法,那么就不存在无参构造方法了。只有显示为它定义了无参构造方法才能使用无参构造方法创建对象。

因 为你的父类已经定义了一个有参的构造器,此时编译器不会为你调用默认的构造器,当子类继承时,必须在自己的构造函数显示调用父类的构造器,自己才能确保子 类在初始化前父类会被实例化,如果你父类中有无参的构造器,字类就不会强制要求调用,即你写的那个就可以通过,编译器会默认帮你调用父类的构造器。

按原来的思路

java无参构造函数的意义 创建一个子类对象的实例的时候,必先调用父类的无参构造函数(默认构造函数),假如父类有带参数的构造函数,那么系统不会给他创建无参数的构造函数,这时,子类在实例化的时候,因为找不到父类的默认构造函数,编译器会报错,如果在子类的构造函数中指定使用父类的带参数的构造函数的时候,或者在父类中加一个无参数的构造函数的时候,就不会报错了。

——————————————————————————————————————————————————————————————

动手操作:

1.即使子类的构造函数没有用super();去调用父类的构造函数,系统也会自动去调用父类的构造函数。去初始化父类示例。所以说是自动的。

class Person{ Person(String name,int age){ System.out.println("父类初始化操作"); } <span class="token function">Person</span><span class="token punctuation">(</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">"父类构造方法被调用!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>

}

class Student extends Person{ public Student() { //super(); //super(“1”, 1); System.out.println(“子类构造方法被调用!!!”); } }

12345678910111213141516

2.构造函数的作用就是初始化对象。不管是带参,还是不带参数的。并且首先调用父类的构造函数,先初始化父类,然后才初始化自己。一般对象都有一个Object的父类。所以构造实例是,必定会构造Object

最新回复(0)