父类:
public class ManKind {
int sex;
int salary;
//判断性别
void manOrWoman(){
if(sex == 1){
System.out.println("man");
}else if(sex == 0){
System.out.println("woman");
}
}
//通过收入判断是否有工作
void employeed(){
if(salary == 0){
System.out.println("no job");
}else{
System.out.println("job");
}
}
}
子类:
//继承父类Mankind
public class Kids1 extends ManKind{
int yearsOld;
void printAge(){
System.out.println(yearsOld);
}
public static void main(String[] args) {
Kids1 kid = new Kids1();
//访问父类的salary、sex
kid.yearsOld = 22;
kid.salary = 1000;
kid.sex = 0;
//调用父类的employeed()、manOrWoman()方法
kid.employeed();
kid.manOrWoman();
kid.printAge();
}
}
原文:http://www.cnblogs.com/ahfz/p/5860146.html