Object-Oriented Chapter 5 After-School Exercises

1. Code to create a printing class Printer, define the abstract method print(); create two subclasses, namely the dot matrix printer class DotMatrixPrinter and the inkjet printer class LnkpetPrinter, and rewrite the method print() in their respective classes, write the test class implementation For two kinds of printer printing, add a laser printer subclass LaserPrinter, rewrite the method print(), and modify the test class to realize the printer printing.
package kehouzuoye;

public abstract class Printer {
	public abstract void print();//The abstract method of the parent class, implemented in the subclass
}package kehouzuoye;

public class DotMatrixPrinter extends Printer {
	public void print() {
		System.out.println("The dot matrix printer is printing");
	}
}
package kehouzuoye;

public class LaserPrinter extends Printer {
	public void print() {
		System.out.println("The laser printer is printing");
	}
}package kehouzuoye;

public class LnkpetPrinter extends Printer {
	public void print() {
		System.out.println("Inkjet printer is printing");
	}
}package kehouzuoye;

public class Test {

	public static void main(String[] args) {
		//The parent class reference points to the subclass object DotMatrixPrinter
		Printer pr = new DotMatrixPrinter();
		pr.print();
		//The parent class reference points to the subclass object LnkpetPrinter
		Printer pr2 = new LnkpetPrinter();
		pr2.print();
		//The parent class reference points to the subclass object LaserPrinter
		Printer pr3 = new LaserPrinter();
		pr3.print();
	}
}
2. Please use polymorphism to achieve the following requirements. Niu Ben has two very good friends, one is Wang Xiaoqiang from China, who likes to eat Sichuan food and practice Tai Chi, and the other is John from the United States, who likes to eat pizza and play football. When friends come to visit, Niuben will entertain them according to their own preferences.
package kehouzuoye2;

public abstract class Niu {
	String name;//Name
	String hobby;//hobby
	public abstract void print();//Abstract methods use subclasses to achieve different functions
}package kehouzuoye2;

public class Wang extends Niu {

	@Override
	//Override the parent class abstract method
	public void print() {
		System.out.println("姓名:"+this.name+"喜欢"+this.hobby);

	}
}
package kehouzuoye2;

public class Yue extends Niu {

	@Override
	//Override the parent class abstract method
	public void print() {
		System.out.println("姓名:"+this.name+"喜欢"+this.hobby);

	}
}package kehouzuoye2;

import java.util.Scanner;

public class Test {

	public static void main(String[] args) {
		Niu n = new Wang ();
		n.name = "Wang Xiaoqiang";
		n.hobby = "eat Sichuan food and practice Taijiquan";
		Niu n2 = new Yue();
		n2.name = "John";
		n2.hobby = "eat pizza and play football";
		Scanner input = new Scanner(System.in);
		System.out.println("Who is visiting? (Please enter name:)");
		String name = input.next();
		if (name.equals("王小强")) {
			n.print();
		} else if (name.equals("约翰")) {
			n2.print();
		}
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325906200&siteId=291194637