1、接口
public class InterfaceDemo {
public static void main(String[] args) {
// student p=new student();
// System.out.println(p.getname());
// 这是接口的匿名内部类的写法,在内部把方法实现,就不用再去制造一个类来实例化接口
System.out.println(
new people() {
public String getname() {
return "nice";
}
public int getid() {
return 3;
}
}.getname()
);
}
}
interface people{
String getname();
int getid();
}
//只使用一次很浪费,所以用匿名内部类用来减少类
//class student implements people {
// public String getname() {
// return "nice";
// }
// public int getid() {
// return 3;
// }
//}
2、抽象类
public class InterfaceDemo {
//另一个静态方法
public static void test(people me) {
me.getname();
}
public static void main(String[] args) {
//写法相同
InterfaceDemo.test(
new people() {
public void getname() {
System.out.println("古尔丹");
}
});
}
}
abstract class people{
private int age;
private String name;
public abstract void getname();
}