系列文章目录
第一章 JAVA集中学习第一周学习记录(上)
第二章 JAVA集中学习第一周项目实践
第三章 JAVA集中学习第一周学习记录(下)
第四章 JAVA集中学习第一周课后习题
第五章 JAVA集中学习第二周学习记录(上)
第六章 JAVA集中学习第二周项目实践
第七章 JAVA集中学习第二周学习记录(中)
第八章 JAVA集中学习第二周课后习题
前言
作为Java前期学习最困难最重要的地方,面向对象编程需要更多的练习,本文通过提供一些习题来帮助客观更好的理解什么是面向对象编程。我认为代码的练习不是看一看就能会的,请各位客官在观看解决方法前,先用两分钟想一想自己应该怎么实现这个功能。 在下攸攸太上,此名即将传遍大江南北,谨记此名早点抱紧大腿。
一、封装练习
提示:封装过程比较简单,本文只会用一道题来练习,但在之后的继承多态的类都会使用封装,所以要牢记实现方法
题目
解题思路
我们需要创建两个类
一个类是题目要求的卡车类,建造好卡车的属性和方法后,通过private关键字修饰所有属性,通过快捷键生成有参无参构造函数以及每个属性的getset方法。
另一个类是测试类,我们在类里写主函数,在主函数里构建卡车对象,对卡车进行测试。
实现代码
卡车类
public class Track {
String name;
int days;
private String CarNumber;
private String CarModel;
private String CarColor;
private int CarWeight;
private int MoneyPerDay;
public Track( String carNumber, String carModel, String carColor, int carWeight) {
setCarNumber(carNumber);
setCarModel(carModel);
setCarColor(carColor);
setCarWeight(carWeight);
}
public int getDays() {
return days;
}
public void setDays(int days) {
if (days > 0)
this.days = days;
else {
System.out.println("输入不符合规范,已赋值为默认值0");
this.days = 0;
}
}
public String getCarNumber() {
return CarNumber;
}
public void setCarNumber(String carNumber) {
this.CarNumber = carNumber;
}
public String getCarModel() {
return CarModel;
}
public void setCarModel(String carModel) {
this.CarModel = carModel;
}
public String getCarColor() {
return CarColor;
}
public void setCarColor(String carColor) {
this.CarColor = carColor;
}
public int getCarWeight() {
return CarWeight;
}
public void setCarWeight(int carWeight) {
this.CarWeight = carWeight;
}
public int getMoneyPerDay() {
return MoneyPerDay;
}
public void setMoneyPerDay() {
this.MoneyPerDay = getDays() * 20;
}
public void rent(String name, int days){
setDays(days);
setMoneyPerDay();
System.out.println("租赁信息");
System.out.println("车牌号:" + getCarNumber());
System.out.println("车型:" + getCarModel());
System.out.println("颜色:" + getCarColor());
System.out.println("载重量:" + getCarWeight());
System.out.println("租车人:" + name);
System.out.println("应付金额:" + getMoneyPerDay());
}
}
测试类
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入租车人姓名:");
String name = scanner.next();
System.out.print("请输入租赁天数:");
int days = scanner.nextInt();
Track car1 = new Track("京A11111", "东风", "蓝色", 500);
car1.rent(name, days);
}
运行结果
二、继承练习
题目
解题思路
两妖怪共有的属性:名字name;生命值life;攻击力attack
两妖怪共有的方法:攻击;移动(需要子类重写)
蛇怪独有方法:大蛇吸血术
实现代码
父类Monster
public class Monster {
private String name;
private int blood;
private int attack;
public Monster(String name, int blood, int attack) {
this.name = name;
this.blood = blood;
this.attack = attack;
}
public Monster() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public void otk(){
System.out.println("怪物" + getName() + "展开攻击");
System.out.println("当前生命值是:" + getBlood());
System.out.println("攻击力是:" + getAttack());
}
public void move(){
}
}
蜈蚣精类CentipedeMonster
public class CentipedeMonster extends Monster{
public CentipedeMonster(String name, int blood, int attack) {
super(name, blood, attack);
}
public CentipedeMonster() {
}
@Override
public void move() {
super.move();
System.out.println("我是蜈蚣精,御风飞行");
}
}
蛇怪类SnakeMonster
public class SnakeMonster extends Monster{
public SnakeMonster(String name, int blood, int attack) {
super(name, blood, attack);
}
public SnakeMonster() {
}
public void BloodRestoreMyBody(){
if (getBlood() < 10)
{
setBlood(getBlood() + 20);
System.out.println("BloodRestoreMyBody,blood is" + getBlood());
}
}
@Override
public void move() {
super.move();
System.out.println("我是蛇怪,我走S型路线");
}
}
测试类TestMonster
public class TestMonster {
public static void main(String[] args) {
SnakeMonster SnakeMonsterJ = new SnakeMonster("蛇妖甲", 5, 20);
SnakeMonsterJ.otk();
SnakeMonsterJ.BloodRestoreMyBody();
SnakeMonsterJ.move();
System.out.println("************************************");
CentipedeMonster CentipedeMonsterY = new CentipedeMonster("蜈蚣乙", 60, 15);
CentipedeMonsterY.otk();
CentipedeMonsterY.move();
}
}
运行结果
三、多态练习
题目
解题思路
多态的实现条件是:继承->重写->父类声明 = new 子类。我们讲解时,会按照这个顺序来解析题目。
1、继承:
首先提取培根比萨和海鲜比萨共同的属性和方法。
属性:名称name、价格price、大小/size
方法:展示属性show()【需要被重写】
培根比萨PorkPizza特有属性:培根克数peiliao(犯懒了)
海鲜比萨FishPizza特有属性:配料信息peiliao
我们按照上面分析的属性和方法构建父类Pizza和他的两个子类PorkPizza、FishPizza
2、重写:
我们需要重写show()方法
培根比萨需要输出培根克数;海鲜比萨需要输出配料
3、父类声明 = new 子类:
我们这道题使用父类做返回值类型的多态实现方式。
我们在PizzaFactory类使用if语句判断客户要求的是哪种比萨
之后用主类Pizza作为返回值实现多态
实现代码
父类Pizza
public abstract class Pizza {
private String name;
private int price;
private int size;
private String peiliao;
public Pizza(String name, int price, int size, String peiliao) {
this.name = name;
this.price = price;
this.size = size;
this.peiliao = peiliao;
}
public Pizza() {
}
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 String getPeiliao() {
return peiliao;
}
public void setPeiliao(String peiliao) {
this.peiliao = peiliao;
}
public abstract void show();
}
培根比萨PorkPizza
public class PorkPizza extends Pizza{
private String peiliao;
public PorkPizza(String name, int price, int size, String peiliao, String peiliao1) {
super(name, price, size, peiliao);
this.peiliao = peiliao1;
}
public String getPeiliao() {
return peiliao;
}
public void setPeiliao(String peiliao) {
this.peiliao = peiliao;
}
public PorkPizza() {
}
@Override
public void show() {
System.out.println("名称:" + getName());
System.out.println("价格:" + getPrice() + "元");
System.out.println("大小:" + getSize() + "寸");
System.out.println("培根克数:" + getPeiliao());
}
}
海鲜比萨
public class FishPizza extends Pizza {
private String peiliao;
public FishPizza(String name, int price, int size, String peiliao, String peiliao1) {
super(name, price, size, peiliao);
this.peiliao = peiliao1;
}
public FishPizza() {
}
@Override
public void show() {
System.out.println("名称:" + getName());
System.out.println("价格:" + getPrice() + "元");
System.out.println("大小:" + getSize() + "寸");
System.out.println("配料:" + getPeiliao());
}
}
比萨工厂
import java.util.Scanner;
public class PizzaFactory {
public PizzaFactory() {
}
public Pizza sell(){
Pizza pizza = null;
Scanner scanner = new Scanner(System.in);
System.out.println("请选择想要制作的比萨(1.培根比萨2.海鲜比萨):");
int type = scanner.nextInt();
if (type == 1){
pizza = new PorkPizza();
System.out.println("请输入培根克数:");
String peiliao = scanner.next();
System.out.print("请输入比萨大小:");
int size = scanner.nextInt();
System.out.print("请输入比萨价格:");
int prize = scanner.nextInt();
pizza.setPeiliao(peiliao);
pizza.setPrice(prize);
pizza.setSize(size);
pizza.setName("培根披萨");
return pizza;
}
if (type == 2){
pizza = new FishPizza();
System.out.println("请输入配料信息:");
String peiliao = scanner.next();
System.out.print("请输入比萨大小:");
int size = scanner.nextInt();
System.out.print("请输入比萨价格:");
int prize = scanner.nextInt();
pizza.setPeiliao(peiliao);
pizza.setPrice(prize);
pizza.setSize(size);
pizza.setName("海鲜披萨");
return pizza;
}
return null;
}
}
测试PizzaTest
public class PizzaTest {
public static void main(String[] args) {
PizzaFactory factory = new PizzaFactory();
Pizza pizza= factory.sell();
pizza.show();
}
}
运行结果
总结
本文分别提供了封装、继承、多态每种类型一道题,请各位客官认真观看,认真思考代码中的逻辑,特别是父类做返回值类型的多态比萨题,我在理解时,也曾感到难以理解,但终于在我洗澡想、跑步想、走路想过后终于能够理解他的逻辑,但还是没有理解父类做返回值能实现代码的扩展性的理由,有知道简单易懂的理解方法的路过神仙请为在下指指路,感谢各位用心观看,在下告辞。