Java实验 继承和多态2

Java实验 继承和多态2

1.方法重写(1)

题目概述

本关任务:重写 Cat 类中的 toString 方法,返回 Cat 类的基本信息。

参考代码

package 第一题;
/**
 * 任务:重写 Cat 类中的 toString 方法,返回 Cat 类的基本信息。
 */
class Animal{
    private String name; // 动物名称
    private int age; // 动物年龄

    // 返回动物类的基本信息
    public String toString() {
        return "Anaimal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Cat extends Animal{
    private String name; // 小猫的名称
    private int age; // 小猫年龄

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }
    // 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码
    /********* Begin *********/
    // 重写 Anaimal 中的 toString 方法,返回类型为 String,格式:我是一只名为xx的小猫,今年xx岁了
    @Override
    public String toString() {
        String s;
        s="我是一只名为"+this.name+"的小猫,今年"+this.age+"岁了";
        return s;
    }
    /********** End **********/
}

2.方法重写(2)

题目概述

本关任务:计算球的表面积。

参考代码

package 第二题;
/**
 * 重写 Shape 中的 area 方法,计算球的表面积。
 */
class Shape {
    private double r; //球的半径
    // 球的体积
    public double area(){
        double s = (double)3/4*Math.PI*Math.pow(r,3);
        return s;
    }
}
public class Sphere extends Shape{
    private double r; //球的半径
    public Sphere(double r) {
        this.r = r;
    }
    // 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码
    /********* Begin *********/
    // 重写 Shape 中的 area 方法,计算球的表面积,将计算结果返回
    @Override
    public double area() {
        return 4*Math.PI*this.r*this.r;
    }
    /********** End **********/
}

3.对象类型的转换(1)

题目概述

本关任务:使用对象类型的转换,根据编程提示,完成猫类和动物类的转换,以及彼此方法和属性的调用。

参考代码

package 第三题;
/**
 * 使用对象类型的转换,根据编程提示,完成猫类和动物类的转换,以及彼此方法和属性的调用
 */
// 定义动物类
class Animal{
    // 定义动物类的属性
    public String name = "动物";
    public static String staticName = "可爱的动物";
    // 定义动物类的行为方法
    public void eat() {
        System.out.println("动物吃饭");
    }
    public static void staticEat() {
        System.out.println("可爱的动物正在在吃饭");
    }
}
// 定义猫类,该类继承动物类
public class Cat extends Animal{
    // 定义猫类的属性
    public String name = "猫";
    public String str = "可爱的小猫";
    public static String staticName = "我是喵星人";
    // 定义猫类的行为方法
    public void eat() {
        System.out.println("猫吃饭");
    }
    public static void staticEat() {
        System.out.println("喵星人在吃饭");
    }
    public void eatMethod() {
        System.out.println("猫喜欢吃鱼");
    }
    public static void main(String[] args) {
        // 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码
        /********* Begin *********/
        // 向上转型,把猫类对象赋值给动物类
        Animal animal = new Cat();
        // 向下转型,将动物类引用转换为猫类对象
        Cat cat = (Cat) animal;
        // 输出Animal类的name变量
        System.out.println(animal.name);
        // 输出Animal类的staticName变量
        System.out.println(animal.staticName);
        // 输出Cat类的eat()方法
        animal.eat();
        // 输出Animal类的staticEat()方法
        animal.staticEat();
        // 调用Cat类的str变量
        System.out.println(cat.str);
        // 调用Cat类的eatMethod()方法
        cat.eatMethod();
        /********** End **********/
    }
}

4.对象类型的转换(2)

题目概述

本关任务:判断梨类、苹果类和水果类的关系,并通过对象类型转换调用彼此的属性和方法。

参考代码

package 第四题;
/**
 * 判断梨类、苹果类和水果类的关系,并通过对象类型转换调用彼此的属性和方法。
 */
class Fruits {
    public String name; // 定义水果名称
    Fruits (String name) {
        this.name = name;
    }
}
// 苹果类继承水果类
class Apple extends Fruits {
    public String acolor; // 苹果颜色
    public Apple(String name, String acolor) {
        super(name);
        this.acolor = acolor;
    }
}
// 梨类继承水果类
class Pear extends Fruits {
    public String pcolor; // 梨的颜色
    public Pear(String name, String pcolor) {
        super(name);
        this.pcolor = pcolor;
    }
}
public class Tests {
    public static void main(String args[]) {
        Fruits f = new Fruits("水果");
        Apple a = new Apple("苹果","red");
        Pear p = new Pear(";梨","yellow");
        // 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码
        /********* Begin *********/
        // 依次判断 f、a、p是否为 Fruits的子类对象
        boolean b1=f instanceof Fruits;
        boolean b2=a instanceof Fruits;
        boolean b3=p instanceof Fruits;
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);
        // 把梨类对象赋值给水果类,其中name值为bigPear,颜色为green
        f=p;
        p.name="bigPear";
        p.pcolor="green";
        // 输出当前水果类引用的名称
        System.out.println(p.name);
        // 依次判断当前水果类引用是否为水果类和梨类的子类
        System.out.println(p instanceof Fruits);
        System.out.println(p instanceof Pear);
        // 将当前水果类引用强转为梨类
        p=(Pear) f;
        // 输出当前梨类的颜色
        System.out.println(p.pcolor);
        /********** End **********/
    }
}

5.final关键字(1)

题目概述

本关任务:调试代码,对代码进行增添、删除和修改等操作,使得程序能够正常运行,输出结果请参照预期输出结果。

参考代码

package 第五题;
/**
 *  调试代码,对代码进行增添、删除和修改等操作,使得程序能够正常运行,输出结果请参照预期输出结果。
 */
// 请在下面的Begin-End之间编写正确的代码
/********* Begin *********/
public class Demo {
    public static void main(String args[]) {
        Bike1 obj = new Bike1();
        obj.run();
        Honda honda = new Honda();
        honda.run();
        Yamaha yamaha = new Yamaha();
        yamaha.run();
    }
}
class Bike1 {
    int speedlimit = 90; // 定义速度限制
    void run() {
        // 修改速度限制为 120,并输出
        speedlimit = 120;
        System.out.println("speedlimit=120");
    }
}
class Bike2 {
    // 输出 running
    void run() {
        System.out.println("running");
    }
}
// 继承 Bike2 类
class Honda extends Bike2 {
    // 重写 run 方法
    void run() {
        System.out.println("running safely with 100kmph");
    }

}
class Bike3 {
}
// 继承 Bike3 类
class Yamaha extends Bike3 {
    void run() {
        System.out.println("running safely with 10kmph");
    }
}
/********** End **********/

6.final关键字(2)

题目概述

本关任务:按照代码文件中提供的注释完成 Demo 类的编写,使得程序正常输出。

参考代码

package 第六题;
/**
 * 按照代码文件中提供的注释完成 Demo 类的编写,使得程序正常输出。
 */
class DemoTest{
    int i = 10;
}
// 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码
/********* Begin *********/
// 将Demo类改为 final形式
final class Demo{
    // 定义一个final、static常量,名为PI,值为3.14
    final static double PI=3.14;
    // 声明一个final类型的DemoTest对象,命名为demoTest
    final DemoTest demoTest=new DemoTest();
    // 声明一个不为final类型的DemoTest对象,命名为demoTest2
    DemoTest demoTest2=new DemoTest();
    // 声明一个final型的数组,类型为int,值为 1,2,3,命名为a
    final int[] a={1,2,3};
    // 删除主函数中错误的代码,使得程序能够正确输出
    public static void main(String[] args) {
        Demo demo = new Demo();
        //demo.demoTest = new DemoTest();
        System.out.println(demo.PI);
        demo.demoTest2 = new DemoTest();
        System.out.println(demo.demoTest2.i);
        for (int i = 0; i < demo.a.length; i++){
            demo.a[i] = 9;
            System.out.println(demo.a[i]);
        }
    }
}
/********** End **********/

7.抽象类(1)

题目概述

本关任务:根据所学知识,完成抽象类的定义与使用。

参考代码

package 第七题;
// 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码
/********** Begin **********/
// 定义员工抽象类 Employee,其中包含 2 个受保护的变量和两个抽象方法
abstract class Employee
{
// 两个受保护的变量:姓名 name(String),和工资 salary(double);
    protected String name;
    protected double salary;
    public Employee() {
    }
    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }
    //抽象方法 work,无返回值,表示工作内容
    abstract void work();
//抽象方法 info,无返回值,表示员工信息
    abstract void info();
}
// 定义一个公开的经理类 Manager,该类继承员工类,除了有员工类的基本属性外,还有岗位级别 gender(String)私有属性。
public class Manager extends Employee{
// 定义一个有参构造方法
    private String gender;
    public Manager(String gender) {
        this.gender = gender;
    }
    public Manager(String name, double salary, String gender) {
        super(name, salary);
        this.gender = gender;
    }
    // 重写 work() 方法,输出:“我负责对施工项目实施全过程、全面管理。”;
    @Override
    void work() {
        System.out.println("我负责对施工项目实施全过程、全面管理。");
    }
// 重写 info() 方法,输出:“姓名:xx,工资:xx,岗位级别:xx”。
    @Override
    void info() {
        System.out.println("姓名:"+this.name+",工资:"+this.salary+",岗位级别:"+this.gender);
    }
}
/********** End **********/

8.抽象类(2)

题目概述

本关任务:通过图形类的计算面积的方法,计算矩形和三角形的面积。

参考代码

package 第八题;
/**
 * 通过图形类的计算面积的方法,计算矩形和三角形的面积。
 */
// 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码
/********** Begin **********/
// 将 Shape 类改为抽象类
public abstract class Shape {
    public int width; // 几何图形的宽
    public int height; // 几何图形的高
    public Shape(int width, int height) {
        this.width = width;
        this.height = height;
    }
    // 定义一个抽象方法 area(),返回值类型为 double,计算图形的面积
    abstract double area();
}
// Rectangle 为矩形类,该类继承 Shape 类,并拥有 Shape 类的属性
class Rectangle extends Shape{
    // 定义一个有参构造器
    public Rectangle(int width, int height) {
        super(width, height);
    }
    // 重写抽象方法 area,计算矩形的面积(高*宽),并将计算结果返回
    @Override
    double area() {
        return this.height*this.width;
    }
}
// Triangle 为矩形类,该类继承 Shape 类,并拥有 Shape 类的属性
class Triangle extends Shape{
    // 定义一个有参构造器
    public Triangle(int width, int height) {
        super(width, height);
    }
    // 重写抽象方法 area,计算三角形的面积(高\*宽/2),并将计算结果返回
    @Override
    double area() {
        return this.height*this.width/2;
    }
}
/********** End **********/
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值