建了一个群:908722740 ,欢迎小伙伴门的加入,平时可以互相学习交流,不管是学习还是工作上的都可以多多交流,本人在校学生,技术有限,错误的地方欢迎指正。
目录
一、多态案例素材
【1】乐手弹奏乐器
编写程序实现乐手弹奏乐器。乐手可以弹奏不同的乐器从而发出不同的声音。可以弹奏的乐器包括二胡、钢琴和琵琶。
实现思路及关键代码
- 定义乐器类Instrument,包括方法makeSound()
- 定义乐器类的子类:二胡Erhu、钢琴Piano和小提琴Violin
- 定义乐手类Musician,可以弹奏各种乐器play(Instrument i)
- 定义测试类,给乐手不同的乐器让他弹奏
//二胡类
public class Erhu extends Instrument {
public void makeSound(){
System.out.println("拉二胡");
}
}
//钢琴类
public class Piano extends Instrument {
public void makeSound(){
System.out.println("弹钢琴");
}
}
//小提琴类
public class Violin extends Instrument{
public void makeSound(){
System.out.println("拉小提琴");
}
}
//乐器类
public abstract class Instrument {
public void makeSound(){
}
}
//乐手类
public class Musician {
public void play(Instrument instrument){
instrument.makeSound();
}
}
//测试类
public class Test {
public static void main(String[] args) {
Musician musician=new Musician();
Erhu erhu=new Erhu();
musician.play(erhu);
Piano p=new Piano();
musician.play(p);
Violin v=new Violin();
musician.play(v);
}
}
【2】比萨制作
编写程序实现比萨制作。需求说明编写程序,接收用户输入的信息,选择需要制作的比萨。可供选择的比萨有:培根比萨和海鲜比萨。
实现思路及关键代码
- 分析培根比萨和海鲜比萨
- 定义比萨类
- 属性:名称、价格、大小
- 方法:展示
- 定义培根比萨和海鲜比萨继承自比萨类
- 定义比萨工厂类,根据输入信息产生具体的比萨对象
程序运行结果如图所示
//海鲜披萨
public class Hxpizza extends Pizza {
private String peiliao;
public Hxpizza() {
}
public Hxpizza(String name, int price, int size, String peiliao) {
super(name, price, size);
this.peiliao = peiliao;
}
public String getPeiliao() {
return peiliao;
}
public void setPeiliao(String peiliao) {
this.peiliao = peiliao;
}
public void show() {
System.out.println("名称:" + getName());
System.out.println("价格:" + getPrice() + "元");
System.out.println("大小:" + getSize() + "寸");
System.out.println("配料:" + peiliao);
}
}
//培根披萨
public class Pgpizza extends Pizza {
private int keshu;
public Pgpizza() {
}
public Pgpizza(String name, int price, int size, int keshu) {
super(name, price, size);
this.keshu = keshu;
}
public int getKeshu() {
return keshu;
}
public void setKeshu(int keshu) {
this.keshu = keshu;
}
public void show() {
System.out.println("名称:" + getName());
System.out.println("价格:" + getPrice() + "元");
System.out.println("大小:" + getSize() + "寸");
System.out.println("克数:" + keshu);
}
}
//披萨类
public class Pizza {
private String name;
private int price;
private int size;
public Pizza() {
}
public Pizza(String name, int price, int size) {
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 void show() {
System.out.println("名称:" + name);
System.out.println("价格:" + price);
System.out.println("大小:" + size);
}
}
//工厂类
import java.util.Scanner;
public class Pizzaplant {
public Pizza makePizza(int x) {
Pizza pizza=null ;
Scanner input = new Scanner(System.in);
switch (x) {
case 1:
System.out.println("请输入培根克数:");
int keshu = input.nextInt();
System.out.println("请输入披萨大小:");
int size = input.nextInt();
System.out.println("请输入披萨价格:");
int price = input.nextInt();
pizza = new Pgpizza("培根披萨", price, size, keshu);
break;
case 2:
System.out.println("请输入配料信息:");
String peiliao = input.next();
System.out.println("请输入披萨大小:");
int size2 = input.nextInt();
System.out.println("请输入披萨价格:");
int price2 = input.nextInt();
pizza = new Hxpizza("海鲜披萨", price2, size2, peiliao);
break;
}
return pizza;
}
}
//测试类
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("请选择要制作的披萨(1.培根披萨 2.海鲜披萨):");
Scanner input = new Scanner(System.in);
Pizzaplant pi = new Pizzaplant();
//制作
Pizza pizza = pi.makePizza(input.nextInt());
pizza.show();
//展示
}
}
【3】购买饮料
编写程序实现饮料购买:
编写程序,接收用户输入的信息,选择购买的饮料。
可供选择的饮料有:咖啡、矿泉水和可乐。
其中,购买咖啡时可以选择:加糖、加奶还是什么都不加。
购买可乐时可以选择:买可口可乐还是百事可乐。
程序运行效果如图所示:
//咖啡
public class Coffee extends Drinks{
private String peiliao;
public Coffee(String name, int number, String peiliao) {
super(name, number);
this.peiliao = peiliao;
}
public String getPeiliao() {
return peiliao;
}
public void setPeiliao(String peiliao) {
this.peiliao = peiliao;
}
public void buyinformation(){
System.out.println("您购买的饮料信息如下:");
System.out.println("名称:"+getName());
System.out.println("添加配料:"+peiliao);
System.out.println("容量:"+getNumber());
}
}
//可乐
public class Coke extends Drinks{
private String type;
public Coke(String name, int number, String type) {
super(name, number);
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void buyinformation(){
System.out.println("您购买的饮料信息如下:");
System.out.println("名称:"+getName());
System.out.println("类型:"+type);
System.out.println("容量:"+getNumber());
}
}
//饮料
public class Drinks {
private String name;
private int number;
public Drinks(String name, int number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public void buyinformation(){
System.out.println("您购买的饮料信息如下:");
}
}
//矿泉水
public class Mineralwater extends Drinks {
public Mineralwater(String name, int number) {
super(name, number);
}
public void buyinformation(){
System.out.println("您购买的饮料信息如下:");
System.out.println("名称:"+getName());
System.out.println("容量:"+getNumber());
}
}
//工厂
import java.util.Scanner;
public class Plant {
public Drinks makeDrinks(int x){
Drinks drinks=null;
Scanner input=new Scanner(System.in);
switch (x){
case 1:
System.out.println("请输入购买容量:");
int number=input.nextInt();
System.out.println("请问是否需要配料:(1.加糖 2.加奶 3.什么都不加)");
int y=input.nextInt();
String p;
if (y==1){
p="加糖";
}else if (y==2){
p="加奶";
}else{
p="什么都不加";
}
drinks=new Coffee("咖啡",number,p);
break;
case 2:
System.out.println("请输入购买容量:");
int number1=input.nextInt();
System.out.println("请选择类型(1.可口可乐2.百事可乐):");
int y1=input.nextInt();
String p1;
if (y1==1){
p1="可口可乐";
}else {
p1="百事可乐";
}
drinks=new Coke("可乐",number1,p1);
break;
case 3:
System.out.println("请输入购买容量:");
int number3=input.nextInt();
drinks=new Mineralwater("矿泉水",number3);
}
return drinks;
}
}
//测试
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("请选择饮料(1.咖啡 2.可乐 3.矿泉水):");
Scanner input=new Scanner(System.in);
Plant p=new Plant();
Drinks drinks= p.makeDrinks(input.nextInt());
drinks.buyinformation();
}
}
二、接口案例素材
【1】兔子和青蛙
编写程序描述兔子和青蛙
需求说明:使用面向对象的思想,设计自定义类描述兔子和青蛙。
实现思路及关键代码
- 分析兔子和青蛙的共性
- 根据共性,定义抽象的动物类
属性:名字、颜色、类别(哺乳类、非哺乳类)
方法:吃饭,发出叫声
- 根据青蛙会游泳 抽象游泳的接口
方法:游泳
- 定义兔子继承动物类,青蛙继承动物同时实现游泳接口
程序运行结果如图所示。
//动物类
public abstract class Animal {
private String name;
private String colour;
private String type;
public Animal(String name, String colour, String type) {
this.name = name;
this.colour = colour;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public abstract void eat();
public abstract void cry();
}
//青蛙
public class Frog extends Animal implements Swimming {
public Frog(String name, String colour, String type) {
super(name, colour, type);
}
@Override
public void eat() {
System.out.println("青蛙是"+super.getType()+",爱吃昆虫");
}
@Override
public void cry() {
System.out.println("那只"+super.getColour()+"的,名叫"+super.getName()+"的青蛙正在呱呱的叫");
}//青蛙
@Override
public void swimming() {
System.out.println("虽然不是鱼,但是青蛙也是泳坛高手");
}
}
//兔子
public class Rabbit extends Animal{
public Rabbit(String name, String colour, String type) {
super(name, colour, type);
}
@Override
public void eat() {
System.out.println("兔子是"+super.getType()+",爱吃胡萝卜");
}
@Override
public void cry() {
System.out.println("那只"+super.getColour()+"的,名叫"+super.getName()+"的兔子正在叽叽的叫");
}
}
//游泳接口
public interface Swimming {
public void swimming();
}
//测试
public class Test {
public static void main(String[] args) {
Animal frog=new Frog("大兵","绿色","非哺乳类");
Animal rabbit=new Rabbit("美人","黑色","哺乳类");
rabbit.cry();
rabbit.eat();
System.out.println("--------------");
frog.cry();
frog.eat();
Swimming swimming= (Swimming) frog;
swimming.swimming();
}
}
【2】影视歌三栖艺人
编写程序描述影视歌三栖艺人
需求说明:请使用面向对象的思想,设计自定义类,描述影视歌三梄艺人。
实现思路及关键代码
- 分析影视歌三栖艺人的特性
- 可以演电影
- 可以演电视剧
- 可以唱歌
- 定义多个接口描述特性
- 演电影的接口-----方法:演电影
- 演电视剧的接口-----方法:演电视剧
- 唱歌的接口-----方法:唱歌
- 定义艺人类实现多个接口
程序运行结果如图所示。
//演员
public class Actor implements Filmable,Tvable,Singable{
private String name;
public Actor(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sing(){
System.out.println("我会唱歌");
}
public void showtv(){
System.out.println("我能演电视剧");
}
public void film(){
System.out.println("我能演电影");
}
public void information(){
System.out.println("大家好,我是"+name);
}
}
//演电影接口
public interface Filmable {
public void film();
}
//唱歌接口
public interface Singable {
public void sing();
}
//演电视剧接口
public interface Tvable {
public void showtv();
}
//测试
public class Test {
public static void main(String[] args) {
Actor actor=new Actor("马素素");
actor.information();
actor.film();
actor.showtv();
actor.sing();
}
}
标签:Java,String,System,name,面向对象,详解,println,public,out
From: https://blog.csdn.net/m0_62936414/article/details/136896550