赞
踩
Android:JAVA语言extends和implements用法的实例学习
一、extends和implements介绍
1、extends是继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承.二、以Java程式语言的角度来看﹐Java的interface是一些函数或资料成员为另一些属于不同类别的物件所需共同拥有,则将这些函数与资料成员定义在一个interface中,然后让所有不同类别的Java物件可以共同操作使用之;Java的class只能继承一个父类别(用extends关键字), 但可以拥有许多interface(用implements关键字)。
三、extends和implements有什么不同?四、简单的说,extends可以理解为全盘继承了父类的功能; implements可以理解为为这个类附加一些额外的功能; 举个例子,Animal是一个父类,cat,dog,bird,insect都extends了Animal, 但是cat,dog,bird还可以implements比如run,shout,这些interface,bird,insect可以 implements比如fly这些interface;
五、继承过后通常会定义一些父类没有的成员或者方法。
class B extends A
继承过后通常会定义一些父类没有的成员或者方法。
A a = new B();
这样是可以的,上传。
a是一个父类对象的实例,因而不能访问子类定义的新成员或方法。
假如这样定义:
class A{
int i;
void f(){}
}
class B extends A{
int j;
void f(){}//重写
void g(){}
}
六、实例学习。
1、java代码extends_and_implements.java
-
- interface Runner
- {
- int ID=520111;
- void run ();
- }
- interface Flyer
- {
- void fly ();
- }
- interface Shouter
- {
- void shout();
- }
- /*
- **extends可以理解为全盘继承了父类的功能,不需要重run方法。
- */
- interface Animal extends Runner
- {
- int ID=96;
- void breathe();
- }
-
- class Fish implements Animal
- {
- int ID=98;
- static int fish_id=10;
- public void run ()
- {
- System.out.println("fish is swimming");
- }
- public void breathe()
- {
- System.out.println("fish is bubbing");
- }
- }
-
-
- class Bird implements Runner , Flyer
- {
-
- public void run ()
- {
- System.out.println("the bird is running");
- }
- public void fly ()
- {
- System.out.println("the bird is flying");
- }
- }
-
- abstract class LandAnimal implements Animal
- {
-
- public void breathe()
- {
- System.out.println("LandAnimal is breathing");
- }
- }
- /*
- **********************************************************************************************
- ****如果将一个类设置为abstract,则此类必须被继承使用。此类不可生成对象,必须被继承使用。
- ****Abstract可以将子类的共性最大限度的抽取出来,放在父类中,以提高程序的简洁性。下面的例子中 FlyAnimal就不需要重写Animal以及Flyer接口中的方法
- ****Abstract虽然不能生成对象,但是可以声明,作为编译时类型,但不能作为运行时类型。
- ****Final和abstract永远不会同时出现。
- ***********************************************************************************************
- */
- abstract class FlyAnimal implements Animal , Flyer
- //class FlyAnimal implements Animal , Flyer//error,FlyAnimal不是抽象的, 并且未覆盖Runner中的抽象方法run()
- {
-
- public void breathe()
- {
- System.out.println("LandAnimal is breathing");
- }
-
- }
- /*
- **********************************************************************************************
- ***抽象类存在的意义是用来被继承的。一个类继承了一个抽象类,必须实现抽象类里面所有的抽象方法,否则,此类也是抽象类。
- ***例如下面的swallow燕子类继承FlyAnimal,必须实现抽象类FlyAnimal里面的breathe()和run ()和fly ()方法以及Shouter类里面的shout()方法
- ***********************************************************************************************
- */
- class Swallow extends FlyAnimal implements Shouter
- //class Swallow implements FlyAnimal , Shouter//error错误: implements后面需要跟接口
- {
-
- public void breathe()
- {
- System.out.println("Swallow is breathing");
- }
- public void run ()
- {
- System.out.println("Swallow is runing");
- }
- public void fly ()
- {
- System.out.println("Swallow is flying");
- }
- public void shout()
- {
- System.out.println("Swallow is shouting");
- }
- }
-
- public class extends_and_implements
- {
- public static void main (String args[])
- {
- int j = 0;
- System.out.println("extends_and_implements class!");
- Fish mFish = new Fish();
- Swallow m_Swallow=new Swallow();
- //Animal mAnimal=new Animal();
-
- //FlyAnimal mFlyAnimal= new FlyAnimal();//错误: FlyAnimal是抽象的; 无法实例化
- mFish.run();
- System.out.println("mFish.ID="+mFish.ID);
- j = Runner.ID;
- System.out.println("Runner.ID="+j);
-
- System.out.println("Fish.fish_id="+Fish.fish_id);
- Fish.fish_id=20;
- System.out.println("Fish.fish_id="+Fish.fish_id);
- //Swallow.breathe();//error,错误: 无法从静态上下文中引用非静态 方法 breathe()
- m_Swallow.breathe();
- }
- }

2、编译&执行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。