本帖最后由 波涛 于 2014-5-18 21:33 编辑
- public class Demo {
- public static void main(String[] args) {
- B b1 = new B("Lily");
- B b2 = new B("Lucy",23);
- b1.speak();
- b2.speak();
- }
- }
- class A{
- String name;
- A(String name){
- this.name=name;
- }
- public void speak(){
- System.out.println("I'm "+name);
- }
- }
- class B extends A{
- int age;
- B(String name){
- super(name);
- }
- B(String name,int age){
- super(name);
- this.age=age;
- }
- public void speak(){
- System.out.println("I'm "+name+", "+age+" years old,I'm eating now!");
- }
- }
复制代码
子类不能再声明无参构造了,因为他找不到父类中对应的无参构造方法。
|