Object Oriented Programming - Chapter 5 Polymorphism Homework

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.

/**
 * Parent class printer (abstract class)
 * @author Lenovo
 *
 */
abstract class Printers {
	/**
	 * abstract method
	 */
	public abstract void print();
}
class DotMatrixPrinter extends Printers {
	/**
	 * Override parent class method
	 */
	public void print() {
		System.out.println("Pin print!");
	}
}
class InkpetPrinter extends Printers {
	/**
	 * Override parent class method
	 */
	public void print() {
		System.out.println("inkjet printing!");
	}
}
class LaserPrinter extends Printers {
	/**
	 * Override parent class method
	 */
	public void print() {
		System.out.println("Laser printing!");
	}
}
public class Printer {
	public static void main(String[] args) {
		Printers printers = new DotMatrixPrinter();//The parent class application points to the child class object
		printers.print();//Output needle printer information
		Printers printers1 = new InkpetPrinter();//The parent class application points to the child class object
		printers1.print();//Output inkjet printer information
		Printers printers2 = new LaserPrinter();//The parent class application points to the child class object
		printers2.print();//Output laser printer information
	}
}

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 come.job.dome;
/**
 *
 * @author master human
 *
 */
public abstract class Master {  

String masterName = "Niuben"; //Master name  
      
String name = " "; //Guest name  
      
String eat = " "; // food to eat  
      
String hobby = " "; // guest's hobby

public void serve(Master master) {
	
	if(master instanceof Friend) {
		
		Friend friend = (Friend)master;
		
		friend.serve();
		
	}else if(master instanceof Friend1) {
		
		Friend1 friend1 = (Friend1)master;
		
		friend1.serve();
	}
}
public abstract void serve(); //abstract hospitality method  
}      
 package come.job.dome;
/**
 *
 * @author Wang Xiaoqiang
 *
 */
public class Friend extends Master{
	public void serve() {
		super.name = "Wang Xiaoqiang";
		super.eat = "Sichuan cuisine";
		super.hobby = "Practice Tai Chi";
		System.out.println(this.masterName+"在招待"+super.name+"吃"+super.eat+","+super.hobby);
	}
}
package come.job.dome;
/**
 *
 * @author john
 *
 */
public class Friend1 extends Master {
	public void serve() {
		super.name = "John";
		super.eat = "pizza";
		super.hobby = "Play football";
		System.out.println(this.masterName+"在招待"+super.name+"吃"+super.eat+","+super.hobby);
	}
}
package come.job.dome;

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Please enter who to visit (1. Wang Xiaoqiang, 2. John):");
		if(input.nextInt()==1) {
			Master master = new Friend();
			master.serve(master);
		}else if(input.nextInt()==2) {
			Master master = new Friend1();
			master.serve(master);
		}
	}
}

Guess you like

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