接口实践;接口与抽象类
1.接口声明:
2.接口实现:package JavaInterfaceStudy;
/**
*接口实践
* @author scown
* @param <T>
*/
public interface UseMouth <T>{
void eat(T parm);
void speak(T parm);
}
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.接口与抽象类: