/*
java中,一个类实现某个接口,必须重写接口中的所有方法吗?
答:抽象类可以不用,普通类必须
*/
class Demo{
public static void main(String[] args){
/*
Person p = new Person();
Person p2 = new Person();
Person p3 = new Person();
*/
//Animal a = new Cat();
Inst1 myinst = new Inst1();
myinst.fn0();
new My(){//前面必须加public
// 否则:Cannot reduce the visibility of the inherited method from My Java Problem
public void fn0(){}
public int fn1(){}
//void fn2(){} wrong
public void fn2(){}
};
}
}
interface My{
void fn0();
int fn1();
void fn2();
}
//1.抽象类实现某个接口 不必全部重写接口中的方法
abstract class Middle implements My{
public void fn0(){
System.out.println("haha");
}
}
/*2.普通类实现某个接口,必须重写接口中的所有方法吗?
class MyInst implements My{
// The type MyInst must implement the inherited abstract method My.fn1() Java Problem
//The type MyInst must implement the inherited abstract method My.fn2() Java Problem
public void fn0(){
System.out.println("haha,creatte myInst");
}
}
*/
class Inst1 extends Middle implements My{
public int fn1(){
return 1;
}
//如果屏蔽下面两行语句 则会出现没有实现抽象方法的 错误
//The type Inst1 must implement the inherited abstract method My.fn2() Java Problem
public void fn2(){
System.out.println(11);
}
}
class Inst2 extends Middle implements My{
public int fn1(){
return 2;
}
public void fn2(){
System.out.println(22);
}
}
class Inst3 extends Middle implements My{
public int fn1(){
return 3;
}
public void fn2(){
System.out.println(33);
}
}