|
public class Test4 { public void f1(final int i){ |
| abstract class A { private int i=1; //在抽象类中可以定义变量 abstract int aa(int x,int y); abstract void bb(); public void cc() //在抽象类中可以定义一般的方法 {System.out.println(i);} } class B extends A { int aa(int x,int y){ return 1; } //方法中必须有一个反回值 void bb(){} public static void main(String[] args) |
interface Runner
{ public static final int ID=1;或 int ID=1 // 定义常量,可将public static final 省略,定义的也是常量,因为接口中只能定义常量。
void run();
}
interface Animal extends Runner //继承Runner的接口
{ void breathe();
}
class Fish implements Animal
{ public void run(); //因为接口里声明的类默认是public,而在继承时子类必须比父类的访问权限要高,所以不能直接写void run(),如果不写public编绎时会出错。
{ System.out.println("fish is swimming");
}
public void breathe()
{ System.out.println("fish is bubbling");
}
public static void main(String[] args)
{ Fish f=new Fish();
int j=0;
j=Runner.ID; //去引用Runner中的常量id,因为id默认是static型的,所以用Runner直接调用。
j=f.ID;
j=Fish.ID; //因为Fish类,继承父类,所以可以这样的引用
f.iD=2; //这句错,因为id是常量,不能赋值
}
}
abstract class LandAnimal implements Animal //因为LandAnimal没有继承Animal的所有方法,所以在类前必须声明为抽象类。
{ public void breathe(){}; //?????有一个问题,这样定义一个类以后,因为是抽象类不能定义对象,然而作一个这个类的继承以后,编译出错,还有抽象类里面为什么加{},原程序在我的文档中,Aaa.java
} //问题解决了,抽象类的确不能创建对象,这没错,而要是继承这个类,前面的所有继承都须要被重写,而前面说到的{}则是必须要加到里面的,一个抽象类继承一个接口,抽象类中的方法必须加{},下面写一个继承它的程序,但要是把它再定义成抽象方法,可以这样写,abstract public void breathe() ;
class LAL extends LandAnimal
{ public void breathe(){
System.out.println("hhhhhhhhhhhhhhhhh");
}
public voic run(){}; //子类继承父类是抽象类或接口必须继承父类的所有的方法
} // 这样就没问题了
class Student extends Person implements Runner //假设前面有个Person类,这个就是即继承Person这个类,而且还实现了implements这个接口,但extends必须在implements的前面.
{ public void run();
}
interface Flyer
{ void fly();}
class Bird implements Runner,Flyer //一个类可以同时继承多个接口
{ public void run(){}
public void fly(){}
}

