/**
*
*/
package cn.yanshun.interfaces;
/** 这个类用来测试接口的使用
*/
public class Test02_UseInterface {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//可以创建接口对象,进行测试吗?? ----报错,无法创建接口对象,Cannot instantiate the type Face
//接口是个特殊的抽象类,抽象类不能创建实例化对象
//Face f = new Face();
//怎么测试接口呢??难道是通过创建子类对象,测试使用接口吗?? ----是的
//报黄线:The value of the local variable s is not used
Son4 s = new Son4();
s.pmethod();
s.love();
//s.i = 1; ---- 报错:The final field Face.i cannot be assigned
//s.i + s.s 不是标准语法----The static field Face.i should be accessed in a static way
System.out.println(Face.i);
System.out.println(Face.s);
Face.hate();
/** 通过以上代码可知:接口与子类间,既是继承关系,又是实现关系,或者说实现关系中包含了继承关系,实现是对继承的优化 */
}
}
//既然要测试接口的使用,需要你创建接口
interface Face{
//创建接口的构造方法
//报错:Interfaces cannot have constructors
// public Face(){}
//创建成员方法/属性
//创建成员属性
//报错:The blank final field i may not have been initialized
// int i;
// String s;
/** 接口中,成员属性,在声明时,必须手动初始化,完成赋值。成员变量,必须在首次声明时初始化赋值,说明此成员属性,是一个常量。 */
int i = 0;
String s = "今天有太阳";
//创建成员方法
//接口中的成员方法不能含有方法体
//abstract method
public void pmethod();
//不是abstract method,而是default方法
public default void love() {
//这又是啥方法声明??
System.out.println(i);
}
//不是abstract method,也不是成员方法,而是static方法
public static void hate() {
//这又是啥方法声明??
System.out.println(s);
}
}
//The type Son must implement the inherited abstract method Face.pmethod()
//abstract method Face.pmethod()抽象方法
//inherited继承
//子类、实现类
class Son4 implements Face{
/** ----add unimplemented methods
----make type Son abstract */
@Override
public void pmethod() {
// TODO Auto-generated method stub
System.out.println("今天天气正好:"+s+"/"+i);
}
}