java接口练习2

1、编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法voidprintCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然后写一个类Print实现接口InterfaceA和InterfaceB,要求printCapitalLetter()方法实现输出大写英文字母表的功能,printLowercaseLetter()方法实现输出小写英文字母表的功能。再写一个主类test,在主类test的main方法中创建Print的对象并赋值给InterfaceA的变量a,对象a调用printCapitalLetter方法;最后再在主类test的main方法中创建Print的对象并赋值给InterfaceB的变量b,对象b调用printLowercaseLetter方法。

 


public interface InterfaceA {
void printCapitalLetter();
}
public interface InterfaceB {
void printLowercaseLetter();
}

public class Print implements InterfaceA,InterfaceB {

@Override
public void printLowercaseLetter() {
char[] chr=new char[26];
char j=65;
for(int i=0;i<=25;i++){
chr[i]=j;
j++;
}
System.out.print("大写字母:");
for(char temp:chr){
System.out.print(temp);
}
System.out.println();
}

@Override
public void printCapitalLetter() {
char[] chr=new char[26];
char j=97;
for(int i=0;i<=25;i++){
chr[i]=j;
j++;
}
System.out.print("小写字母:");
for(char temp:chr){
System.out.print(temp);
}
System.out.println();
}
}

public class test {
public static void main(String[] args){
InterfaceA a =new Print();
a.printCapitalLetter();
InterfaceB b=new Print();
b.printLowercaseLetter();
}
}


2、按要求编写Java程序:

(1)编写一个接口:InterfaceA,只含有一个方法int method(int n);

(2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方法时,要求计算1到n的和;

(3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口方法时,要求计算n的阶乘(n!);

(4)编写测试类E,在测试类E的main方法中使用接口回调的形式来测试实现接口的类。

 

public interface InterfaceA {
void printCapitalLetter();

int method(int n);
}
public class ClassA implements InterfaceA{
@Override
public int method(int n) {
// 1到n的和
int sum=0;
for(int i=0;i<=n;i++){
sum+=i;
}
return sum;
}
public void printCapitalLetter() {

public class ClassB implements InterfaceA{

public int method(int n) {
//n的阶乘
int sum=1;
for(int i=1;i<=n;i++){
sum*=i;
}
return sum;
}

}

public class E {
public static void main(String[] args){
InterfaceA a =new Print();
a.printCapitalLetter();
InterfaceB b=new Print();
b.printLowercaseLetter();

InterfaceA e=new ClassA();
InterfaceA ee=new ClassB();
System.out.println("1到100的和是:"+e.method(100));
System.out.println("10的阶乘是:"+ee.method(10));
}
}

 


3、利用接口做参数,写个计算器,能完成+-*/运算

(1)定义一个接口Compute含有一个方法int computer(int n,int m);

(2)设计四个类分别实现此接口,完成+-*/运算

(3)设计一个类UseCompute,含有方法:public void useCom(Compute com, int one, int two)

此方法要求能够:1.用传递过来的对象调用computer方法完成运算

                      2.输出运算的结果

(4)设计一个测试类,调用UseCompute中的方法useCom来完成+-*/运算

public interface Compute {
int computer(int n,int m);
}

public class Jia implements Compute {
@Override
public int computer(int n, int m) {
return n+m;
}
}


public class Jian implements Compute{

@Override
public int computer(int n, int m) {
return n-m;
}

}

public class Cheng implements Compute {

@Override
public int computer(int n, int m) {
return n*m;
}

}


public class Chu implements Compute {
@Override
public int computer(int n, int m) {
int sum = 0;
try{
sum=n/m;
}catch(Exception e){
System.out.println("被除数不能为0");
}
return sum;
}
}

public class UseCompute {
public void useCom(Compute com, int one, int two){
System.out.println(com.computer(one, two));
}
}

public class Testcompute {

public static void main(String[] args) {
UseCompute uc=new UseCompute();
System.out.print("10+5=");
uc.useCom(new Jia(),10,5);
System.out.print("10-5=");
uc.useCom(new Jian(),10,5);
System.out.print("10X5=");
uc.useCom(new Cheng(),10,5);
System.out.print("10/5=");
uc.useCom(new Chu(),10,5);

uc.useCom(new Chu(),10,0);
}

}

 

 

 


---------------------
作者:pgx115
来源:CSDN
原文:https://blog.csdn.net/pgx115/article/details/75201239
版权声明:本文为博主原创文章,转载请附上博文链接!

posted @ 2018-11-05 10:31  代码缔造的帝国  阅读(326)  评论(0编辑  收藏  举报