java关于接口的实例


#接口#

package 接口;
/*
 *  定义一个rec_Area_Peri接口,
 *  里面有两个方法getArea()和getPerimeter(),
 *  分别表示“计算面积”和“计算周长”;
 *  再定义一个Rectangle类,实现接口rec_Area_Peri。
 */
public interface rec_Area_Peri {
	abstract int getArea();
	abstract int getPerimeter();
}



#rectangle类#

package 接口;

class rectangle implements rec_Area_Peri{
	int width;
	int length;
	public rectangle(int w,int l){
		width = w;
		length = l;
	}
	public int getArea(){
		return length * width;
	}
	public int getPerimeter(){
		return 2 * (length + width);
	}
}



#测试Test类#

package 接口;

public class Test_interface {

	public static void main(String[] args) {
		rectangle rect = new rectangle(30, 40);
		System.out.println("矩形的面积:    " + rect.getArea());
		System.out.println("矩形的周长:    " + rect.getPerimeter());
	}

}
//1. 在实现类Rectangle中注释掉一个方法后进行编译,看发生了什么?为什么?
//error:   The type rectangle must implement the inherited abstract method rec_Area_Peri.getArea()

//2. 在实现类Rectangle中将各实现方法前的public修饰符去掉再进行编译,看发生了什么?为什么?
//error:   Cannot reduce the visibility of the inherited method from rec_Area_Peri
//原因:一个类实现一个接口时,必须使接口中的方法是 public 访问控制符

//3. 将接口rec_Area_Peri中定义的两个方法前加上abstract修饰符再编译,看对程序有影响否?为什么?
//没有影响,原因:interface 里的方法默认为隐式 public 和 abstract类型

//4. 将接口rec_Area_Peri中定义的两个方法拆分定义成两个接口,即每个接口只定义一个方法,程序将如何修改?
/*
  
  	public interface rec_Area_Peri1 {
		abstract int getArea();
	
	}

  	public interface rec_Area_Peri2 {
			abstract int getPerimeter();
	
	}

	class rectangle implements rec_Area_Peri1,rec_Area_Peri2{
		int width;
		int length;
		public rectangle(int w,int l){
			width = w;
			length = l;
		}
		public int getArea(){
			return length * width;
		}
		public int getPerimeter(){
			return 2 * (length + width);
		}
	}


 */


测试结果:



其他结果不做测试...........




把上述程序的接口改为抽象类,程序实现如下:


# abstract_rec_Area_Peri类 #

package 抽象类;

abstract class abstract_rec_Area_Peri {

	abstract int getArea();
	abstract int getPerimeter();
}


#rectangle1类#

class rectangle1 extends abstract_rec_Area_Peri {
	int width;
	int length;
	public rectangle1(int w,int l){
		width = w;
		length = l;
	}
	public int getArea(){
		return length * width;
	}
	public int getPerimeter(){
		return 2 * (length + width);
	}
}

#Test_atstract类#

public class Test_atstract {
	
	public static void main(String[] args) {
		rectangle1 rect1 = new rectangle1(80, 90);
		System.out.println("矩形的面积:    " + rect1.getArea());
		System.out.println("矩形的周长:    " + rect1.getPerimeter());

	}

}


测试结果:










  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Java接口编程的实例,该实例演示了如何使用命令模式实现一个简单的遥控器程序: ```java // 定义一个命令接口 interface Command { void execute(); } // 定义一个具体的命令类 class LightOnCommand implements Command { private Light light; public LightOnCommand(Light light) { this.light = light; } public void execute() { light.on(); } } // 定义一个接收者类 class Light { public void on() { System.out.println("Light is on"); } public void off() { System.out.println("Light is off"); } } // 定义一个遥控器类 class SimpleRemoteControl { private Command slot; public SimpleRemoteControl() {} public void setCommand(Command command) { slot = command; } public void buttonWasPressed() { slot.execute(); } } // 测试程序 public class RemoteControlTest { public static void main(String[] args) { SimpleRemoteControl remote = new SimpleRemoteControl(); Light light = new Light(); LightOnCommand lightOn = new LightOnCommand(light); remote.setCommand(lightOn); remote.buttonWasPressed(); } } ``` 该程序中,我们定义了一个命令接口`Command`,并实现了一个具体的命令类`LightOnCommand`,该类实现了`Command`接口,并持有一个接收者对象`Light`。我们还定义了一个遥控器类`SimpleRemoteControl`,该类持有一个命令对象,并提供了一个按钮按下的方法`buttonWasPressed()`,该方法会调用命令对象的`execute()`方法。在测试程序中,我们创建了一个遥控器对象和一个灯对象,并将灯对象包装成一个命令对象,然后将该命令对象设置到遥控器对象中,最后按下遥控器按钮,灯就会被打开。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值