Java练习——接口习题

1、

interface Interface1{

//接口Interface1
String S = "GOOD";//常量字符串S
void f();//f的抽象方法
}
abstract class ClassA{
abstract void g();//g的抽象方法
}
class ClassB extends ClassA implements Interface1{
void g(){
//覆写ClassA的抽象方法
System.out.print(S);
}
public void f(){
//覆写f()的抽象方法
System.out.print(" "+S);
}

public class Test4{
public static void main(String[] args) {
ClassA a = new ClassB();//向上转型
Interface1 b = new ClassB();
a.g();//调用g()方法
b.f();//调用f()方法
}

}


2、

利用接口做参数,写个计算器,能完成加减乘除运算。 
1)定义一个接口Compute含有一个方法int computer(int n, int m)。 
2)设计四个类分别实现此接口,完成加减乘除运算。 
3)设计一个类UseCompute,类中含有方法:public void useCom(Compute com, int one, int two),此方法能够用传递过来的对象调用computer方法完成运算,并输出运算的结果。 

4)设计一个主类Test,调用UseCompute中的方法useCom来完成加减乘除运算。 

interface ICompute{
//接口
int computer(int a,int b);
}


//四个类实现接口
class Add implements ICompute{
public int computer(int a,int b){
return a+b;
}
}
class Minus implements ICompute{
public int computer(int a,int b){
return a-b;
}
}
class Multiply implements ICompute{
public int computer(int a,int b){
return a*b;
}
}
class Divide implements ICompute{
public int computer(int a,int b){
return a/b;
}
}


class UseComputer{
public void useCom(ICompute com,int one,int two){
//ICompute com就是ICompute com =new ICompute(); com.computer 
        //new个com对象,对象调用方法,把one和two传参进去  
System.out.println(com.computer(one,two));
}
}
public class Test4{
public static void main(String[] args) {
UseComputer user = new UseComputer();
Add s1= new Add();
System.out.print("和为:");
user.useCom(s1,12,4);
Minus s2= new Minus();
System.out.print("差为:");
user.useCom(s2,12,4);
Multiply s3 = new Multiply();
System.out.print("乘积为:");
user.useCom(s3,12,4);
Divide s4 = new Divide();
System.out.print("商为:");
user.useCom(s4,12,4);
}
}

Java练习——接口习题_第1张图片

4、1)定义接口A,里面包含值为3.14的常量PI和抽象方法double area()。 
(2)定义接口B,里面包含抽象方法void setColor(String c)。 
(3)定义接口C,该接口继承了接口A和B,里面包含抽象方法void volume()。 
(4)定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:底圆半径radius、 
圆柱体的高height、颜色color。 

(5)创建主类来测试类Cylinder。

interface IA{
public static final double Pi = 3.14;
double area();
}


interface IB{
void setColor(String c);
}


interface IC extends IA,IB{
void volume();
}


class Cylinder implements IC{
private double radius;
private double height;
private String color; 
public void setRadius(){
this.radius = radius;
}
public double getRadius(){
return this.radius;
}
public void setHeight(){
this.height = height;
}
public double getHeight(){
return this.height;
}
public void setColor(){
this.color = color;
}
public String getColor(){
return this.color;
}
public Cylinder(double radius,double height,String color){
this.radius = radius;
this.height = height;
this.color =  color;
}
public double area(){
return Pi*radius*radius;
}
public void setColor(String c){
this.color = c;
System.out.println(this.color);
}
public void volume(){
System.out.println(Pi*radius*radius*height);
}
}
public class Test4{
public static void main(String[] args) {
Cylinder cy1= new Cylinder(2,4,"yellow");
System.out.println(cy1.area());
cy1.setColor("red");
cy1.volume();
}
}

Java练习——接口习题_第2张图片

你可能感兴趣的:(Java练习题集)