编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();
然后写一个类Print实现接口InterfaceA和InterfaceB,要求printCapitalLetter()方法实现输出大写英文字母表的功能,printLowercaseLetter()方法实现输出小写英文字母表的功能。
再写一个主类E,在主类E的main方法中创建Print的对象并赋值给InterfaceA的变量a,对象a调用printCapitalLetter方法;最后再在主类E的main方法中创建Print的对象并赋值给InterfaceB的变量b,对象b调用printLowercaseLetter方法。
1 public interface InterfaceA { 2 3 void printCapitalLetter(); 4 }
1 public interface InterfaceB { 2 3 void printLowercaseLetter(); 4 }
1 public class Print implements InterfaceA, InterfaceB { 2 3 @Override 4 public void printLowercaseLetter() { 5 // 输出小写字母表 6 for (char i = 'a'; i <= 'z'; i++) { 7 System.out.print(i + " "); 8 if (i == 'g' || i == 'n' || i == 't' || i == 'z') { 9 System.out.println(); 10 } 11 if (i == 'q' || i == 'w') { 12 System.out.print(" "); 13 } 14 } 15 16 } 17 18 @Override 19 public void printCapitalLetter() { 20 // 输出大写字母表 21 for (char i = 'A'; i <= 'Z'; i++) { 22 System.out.print(i + " "); 23 if (i == 'G' || i == 'N' || i == 'T' || i == 'Z') { 24 System.out.println(); 25 } 26 if (i == 'Q' || i == 'W') { 27 System.out.print(" "); 28 } 29 } 30 31 } 32 33 }
1 public static void main(String[] args) { 2 3 //打印小写字母表 4 InterfaceB b = new Print(); 5 b.printLowercaseLetter(); 6 7 //打印大写字母表 8 InterfaceA a = new Print(); 9 a.printCapitalLetter(); 10 11 }
运行结果: