有专业做网站的吗网站公司,直播秀场网站开发,网站 风格,浙江省建筑信息港官网构造器#xff0c;是面向对象所特有的概念#xff0c;是一种特殊的方法#xff0c;与对象创建有关1、构造器没有返回值类型2、构造器方法名与类名相同#xff0c;而且可以重载构造器3、构造器不能手动调用#xff0c;只能在创建对象时自动调用一次4、如果没有在类中定义构…构造器是面向对象所特有的概念是一种特殊的方法与对象创建有关1、构造器没有返回值类型2、构造器方法名与类名相同而且可以重载构造器3、构造器不能手动调用只能在创建对象时自动调用一次4、如果没有在类中定义构造方法在程序编译时会自动生成公开的、无参的空的构造器5、如果自定义了构造器就不会自动生成默认构造器如果需要此无参构造器需要自定义public 类名(){}public class TestConstructor{public static void main(String args[]){//1、构造器重载OverLoadConstructor olc_1 new OverLoadConstructor();//使用无参构造器创建对象OverLoadConstructor olc_2 new OverLoadConstructor(2);//使用有参构造器创建对象//2、构造器不能手动调用//olc_1.OverLoadConstructor();//错误//3、默认构造器DefaultConstructor dc new DefaultConstructor();System.out.println(dc.name);//4、自定义构造器//DefineConstructor dfc new DefineConstructor();//错误无参构造器未定义DefineConstructor dfc new DefineConstructor(1);}}//构造器重载class OverLoadConstructor{//无参构造器public OverLoadConstructor(){System.out.println(“OverLoadConstructor()”);}//有参构造器public OverLoadConstructor(int n){System.out.println(“OverLoadConstructor(int)”);}}//默认构造器class DefaultConstructor{String name “ldt”;}//自定义构造器class DefineConstructor{public DefineConstructor(int n){System.out.println(“DefineConstructor(int)”);}//无参构造器//public DefineConstructor(){}}© 2014, 李德涛博客. 版权所有.