首页 > 编程知识 正文

类的构造函数可以重载,什么是构造方法的重载

时间:2023-05-05 18:23:43 阅读:114224 作者:3179

构建方法的三个条件:

、方法名称和类名相同

、方法名称前没有返回值类型的声明

、方法中不能使用return语句

方法重载的三个条件:

、方法名称相同,参数类型不同

、参数个数不同,参数顺序不同

、重载与方法的返回类型无关,只与方法的参数有关

打包包1;

公共类演示6 {

publicstaticvoidmain (字符串[ ] args ) {

打印机aa=new打印机(1000;

a .打印(100;

a .打印(hahahahah );

a.print(hahahaha ),88 );

}

}

类打印机{

隐私品牌='联想';

私密双保密;

//制作方法

公共打印机(双精度) {

this.price=price;

}

公共打印机(双精度,字符串品牌) {

this.price=price;

this.brand=brand;

}

//方法重载

公共语音打印(字符串内容) {

System.out.println ('字符串' content );

}

公共语音打印(互内容) {

System.out.println ('整形' content );

}

公共语音打印(stringstr,int content ) {

system.out.println (str '---- ' content );

}

/*

*错误的重载方式

*公共int print (intcontent ) {

返回内容;

() /

//重载仅与方法的参数有关,而与方法的返回类型无关

公共int print (intcontent,double d ) {

返回内容;

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。