面向过程:
语言:C
一件事一套流程做完
应用:算法实现、底层开发、操作系统
面向对象:
语言:C++、JAVA
赋予对象某些功能,调用对象实现功能
应用:复杂系统、提高代码复用性、可维护性的场景
构造函数(也称构造方法、构造器)
是一种特殊的成员函数,初始化对象的成员变量
结构:函数名与类名相同,没有返回值类型
若未显式定义构造函数,则为默认的无参构造函数
构造函数重载
例:
类
public class Resonator {
private String name;
private char sex;
private String weapon;
private String attribute;
public Resonator() {
name="卡卡罗";
sex='男';
}
public Resonator(String weapon) {
this.weapon=weapon;
}
public Resonator(String weapon,char sex){
this.weapon=weapon;
this.sex=sex;
}
public Resonator(char sex,String name){
this.name=name;
this.sex=sex;
}
public void show()
{
System.out.printf("name:%s\tsex:%c\tweapon:%s\tattribute:%s\t\n",name,sex,weapon,attribute);
}
}
调用
public class Test {
public static void main(String[] args) {
Resonator resonator1=new Resonator();
resonator1.show();
Resonator resonator2=new Resonator("sword");
resonator2.show();
Resonator resonator3=new Resonator('女',"吟霖");
resonator3.show();
Resonator resonator4=new Resonator("Gauntlets",'男');
resonator4.show();
}
}
结果
name:卡卡罗 sex:男 weapon:null attribute:null
name:null sex: weapon:sword attribute:null
name:吟霖 sex:女 weapon:null attribute:null
name:null sex:男 weapon:Gauntlets attribute:null
构造函数的类型,形参数量,形参顺序不同,结果不同
缺点:列举可能多
类型相同无法区分