JAVA Programming Oriented to Interface Programming and "Open-Closed" Principle

1. Purpose of the experiment

On the basis of abstract-oriented programming, the core idea of ​​mastering the use of interfaces for programming is to use interface callbacks. Knowing that interfaces can also reflect the "open-close" principle of programming, that is, open for extension and closed for modification.

2. Experimental requirements

A deep understanding of interface-oriented programming and the "open-close" principle requires the definition of an interface variable, which stores a reference to the object of the class that implements the interface, so that the interface variable can call back the interface method implemented by the class.

3. Experimental steps:

1) Define the interface and declare several abstract methods.
2) Create a class, and define several methods in the class whose parameters are interface types.
3) The interface method rewritten by the object callback class of the class that implements the interface
4) Realize the corresponding function

4. Experiment code

(1) Define interfaces and passengers

Passenger.java

package huidiao;

interface Pay{
    
          //定义接口Pay
	double howMuch();  //计算需要多少钱
	String showType();  //显示现在所乘坐的车的类型
	void paid();    //方法:已支付
}
public class Passenger {
    
    
     public void setPay(Pay p) {
    
       //乘车人支付
    	 double s=p.howMuch();  //接口回调
    	 String m=p.showType();  //接口回调
    	 System.out.println("司机:"+m+"已经到达目的地,一共"+s+"元,请扫码支付");
    	 System.out.println("乘客:好的,麻烦师傅等我拿一下手机......");
    	 System.out.println("手机:叮咚!");
    	 System.out.println("顾客:好了,师傅");
    	 p.paid(); //接口回调
     }
}

(2) car driver

CarDriver.java

package huidiao;
public class CarDriver implements Pay {
    
        //定义私家车类使用Pay接口
   public double kilo;              //成员变量公里数
   public  Passenger passenger;    //公有成员变量乘车人
   public CarDriver(Passenger passenger) {
    
      //构造函数
	   this.passenger=passenger;
   }
   public void setKilo(double k) {
    
         //行驶里程
	   this.kilo =k;
   }
   public String showType() {
    
    
	   return "私家车";
   }
   public double howMuch() {
    
             //怎么收费
	   double m;
	   m=8+1.5*(kilo-3);
	   return m;
   }
   public void paid() {
    
               //对接口方法paid的重写
	   System.out.println("司机:已经收到车钱!");
	   System.out.println("司机:感谢您的乘坐,祝您旅途愉快");
   }
   public void pleasePay() {
    
        //司机发起收钱
	   
	   passenger.setPay(this);
   }
}

(3) Bus driver

Bus.java
package huidiao;

public class Bus implements Pay {
    
        //定义公交类使用Pay接口
	   public double kilo;              //成员变量公里数
	   public  Passenger passenger;    //公有成员变量乘车人
	   public Bus(Passenger passenger) {
    
      //构造函数
		   this.passenger=passenger;
	   }
	   public void makeKilo(double k) {
    
         //行驶里程
		   this.kilo =k;
	   }
	   public String showType() {
    
    
		   return "公交车";
	   }
	   public double howMuch() {
    
             //怎么收费
		   double m;
		   m=kilo/2.0;
		   return m;
	   }
	   public void paid() {
    
               //对接口方法paid的重写
		   System.out.println("司机:已经收到车钱!");
		   System.out.println("司机:感谢您的乘坐,祝您旅途愉快");
	   }
	   public void pleasePay() {
    
        //司机发起收钱
		   
		   passenger.setPay(this);
	   }
}

(4) Main class

Mainclass.java
package huidiao;
import java.util.Scanner;
public class Mainclass {
    
    

	public static void main(String[] args) {
    
    
		// TODO 自动生成的方法存根
     Passenger Jack  = new Passenger();     //乘客Jack
     CarDriver Jan =  new CarDriver(Jack);  //私家车Jan
     Bus A189 = new Bus(Jack);              //公交车A189
     Scanner reader=new Scanner(System.in);
     double x;
     System.out.println("请输入里程数:");
     x=reader.nextDouble();                //输入里程数
     Jan.setKilo(x);
     Jan.pleasePay();
     Jan.paid();                           //Jack乘坐私家车行驶x米要付的钱
     System.out.printf("***********************************\n");
     A189.makeKilo(x);
     A189.pleasePay();
     A189.paid();                         //Jack乘坐公交车行驶x米要付的钱
     
	}

}

(5) Description

This program fully embodies the "open-close" principle of the interface. If you need to add another way of riding, you only need to rewrite a class to implement the interface Pay, and the Passenger class does not need to make any changes (that is, the Passenger class Class modification is closed).

Guess you like

Origin blog.csdn.net/m0_53788135/article/details/124203403