<<  < 2016 - >  >>
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30




第一种:方法重载(赋初始值)People类:
public class People{
protected String name;
protected int stuNo;
protected int age;
protected String picName;
//方法重载
protected void say(String _n){
this.name=_n;
};
protected void say(String _n,int _sn){
this.name=_n;
this.stuNo=_sn;
};
protected void say(String _n,int _sn,int _a){
this.name=_n;
this.stuNo=_sn;
this.age=_a;
};
protected void say(String _n,int _sn,int _a,String _pn){
this.name=_n;
this.stuNo=_sn;
this.age=_a;
this.picName=_pn;
};
}  

测试: MainDao类
public static void main(String[] args) {
People p=new People();
p.say("张一");
printSay(p);
p.say("张二", 12020401);
printSay(p);
p.say("张三", 12020402, 23);
printSay(p);
p.say("张四", 12020403, 25, "时光与你");
printSay(p);
};
public static void printSay(People p){
System.out.println(p.name);
System.out.println(p.stuNo);
System.out.println(p.age);
System.out.println(p.picName);
}; 

第二种:造重载(赋初始值)Student类:
public class Student {
protected String name;
protected int stuNo;//学号
protected int age;
protected String picName;//呢称
//构造重载
public Student(){
};
public Student(String n){
this.name=n;
};
public Student(String n,int sn){
this.name=n;
this.stuNo=sn;
};
public Student(String n,int sn,int a){
this.name=n;
this.stuNo=sn;
this.age=a;
};
public Student(String n,int sn,int a,String pn){
this.name=n;
this.stuNo=sn;
this.age=a;
this.picName=pn;
}
}

测试:MainDao类
public static void main(String[] args) {
Student s=new Student();
printStudent(s);
Student s1=new Student("张一");
printStudent(s1);
Student s2=new Student("张二",121101);
printStudent(s2);
Student s3=new Student("张三",121102,23);
printStudent(s3);
Student s4=new Student("张四",121103,25,"时光与你");
printStudent(s4);
}
public static void printStudent(Student sdt){
System.out.println(sdt.name);
System.out.println(sdt.stuNo);
System.out.println(sdt.age);
System.out.println(sdt.picName);
System.out.println("-----------分割线------------");
};

发表评论:
天涯博客欢迎您!