数据加载中……
继承

public class Animal

{

    int height;

    int weight;

    void animal()

    {

        System.out.println("Animal constract");

    }

    void eat()

    {

        System.out.println("Animal eat");

    }

    void sleep()

    {

        System.out.println("Animal sleep");

    }

    void breathe()

    {

        System.out.println("Animal breathe");

    }

}

/*

* 理解继承是理解面向对象程序设计的关键

* 在java中,通过关键字extends继承一个已有的类,被继承的类称为父类(超类,基类),新的类称为子类(派生类)。

* * 在java中,不允许多继承

*/

class Fish extends Animal

{

    void fish()

    {

        

        System.out.println("fish constract");

    }

    void breathe()

    {

        //super.breathe();

        //super.height=40;

        System.out.println("fish boo");

    }

}

class Integration

{

    public static void main(String[]args)

    {

        //Animal an=new Animal();

        Fish fh=new Fish();

        //an.breathe();

        //fh.height=30;

        fh.breathe();

        

    }

}

/*

*在子类当中定义一个与父类同名,返回类型,参数类型均一致的方法,称为方法的覆盖

*方法的覆盖发生在子类和父类之间。

*调用父类的方法使用super

*/

/*特殊变量super,提供了父类的访问

* 可以使用super访问被父类被子类隐藏的变量或覆盖的方法

* 每个子类构造方法的第一句,都是隐藏的调用super(),如果父类没有这种形式的构造函数,那么在编译器中就会报错。

*

*

*

*/

posted on 2008-03-20 11:58 rick 阅读(183) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: