Java realizes printer source assembly

Java realizes printer source assembly based on eclipse

Hello, everyone, I am Xiaoyuer. Yanxiao, I am very honored to come to my blog. Today I shared another wave of code. If you like this article, remember three consecutive waves, and everyone is welcome to speak, not much nonsense to run the code.

Preface

The article java code version "1.8.0_202"
This article uses java interface-oriented programming


Tip: The following is the content of this article, the following cases are for reference

PrinterTest.java:

package chaper11.printdemo.iface;
/**程序测试起步类*/
public class PrinterTest {
    
    
	public static void main(String[] args) {
    
    
		Printer printer = new Printer();
		//为打印机安装纸张和墨盒
		printer.setInkbox(new ColorInkBoxImpl());
		printer.setPaper(new A4PaperImpl());
		printer.print("测试内容");
	}
}

Printer.java:

package chaper11.printdemo.iface;
/**
 * 打印机类-使用标准的墨盒和纸张进行打印
 * @author Administrator
 *
 */
public class Printer {
    
    
	private IInkBox inkbox;//打印机中的墨盒
	private IPaper paper;//打印机中的纸张
	/**
	 * 打印机的打印方法-使用墨盒和纸张
	 * content 传递内容给打印机
	 */
	public void print(String content) {
    
    
		if (null == inkbox || null==paper) {
    
    
			System.out.println("墨盒和纸张出现错误,请安装正确后重试!");
			return;
		}
		//打印过程
		String color = inkbox.getColor();
		String type = paper.getSize();
		System.out.println("以下文字是"+ color +"颜色的!");
		System.out.println("使用纸张: "+ type);
		System.out.println("打印内容: "+ content);
	}
	public IInkBox getInkbox() {
    
    
		return inkbox;
	}
	public void setInkbox(IInkBox inkbox) {
    
    
		this.inkbox = inkbox;
	}
	public IPaper getPaper() {
    
    
		return paper;
	}
	public void setPaper(IPaper paper) {
    
    
		this.paper = paper;
	}
}

IPaper.java:

package chaper11.printdemo.iface;
/**
 * 定义获得纸张尺寸的标准方法(接口类) 
 * @author Administrator
 *
 */
public interface IPaper {
    
    
/**
 * 获得纸张的尺寸
 * @return
 */
	String getSize();
}

IInkBox.java:

package chaper11.printdemo.iface;
/**
 * 墨盒接口-定义一个通用方法=获得墨盒颜色(接口类)
 * @author Administrator
 *
 */
public interface IInkBox {
    
    
/**
 * 获得墨盒的颜色
 */
	public String getColor();
}

ColorInkBoxImpl.java:

package chaper11.printdemo.iface;
/**
 * 定义颜色墨盒
 * @作者 YanXiaolxy
 * @版本 2020.03
 * @时间 2021年1月19日 下午11:11:04
 */
public class ColorInkBoxImpl implements IInkBox{
    
    

	@Override
	public String getColor() {
    
    
		// TODO Auto-generated method stub
		return "红色";
	}
}

A4PaperImpl.java:

package chaper11.printdemo.iface;
/**
 * 定义纸张大小
 * @作者 YanXiaolxy
 * @版本 2020.03
 * @时间 2021年1月19日 下午11:30:15
 */
public class A4PaperImpl implements IPaper{
    
    
	@Override
	public String getSize() {
    
    
		// TODO Auto-generated method stub
		return "A4";
	}
}

to sum up

The order of execution of the java program is as follows:
  1. The default values ​​of all attributes in the class (in one fell swoop).
  2. The parent class static property initialization, static block, static method declaration (executed in order of appearance).
  3. Subclass static property initialization, static block, static method declaration (executed in order of appearance).
The above is (the class loading process, does not involve the construction method).
  4. Call the construction method of the parent class, first the non-static member of the parent class is initialized, the construction block, the declaration of the ordinary method (executed in the order of appearance) and then the parent class construction method.
  5. Invoke the construction method of the subclass, first initialize the non-static member of the subclass, construct the block, the declaration of the ordinary method (executed in the order of appearance) and then the subclass construction method.
Among them:
1-3: Class loading process, no construction method is involved.
1-5: The instantiation process, involving construction methods.

Guess you like

Origin blog.csdn.net/yanxiaolxy/article/details/112854662