package day6; abstract class Employee { private String name; private int id; private double pay; public Employee(String name,int id,double pay) {//父类中定义了一个带参数的构造函数,那么自己本来就有的隐式的构造函数就不存在了,那么子类中必须也有带参数的构造函数 // TODO Auto-generated constructor stub this.name = name; this.id = id; this.pay = pay; } public abstract void work();//抽象方法是没有方法体的,但是在子类中一定要把抽象方法给实现 } class coder extends Employee { public coder(String name, int id, double pay ) { super(name, id, pay ); // TODO Auto-generated constructor stub } @Override public void work() { // TODO Auto-generated method stub System.out.println("coding"); System.out.println(); } } class manager extends Employee { private double bonus; public manager(String name, int id, double pay,double bonus) { super(name, id, pay); // TODO Auto-generated constructor stub this.bonus=bonus; } @Override public void work() { // TODO Auto-generated method stub System.out.println("manage"); } } public class Test4 { public static void main(String[] args ) { coder c = new coder("jinjin", 12, 2.00) ; c.work(); manager m = new manager("lili", 23, 123, 12); m.work(); } }
coding
manage
像这样的话报错的原因,是因为父类中定义了带参数的构造方法之后,不带参数的默认构造方法已经不存在了,所以在子类中没有必要再去写一个不带参数的构造方法,写一个构造方法就ok
来源:https://www.cnblogs.com/mmlovejj/p/5268873.html