JAVA 接口与实现例题1

本文通过多个示例展示了Java接口的定义与实现方法,包括接口的回调、接口作为参数传递以及接口与抽象类的区别。此外,还介绍了如何利用接口实现多态,使程序更加灵活和易于扩展。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现接口

public interface Computable {
      int MAX=46;
      int f(int x);
}

public class China implements Computable { //China类实现Computable接口
	int number;
	public int f(int x) {
		int sum=0;
		for(int i=1;i<=x;i++){
			sum=sum+i;
		}
	 return sum;
	}
     
}

public class Japan implements Computable{
    int number;
    public int f(int x) {
		return MAX+x;  //直接使用接口中的常量
	}
}
public class exma {

	public static void main(String[] args) {
		China zhang;
		Japan henlu;
		zhang=new China();
	    henlu=new Japan();
	    zhang.number=32+Computable.MAX;
	    henlu.number=14+Computable.MAX;
	    System.out.println("zhang的学号"+zhang.number+",zhang求和结果"+zhang.f(100));
	    System.out.println("henlu的学号"+henlu.number+",zhang求和结果"+henlu.f(100));
	}

}


接口回调


interface showMess{
	void 显示商标(String s);
}
class TV implements showMess{
	public void 显示商标(String s) {
		System.out.println(s);
	}
}
class PC implements showMess{
	public void 显示商标(String s) {
		System.out.println(s);
	}
}
public class exam {

	public static void main(String[] args) {
		showMess sm; //声明接口变量
		sm=new TV();   //接口变量中存放对象的引用
		sm.显示商标("长城牌电视机"); //接口回调
		sm=new PC();
		sm.显示商标("联想奔月5008PC机");

	}

}

interface CompurerAverage{
	public double average(double a,double b);
}
class A implements CompurerAverage{
	public double average(double a,double b) {
		double aver=0;
		aver=(a+b)/2;
		return aver;
	}
}
public class abc {

	public static void main(String[] args) {
	

	}

}

接口与多态

interface CompurerAverage{
	public double average(double a,double b);
}
class A implements CompurerAverage{
	public double average(double a,double b) {
		double aver=0;
		aver=(a+b)/2;
		return aver;
	}
}
class B implements CompurerAverage{
	public double average(double a,double b){
	 double aver=0;
	 aver=Math.sqrt(a*b);
	 return aver;
	}
}
public class lianxi {
	public static void main(String[] args) {
	CompurerAverage computer;
	double a =11.23,b=22.78;
	computer =new A();
	double result=computer.average(a, b);
	System.out.printf("%5.2f和%5.2的算术平均值:%5.2f\n",a,b,result);
	computer =new B();
	result=computer.average(a, b);
	System.out.printf("%5.2f和%5.2的几何平均值:%5.2f",a,b,result);

	}

}

接口参数

interface SpeakHello{
	void speakHello();
}
class Chinese implements SpeakHello{
	public void speakHello(){
		System.out.println("中国人习惯问候语:你好,吃饭了吗?");
	}
}
class English implements SpeakHello{
	public void speakHello(){
		System.out.println("英国人习惯问候语:你好,天气不错");
	}
}
class KindHello{
	public static KindHello kindHello;

	public static void lookHllo(SpeakHello hello) {      //接口类型参数
		hello.speakHello();                    //接口回调
	}
}
public class lianxi {
	public static void main(String[] args) {
	KindHello.kindHello=new KindHello();
	KindHello.lookHllo(new Chinese());
	KindHello.lookHllo(new English());

	}

}

interface A{
	double f(double x,double y);
}
class B implements A{
	public double  f(double x,double y) {
		return x*y;
	}
	int g(int a,int b){
		return a+b;
	}
}
public class E {
	public static void main(String[] args) {
       A a=new B();
       System.out.println(a.f(3, 5));
       B b =(B)a;
       System.out.println(b.g(3, 5));
	}
}

interface Com{
	int add(int a,int b);
}
abstract class A{
	 abstract int add(int a,int b);

}
class B extends A implements Com{
	public int add(int a,int b) {
		return a+b;
	}
}
public class E {
	public static void main(String[] args) {
       B b=new B();
       Com com=b;
       System.out.println(com.add(12, 6));
       A a=b;
       System.out.println(a.add(10, 5));
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值