更新时间:2020-07-02 15:25:05 来源:极悦 浏览2324次
1.编写程序实现乐手弹奏乐器。乐手可以弹奏不同的乐器从而发出不同的声音。可以弹奏的乐器包括二胡、钢琴和小提琴。
/**
* 乐器类
* @author Administrator
*
*/
public class Instrument {
public void makeSound(){
System.out.println("发出声音");
}
}
/**
* 钢琴类
* @author Administrator
*
*/
public class Piano extends Instrument{
public void makeSound(){
System.out.println("钢琴声...命运交响曲...");
}
}
/**
* 小提琴类
* @author Administrator
*
*/
public class Violin extends Instrument{
public void makeSound(){
System.out.println("小提琴声音..梁祝...");
}
}
/**
* 二胡类
* @author Administrator
*
*/
public class Erhu extends Instrument{
public void makeSound(){
System.out.println("二胡声音...二泉映月..");
}
}
/**
* 乐手类
* @author Administrator
*
*/
public class Musician {
public static void play(Instrument instrument){
instrument.makeSound();
}
public static void main(String[] args) {
Instrument i1 = new Piano();
play(i1);
Instrument i2 = new Violin();
play(i2);
Instrument i3 = new Erhu();
play(i3);
}
}
2.编写程序实现比萨制作。需求说明编写程序,接收用户输入的信息,选择需要制作的比萨。可供选择的比萨有:培根比萨和海鲜比萨
/**
* 定义抽象类
*/
public abstract class Pizza {
private String name;
private int price;
private int size;
public Pizza() {
super();
}
public Pizza(String name, int price, int size) {
super();
this.name = name;
this.price = price;
this.size = size;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public abstract void show();
}
/**
* 培根比萨
*/
public class BaconPizza extends Pizza{
private int gram;//克数
public BaconPizza() {
super();
}
public BaconPizza(String name, int price, int size, int gram) {
super(name, price, size);
this.gram = gram;
}
public int getGram() {
return gram;
}
public void setGram(int gram) {
this.gram = gram;
}
public void show() {
System.out.println("名称:"+super.getName());
System.out.println("价格:"+super.getPrice()+"元");
System.out.println("大小:"+super.getSize()+"寸");
System.out.println("培根克数:"+this.gram);
}
}
/**
* 海鲜比萨
*/
public class SeaFoodPizza extends Pizza{
private String mixture;//配料
public SeaFoodPizza() {
super();
}
public SeaFoodPizza(String name, int price, int size, String mixture) {
super(name, price, size);
this.mixture = mixture;
}
public String getMixture() {
return mixture;
}
public void setMixture(String mixture) {
this.mixture = mixture;
}
@Override
public void show() {
System.out.println("名称:"+super.getName());
System.out.println("价格:"+super.getPrice()+"元");
System.out.println("大小:"+super.getSize()+"寸");
System.out.println("配料:"+this.mixture);
}
}
/**
* 比萨制作
*/
public class PizzaFactory {
public static Pizza makePizza(){
System.out.println("请选择想要制作的比萨(1.培根比萨 2.海鲜比萨):");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
Pizza pizza = null;
if(i==1){
//制作培根比萨
System.out.println("请输入培根克数:");
int gram = scanner.nextInt() ;
System.out.println("请输比萨大小:");
int size = scanner.nextInt();
System.out.println("请输入比萨价格:");
int price = scanner.nextInt();
pizza = new BaconPizza("培根比萨", price, size, gram);
}else if(i==2){
//制作海鲜比萨
System.out.println("请输入配料信息:");
String mixture = scanner.next();
System.out.println("请输比萨大小:");
int size = scanner.nextInt();
System.out.println("请输入比萨价格:");
int price = scanner.nextInt();
pizza = new SeaFoodPizza("海鲜比萨", price, size, mixture);
}
return pizza;
}
public static void main(String[] args) {
Pizza pizza = makePizza();
pizza.show();
}
}
3.编写程序实现软料购买:编写程序,接收用户输入的信息,选择购买的饮料。可供选择的饮料有:咖啡、矿泉水和可乐。其中,购买咖啡时可以选择:加糖、加奶还是什么都不加。购买可乐时可以选择:买可口可乐还是百事可乐。
//饮料类
public class Drink {
private String name;
private int volume;
public Drink() {
super();
}
public Drink(String name, int volume) {
super();
this.name = name;
this.volume = volume;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
public void show(){
System.out.println("您购买的饮料信息如下:");
System.out.println("名称:"+name);
System.out.println("容量:"+volume);
}
}
//咖啡类
public class Coffee extends Drink{
private String mixture;//添加物
public Coffee() {
super();
}
public Coffee(String name,int volume, String mixture) {
super(name,volume);
this.mixture = mixture;
}
public String getMixture() {
return mixture;
}
public void setMixture(String mixture) {
this.mixture = mixture;
}
public void show(){
System.out.println("您购买的饮料信息如下:");
System.out.println("名称:"+super.getName());
System.out.println("添加配料:"+this.getMixture());
System.out.println("容量:"+super.getVolume());
}
}
//饮料工厂类
public class DrinkFactory {
public static Drink getDrink(String no){
Scanner input = new Scanner(System.in);
Drink drink = null;
if("1".equals(no)){
System.out.println("请输入购买容量:");
int volume = input.nextInt();
System.out.println("请问是否要配料(1、加糖 2、加奶 3、什么也不加)");
int choice = input.nextInt();
String mixture;
switch(choice){
case 1: mixture ="加糖";break;
case 2: mixture ="加奶";break;
default : mixture="什么一个不加";
}
drink = new Coffee("咖啡",volume, mixture);
}else if("2".equals(no)){
//省略
}else if("3".equals(no)){
//省略 }
return drink;
}
}
//测试类
public class TestDrink {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请选择饮料(1.咖啡 2 矿泉水 3 可乐):");
String choice = input.next();
Drink drink = DrinkFactory.getDrink(choice);
drink.show();
}
}
以上就是极悦java培训机构的小编针对“编程基础分享,Java多态的练习题”的内容进行的回答,希望对大家有所帮助,如有疑问,请在线咨询,有专业老师随时为你服务。
0基础 0学费 15天面授
Java就业班有基础 直达就业
业余时间 高薪转行
Java在职加薪班工作1~3年,加薪神器
工作3~5年,晋升架构
提交申请后,顾问老师会电话与您沟通安排学习