java中关于抽象类,多态的使用练习
import java. util. Scanner;
public class test {
public static void main ( String[ ] args) {
Person p= null;
p= new Manager ( "xiaoming" , "55456456" , "13454564654" , "15356456" ) ;
p. work ( ) ;
( ( Manager) p) . show ( ) ;
p= new Staff ( "dongdong" , "456" , "456" ) ;
p. work ( ) ;
( ( Staff) p) . show ( ) ;
}
}
abstract class Person {
String name;
String id;
String salary;
abstract void work ( ) ;
}
class Manager extends Person {
String bonus;
public void work ( ) {
System. out. println ( name+ "经理的工作内容是监督" ) ;
}
public void show ( ) {
System. out. println ( "姓名是:" + name+ "工号是:" + id+ "薪水是:" + salary+ "奖金是" + bonus) ;
}
public Manager ( String name, String id, String salary, String bonus) {
this . name= name;
this . id= id;
this . salary= salary;
this . bonus= bonus;
}
}
class Staff extends Person {
public void work ( ) {
System. out. println ( name+ "员工的职责是干活" ) ;
}
public Staff ( ) {
}
public Staff ( String name, String id, String salary) {
this . name= name;
this . id= id;
this . salary= salary;
}
public void show ( ) {
System. out. println ( "姓名是:" + name+ "工号是:" + id+ "薪水是:" + salary) ;
}
}