1、接口方法用于回调 (这里定义接口是为了使用其接口方法):
[size=1em][color=white !important][size=1em]?
[size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
[size=1em]13
[size=1em]14
[size=1em]15
[size=1em]16
[size=1em]17
[size=1em]18
[size=1em]19
[size=1em]20
[size=1em]21
[size=1em]22
[size=1em]23
[size=1em]24
[size=1em]25
[size=1em]26
[size=1em]27
[size=1em]28
[size=1em]29
[size=1em]30
| [size=1em][size=1em]public interface ICallback {
[size=1em] public void func();
[size=1em]}
[size=1em]
[size=1em]public class Caller {
[size=1em] ICallback callback;
[size=1em] public void doCallback() {
[size=1em] callback.func();
[size=1em] }
[size=1em]
[size=1em] public void setCallback(ICallback callback) {
[size=1em] this.callback = callback;
[size=1em] }
[size=1em]
[size=1em]}
[size=1em]
[size=1em]public class MainClass {
[size=1em] public MainClass() {
[size=1em] }
[size=1em]
[size=1em] public static void main(String[] args) {
[size=1em] Caller caller = new Caller();
[size=1em] caller.setCallback(new ICallback () {
[size=1em] public void func() {
[size=1em] System.out.println("dosth");
[size=1em] }
[size=1em] });
[size=1em] caller.doCallback();
[size=1em] }
[size=1em]}
|
2、向上转型
[size=1em][color=white !important][size=1em]?
[size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
[size=1em]13
[size=1em]14
[size=1em]15
[size=1em]16
[size=1em]17
[size=1em]18
[size=1em]19
[size=1em]20
[size=1em]21
[size=1em]22
[size=1em]23
[size=1em]24
[size=1em]25
| [size=1em][size=1em]interface People{
[size=1em] void peopleList();
[size=1em]}
[size=1em]class Student implements People{
[size=1em] public void peopleList(){
[size=1em] System.out.println("I’m a student.");
[size=1em] }
[size=1em]}
[size=1em]class Teacher implements People{
[size=1em] public void peopleList(){
[size=1em] System.out.println("I’m a teacher.");
[size=1em] }
[size=1em]}
[size=1em]public class Example{
[size=1em] public static void main(String args[]){
[size=1em] People a; //声明接口变量
[size=1em] a=new Student(); //实例化,接口变量中存放对象的引用
[size=1em] a.peopleList(); //接口回调
[size=1em] a=new Teacher(); //实例化,接口变量中存放对象的引用
[size=1em] a.peopleList(); //接口回调
[size=1em] }
[size=1em]}
[size=1em]运行结果:
[size=1em]I’m a student.
[size=1em]I’m a teacher.
|
3、常量接口(这里不在讲)
|