接口,下面是百度来的接口的定义:
说白了,接口就是一类事物抽象出来的共同特征。接口是完全抽象类,是由常量和抽象方法组成的特殊的类
下面是一个生物接口:
/**
* interface s member variables must be public static final like public static final PI = 3.14159
* @author Administrator
*
*/
public interface Biology_Inter {
/**
* interface s methods must be public abstract without body
*/
public abstract void breath();
}
接口可以继承,并且在java中的接口可以多重继承,下面是动物类:
/**
* interface inherit another interface using extends keywords
* one interface can inherit more than one interface
* but class can only extends one class
* class can impelemts more than one interfaces
* @author Administrator
*
*/
public interface Animal_Inter extends Biology_Inter{
public abstract void move();
public abstract void ate();
}
下面是人类:
public interface Man_Inter {
public abstract void think();
public abstract void study();
}
下面我们介绍一下抽象类,下面是百度来的抽象类的定义,简单明了:
抽象类就是对一类事物的抽象,跟接口有点像,但是接口是一类事物的共同行为的抽象,是一个抽象行为的集合。抽象类是抽象的一类事物。比如猴子可以是抽象类,因为猴子可以具体化成各种种类的猴子。动物行为集合可以作为动物接口,把动物的共同特征都抽出来做成集合。这个抽象的程度主要看具体的应用情况,猴子也可以作为一个具体类。
下面是猴子的抽象类:
import For_Interface.Animal_Inter;
import For_Interface.Biology_Inter;
/**
* abstract class have no constructed function because it cannot realized
* @author Administrator
*
*/
public abstract class Monkey_Cla implements Animal_Inter,Biology_Inter{
public void breath() {
System.out.println("im monkey im breathing !");
}
public void move() {
System.out.println("im monkey im moving !");
}
public void ate() {
System.out.println("im monkey im eating !");
}
public abstract void eat_Banana();
public abstract void hunting();
}
抽象类没有构造函数,可以实现接口,有抽象方法。
下面是人类的具体类:
import For_Interface.Man_Inter;
/**
* not abstract class have to override the methods which implements from interfaces
* non abstract class can also be extends
* non abstract class extends abstract class have to override the methods also
* @author Administrator
*
*/
public class Human_Cla extends Monkey_Cla implements Man_Inter{
/**
* non-parameter constructor
*/
public Human_Cla() {
System.out.println("im the non-parameter constructor of human class !");
}
@Override
public void think() {
System.out.println("im a human im thinking now !");
}
@Override
public void study() {
System.out.println("im a human im studying now !");
}
@Override
public void eat_Banana() {
System.out.println("im a human im eating banana now !");
}
@Override
public void hunting() {
System.out.println("im a human im hunting now !");
}
}
具体类必须实现其所继承的抽象父类的所有抽象方法,有抽象方法的必须是抽象类,抽象类可以有具体方法。具体类可以定义自己的方法。具体类有自己的构造函数,可以显示定义也可以隐式定义。
下面是学生继承人类的类:
/**
* normal class extends an normal class
* there s no methods to override
* you can use the object s methods also can use the methods of it s super class
* @author Administrator
*
*/
public class Student_Cla extends Human_Cla {
/**
* non-parameter constructor of student class
*/
public Student_Cla() {
System.out.println("im the non-parameter constructor of student class !");
}
/**
* constructor with a parameter of student class
* @param s
*/
public Student_Cla(String s) {
System.out.println("im the constructor of student class with parameter ! im gonna sing "+s);
}
public void Do_Homework() {
System.out.println("im a student im doing homework now ! dont fucking border me bitch!");
}
}
下面我们写一个主方法来测试这条继承链上构造方法的实现步骤:
public class Test_All {
public static void main(String[] args) {
Student_Cla student = new Student_Cla();
Student_Cla student2 = new Student_Cla("666");
}
}
可以看到我们的继承链是人类具体类继承猴子抽象类,并且实现了其抽象方法。然后学生具体类继承了人类具体类。其构造函数的调用顺序是从最上层的具体类一直往下调用的。即学生继承人类具体类,先调用人类的构造函数,然后再调用自身的构造函数。
并且大家可以看到,当new一个Student的时候我们输入一个串,java会通过动态绑定区分不同的构造函数。