接口实践;接口与抽象类

1.接口声明:

package JavaInterfaceStudy;

/**

*接口实践

* @author scown

* @param <T>

*/

public interface UseMouth <T>{

    void eat(T parm);

    void speak(T parm);

}

2.接口实现:


package javaClassStudy;

import JavaInterfaceStudy.UseMouth;

/**

*

* @author Administrator

*/

public class Student extends Person implements UseMouth <Student>  {

    public Student(String name, int age) {

        super(name, age);

    }

    @Override

    public String getDescription() {

        System.out.println("Name is:"+this.getName());

        return "Name is:" + this.getName();

    }

    /**

     *

     */

    @Override

    public void live() {

        System.out.println("I live in a big house!"+this.getName());

      }

    @Override

    public void eat(Student parm) {

        System.out.println(this.getName()+"eat songthing!");

    }

    //实现speak方法,当前这个学生讲给另一个学生

    @Override

    public void speak(Student parm) {

        Student otherStudent = parm;

        System.out.println(this.getName()+" speak to " + otherStudent.getName());

    }

}

3.接口应用:

import javaClassStudy.Student;

import javaClassStudy.Person;

public class helloWorld {

    public static void main(String[] parm){

          //reflection 

          //实例一个学生对象

          Student studentScown = new Student("yuxg",12);

          Student StudentOther = new Student("张三",12);

          studentScown.speak(StudentOther);

    }

}

4.接口与抽象类:

a403dfaf-9ba6-4748-bdea-3bb783298a8e

e747c143-30f1-4690-9f30-25225e788f5d

posted @ 2016-04-10 21:00  scown  阅读(309)  评论(0编辑  收藏  举报