重写和继承样例
ManKind 类方法以及 Child 继承:
public class ManKind {
int gender;
int salary;
void People(int gender){
if(gender==1)
System.out.println("1代表渣男");
else if(gender==0)
System.out.println("0代表渣女");
else
System.out.println("既不是1,又不是0,你好奇怪噢!");
}
void employee(int x){
if(x>=100)
System.out.println("工资大于100W啦,I am your BOSS!!");
else
System.out.println("我没有那么多钱,我是被拐的非洲黑奴");
}
}
//继承
class Child extends ManKind{
int age;
void printage(int x){
age=x;
System.out.println("悄悄告诉你,我已经"+age+"岁了呢~");
}
void employee(){ //重写:在子类中重新定义父类中已有的方法
System.out.println("小孩子就是应该以读书为重!");
}
}
主方法
public class Today {
public static void main(String[] args) {
ManKind peo=new ManKind();
peo.People(1);
peo.People(0);
peo.People(2);
peo.employee(50);
peo.employee(100);
System.out.println("下面是子类调用");
Child cd=new Child();
cd.People(1);
cd.People(0);
cd.employee(101);
cd.employee();
cd.printage(56);
}
}