原来如此,了解了,谢谢大家!!
以下是修改过的代码……
- abstract class YuanGong
- {
- private String name;
- private int age;
- private double pay;
- public abstract void work();
- public void speak()
- {
- System.out.printf("My name is %s,I'm %d",name,age);
- }
- /*YuanGong()
- {
- }*/
- YuanGong(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- }
- class GuYuan extends YuanGong
- {
- /*
- 要写出子类的有参构造函数,
- 并在其中用super调用父类的有参构造函数,
- 如此,才能在创建子类对象时,把参数传给对象。
- */
- GuYuan(String name,int age)
- {
- super(name,age);
- }
- public void work()
- {
- System.out.println("I'm working hard!");
- }
- }
- public class AbstractDemo
- {
- public static void main(String[] args)
- {
- GuYuan yuan = new GuYuan("zhang san",18);
- yuan.speak();
- yuan.work();
- }
- }
复制代码 |