继承类的练习
一个父类Mankind(成员变量name,sex,salary),一个子类kids(成员变量yearsold)。
Mankind类
package 练习;
public class Mankind {
String name;
int sex;
int salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public void manorwoman(){
if(sex ==0){
System.out.println("性别为man");
}
else if(sex ==1){
System.out.println("性别为woman");
}
else{
System.out.println("sex is wrong!");
}
}
public void employeed(){
if(salary ==0){
System.out.println("no job!");
}
else{
System.out.println("job!");
}
}
}
kids类
package 练习;
public class Kids extends Mankind{
int yearsOld;
public int getYearsOld() {
return yearsOld;
}
public void setYearsOld(int yearsOld) {
this.yearsOld = yearsOld;
}
public void printage(){
System.out.print(this.yearsOld);
}
public static void main(String[] args) {
Kids somekid = new Kids();
somekid.setName("张三");
somekid.setSalary(0);
somekid.setSex(0);
somekid.setYearsOld(15);
System.out.print(somekid.getName());
somekid.printage();
somekid.manorwoman();
somekid.employeed() ;
}
}
运行结果