【0基础学java】教学日志:javaSE-面向对象6-面向对象前4章上机作业点评,主要涉及继承、封装、多态三个章节的内容
一、面向对象第一章上机作业参考答案(略)
可参考课堂Demo进行编写
链接地址:https://www.cnblogs.com/yppjava/p/15690472.html
二、面向对象第二章上机作业参考答案:
1、上机练习1——设计Dog和Penguin类
- 需求说明:
– 运用面向对象思想抽象出Dog类和Penguin类,画出对应类图
编辑
切换为居中
添加图片注释,不超过 140 字(可选)
– 根据类图编写Dog类和Penguin类
– 添加默认构造方法
2、上机练习2——打印Dog信息2-1
- 需求说明:
– 根据控制台提示信息选择领养宠物(狗),
▪ 输入昵称、品种、健康值
▪ 打印宠物信息
– 要保证健康值的有效性(在1到100之间)
3、上机练习3——Dog类的带参构造方法 - 需求说明:
– 增加带参构造方法
Dog(String name, String strain)
– 修改Test类,使用带参构造方法创建对象
4、上机练习4——操作企鹅性别属性 - 需求说明:
– 给Penguin类提供SEX_MALE和SEX_FEMALE两个静态常量,分别取值“Q仔”或“Q妹”
– 修改Test类,使用静态常量对性别进行赋值
– 修改企鹅的性别只能取值“雄”或“雌”,通过修改静态变量实现该需求
参考:
Dog.java
package netclass05.homework2;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/14 - 01 - 14 - 15:42
-
@Description: netclass05.homework2
-
@version: 1.0
*/
public class Dog {//昵称
private String name;//健康值
private int health;//亲密度
private int love;//品种
private String strain;public Dog(){
}
public Dog(String name, int health, String strain) {
this.name = name;
this.health = health;
this.strain = strain;
}public Dog(String name, int health, int love, String strain) {
this.name = name;
this.health = health;
this.love = love;
this.strain = strain;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public int getHealth() {
return health;
}public void setHealth(int health) {
if(health >= 1 &&health <= 100){
this.health = health;
}else{
System.out.println("你输入的健康值有误,请重新输入。");
}}
public int getLove() {
return love;
}public void setLove(int love) {
this.love = love;
}public String getStrain() {
return strain;
}public void setStrain(String strain) {
this.strain = strain;
}// 输出信息
public void print(){
System.out.println("狗的昵称是:" +this.name + ",健康值:" +this.health + ",亲密度:" + this.love + ",品种是:" + this.strain);
}
}
Penguin.java
package netclass05.homework2;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/14 - 01 - 14 - 15:47
-
@Description: netclass05.homework2
-
@version: 1.0
*/
public class Penguin {//昵称
private String name;//健康值
private int health;//亲密度
private int love;//性别
private static String gender;//添加静态常量
static final String SEX_MALE = "Q仔";static final String SEX_FEMALE = "Q妹";
public Penguin() {
}public Penguin(String name, int health, int love, String gender) {
this.name = name;
this.health = health;
this.love = love;
this.gender = gender;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public int getHealth() {
return health;
}public void setHealth(int health) {
if(health >= 1 &&health <= 100){
this.health = health;
}else{
System.out.println("你输入的健康值有误,请重新输入。");
}
}public int getLove() {
return love;
}public void setLove(int love) {
this.love = love;
}public String getGender() {
return gender;
}public void setGender(String gender) {
if("雄".equals(gender)){
this.gender = "雄";
}else if("雌".equals(gender)){
this.gender = "雌";
}else{
System.out.println("您输入的性别有误,请重新输入!");
}
// this.gender = gender;
}//输出方法
public void print(){
System.out.println("企鹅的昵称是:" +this.name + ",健康值:" +this.health + ",亲密度:" + this.love + ",性别是:" + this.gender);}
public static void main(String[] args) {
// Penguin p = new Penguin("小黑",10,20,"雄性");
// Penguin p = new Penguin();
// p.setName("小白");
// p.setHealth(10);
// p.setLove(20);
// p.setGender("雄性");
// System.out.println(p.getName());
// p.print();
// 创建
}
}
TestDemo.java
package netclass05.homework2;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/14 - 01 - 14 - 16:32
-
@Description: netclass05.homework2
-
@version: 1.0
*/
public class TestDemo {public static void main(String[] args) {
// //创建Scanner对象,接收键盘输入
// Scanner scanner = new Scanner(System.in);
// //读取昵称
// System.out.print("请输入狗的昵称:");
// String name = scanner.next();
// //读取品种
// System.out.print("请输入狗的品种:");
// String strain = scanner.next();
// //读取健康值
// System.out.print("请输入狗的健康值:");
// int health = scanner.nextInt();
//创建狗对象
// Dog dog = new Dog();
// dog.setName(name);
// dog.setStrain(strain);
// dog.setHealth(health);
// Dog dog = new Dog(name,health,strain);
// dog.print();Penguin p = new Penguin(); p.setName("小白"); p.setHealth(30);
// p.setGender(Penguin.SEX_MALE);
p.setGender("雄性");
p.print();
}
}
三、面向对象第三章上机作业参考答案:
1、上机练习1已在第三章博客中编写,请参考;
链接地址:
【0基础学java】教学日志:javaSE-面向对象3-this关键字、static关键字、代码块、package、import、封装、访问修饰符
2、上机练习2
参考:
父类Animal.java
package netclass05.homework3;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/14 - 01 - 14 - 15:21
-
@Description: netclass05.homework3
-
@version: 1.0
*/
public abstract class Animal {//年龄
private int age;public Animal(){
}
public Animal(int age){
this.age = age;
}public int getAge() {
return age;
}public void setAge(int age) {
this.age = age;
}//自我介绍的抽象方法
public abstract void introduce();
}
子类Fish.java
package netclass05.homework3;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/14 - 01 - 14 - 15:24
-
@Description: netclass05.homework3
-
@version: 1.0
*/
public class Fish extends Animal {private int weight;
public Fish(){
}
public Fish(int age,int weight){
super(age);
this.weight = weight;
}public int getWeight() {
return weight;
}public void setWeight(int weight) {
this.weight = weight;
}@Override
public void introduce() {
System.out.println("鱼的年龄是:" + super.getAge() + ",重量是:" + weight + "kg");
}//子类特有的方法 游泳
public void swim(){
System.out.println("我是一条鱼,正在游泳呢... ...");
}
}
子类Bird.java
package netclass05.homework3;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/14 - 01 - 14 - 15:27
-
@Description: netclass05.homework3
-
@version: 1.0
*/
public class Bird extends Animal{
private String color;public Bird(){
}
public Bird(int age,String color){
super(age);
this.color = color;
}public void setColor(String color){
this.color = color;
}public String getColor(){
return color;
}@Override
public void introduce() {
System.out.println("鸟的年龄:" + super.getAge() +",颜色是:" +color);
}//子类特有的方法 飞
public void fly(){
System.out.println("我是一只鸟,我正在飞... ...");
}
}
测试类AnimalTest.java
package netclass05.homework3;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/14 - 01 - 14 - 15:30
-
@Description: netclass05.homework3
-
@version: 1.0
*/
public class AnimalTest {public static void main(String[] args) {
Fish fish = new Fish(17,2);
fish.introduce();
fish.swim();
Bird bird = new Bird(16,"彩色");
bird.introduce();
bird.fly();
}
}
四、面向对象第四章作业参考答案:
1、上机练习—使用多态实现主人领养宠物并与宠物玩耍
需求说明:
– 主人根据宠物编号领养宠物
– 主人和狗狗玩接飞盘游戏,狗狗健康值减少10,不主人亲密度增加5
– 主人和企鹅玩游泳游戏,企鹅健康值减少10,不主人亲密度增加5
2、上机练习——计算一次租赁多辆汽车的总租金
3、上机练习——购置新车
参考:
机动车类 MotoVehical.java
package netclass04.abstracts.homework;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/12 - 01 - 12 - 16:23
-
@Description: netclass04.abstracts.homework
-
@version: 1.0
*/
public abstract class MotoVehical {private String no;
private String brand;public MotoVehical(){
}
public MotoVehical(String no, String brand) {
this.no = no;
this.brand = brand;
}public String getNo() {
return no;
}public void setNo(String no) {
this.no = no;
}public String getBrand() {
return brand;
}public void setBrand(String brand) {
this.brand = brand;
}//抽象方法(根据天数计算租金)
public abstract int calcRent(int days);
}
轿车类 Car.java
package netclass04.abstracts.homework;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/12 - 01 - 12 - 16:25
-
@Description: netclass04.abstracts.homework
-
@version: 1.0
*/
public class Car extends MotoVehical {private String type;
public Car(){
}
public Car(String no,String brand,String type){
super(no,brand);
this.type = type;
}public String getType() {
return type;
}public void setType(String type) {
this.type = type;
}@Override
public int calcRent(int days) {
if(this.type.equals("0")){
return 600 * days;
}else if(this.type.equals("1")){
return 500 * days;
}else if(this.type.equals("2")){
return 300 * days;
}else{
System.out.println("您输入的车型不存在!");
return 0;
}
}
}
客车类 Bus.java
package netclass04.abstracts.homework;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/12 - 01 - 12 - 16:28
-
@Description: netclass04.abstracts.homework
-
@version: 1.0
*/
public class Bus extends MotoVehical {private int seatCount;
public Bus(){
}
public Bus(String no,String brand,int seatCount){
super(no,brand);
this.seatCount = seatCount;
}public int getSeatCount() {
return seatCount;
}public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}@Override
public int calcRent(int days) {
if(this.seatCount > 16){
return 1500 * days;
}else{
return 800 * days;
}
}
}
卡车类 Truck.java
package netclass04.abstracts.homework;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/14 - 01 - 14 - 14:50
-
@Description: netclass04.abstracts.homework
-
@version: 1.0
*/
public class Truck extends MotoVehical {//吨位
private int weight;public Truck(String no,String brand,int weight){
super(no,brand);
this.weight = weight;
}@Override
public int calcRent(int days) {
return 50 * days * this.weight;
}
}
测试类 MotoVehicalTest.java
package netclass04.abstracts.homework;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/12 - 01 - 12 - 16:30
-
@Description: netclass04.abstracts.homework
-
@version: 1.0
*/
public class MotoVehicalTest {public static void main(String[] args) {
// MotoVehical moto = new MotoVehical();
// Car car = new Car("豫A 8888888","别克","0");
// int total1 = car.calcRent(5);
// System.out.println("租金为:" +total1);
// Bus bus = new Bus("豫A 6666666","金杯",16);
// int total2 = bus.calcRent(5);
// System.out.println("租金为:" +total2);
MotoVehical[] motoArray = new MotoVehical[5];
//String[] arrays = new String[4];
motoArray[0] = new Car("豫A 00099999","宝马550i","1");
motoArray[1] = new Car("豫B 00088888","宝马550i","1");
motoArray[2] = new Car("豫C 00066666","别克林荫大道","2");
motoArray[3] = new Bus("豫D 00011111","金龙",34);
motoArray[4] = new Truck("豫E 000333333","东方红",50);
// int totalRent = 0;
// for(int i = 0; i<motoArray.length;i++){
// totalRent += motoArray[i].calcRent(5);
// }
int totalRent = MotoVehicalTest.calcRent(motoArray,5);
System.out.println("租5天的总租金是:" + totalRent);
}public static int calcRent(MotoVehical[] motoArray,int days){
int totalRent = 0;
for(int i = 0; i<motoArray.length;i++){
totalRent += motoArray[i].calcRent(days);
}
return totalRent;
}
}
五、答疑,主要是关于Object对象、抽象类的使用、instanceof的使用、向上转型和向下转型的理解。
1、Object对象的使用
package netclass05.demo;
import java.util.Objects;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/14 - 01 - 14 - 17:17
-
@Description: netclass05
-
@version: 1.0
/
public class ObjectTest /extends Object*/ {private String name;
private int age;public ObjectTest(){
// super();
}public ObjectTest(String name,int age){
this.name = name;
this.age = age;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public int getAge() {
return age;
}public void setAge(int age) {
this.age = age;
}@Override
public String toString() {
return "ObjectTest{" +
"name='" + name + ''' +
", age=" + age +
'}';
}@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ObjectTest that = (ObjectTest) o;
return age == that.age &&
Objects.equals(name, that.name);
}@Override
public int hashCode() {
return Objects.hash(name, age);
}public static void main(String[] args) {
// 需求:想打印出对象的内容
ObjectTest ot = new ObjectTest("张飞",18);
ObjectTest ot2 = new ObjectTest("张飞",18);
System.out.println(ot);
// System.out.println(ot == ot2);
// System.out.println(ot.equals(ot2));
// System.out.println(ot.getClass().getName());
// System.out.print(ot);
// ot.setName("张飞");
// ot.setAge(18);
// System.out.println(ot.getName());
// System.out.println(ot.getAge());//instanceof的用法 : 判断一个对象是否是某个类的实例 if(ot instanceof ObjectTest){ System.out.println("yes"); }else{ System.out.println("no"); }
}
}
2、instanceof的使用
package netclass05.demo;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/14 - 01 - 14 - 17:41
-
@Description: netclass05.demo
-
@version: 1.0
*/
public class A {public void print(){
System.out.println("我是A... ...");
}
}
package netclass05.demo;
/**
-
@Auther: Yu Panpan
-
@Date: 2022/1/14 - 01 - 14 - 17:41
-
@Description: netclass05.demo
-
@version: 1.0
*/
public class B extends A {public void print(){
System.out.println("我是B... ...");
}public static void main(String[] args) {
A a = new A();
//向下转型
B b = (B)a;
b.print();
}
}
3、抽象类的使用、向上转型和向下转型的理解
父类 AbstractTest.java
package netclass05.demo;
/**
- @Auther: Yu Panpan
- @Date: 2022/1/14 - 01 - 14 - 17:31
- @Description: netclass05.demo
- @version: 1.0
*/
/**
-
定义一个人类
*/
public abstract class AbstractTest {private String name;
private int age;
//基本工资
private double basicSalary;public AbstractTest() {
}public AbstractTest(String name, int age) {
this.name = name;
this.age = age;
}public AbstractTest(String name, int age,double basicSalary) {
this.name = name;
this.age = age;
this.basicSalary = basicSalary;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public int getAge() {
return age;
}public void setAge(int age) {
this.age = age;
}public double getBasicSalary() {
return basicSalary;
}public void setBasicSalary(double basicSalary) {
this.basicSalary = basicSalary;
}//定义一个计算工资的方法
public abstract double getSalary();
}
子类 Worker.java
package netclass05.demo;
/**
- @Auther: Yu Panpan
- @Date: 2022/1/14 - 01 - 14 - 17:32
- @Description: netclass05.demo
- @version: 1.0
*/
/**
-
工人类
*/
public class Worker extends AbstractTest {//绩效工资
private double kpiSalary;public Worker(){
}
public Worker(String name,int age,double kpiSalary){
super(name,age);
this.kpiSalary = kpiSalary;
}public double getKpiSalary() {
return kpiSalary;
}public void setKpiSalary(double kpiSalary) {
this.kpiSalary = kpiSalary;
}@Override
public double getSalary() {
//基本工资+绩效工资
return super.getBasicSalary() + this.getKpiSalary();
}public static void main(String[] args) {
//向上转型:子类向父类转化,会自动转化
AbstractTest at = new Worker();//向下转型:父类向子类转化,需要强制转化,而且可能会出现问题
// AbstractTest at2 = new AbstractTest();//错误的
}
}