鱼C论坛

 找回密码
 立即注册
查看: 356|回复: 2

[已解决]java关于final的用法

[复制链接]
发表于 2023-11-25 09:09:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
修改给定的程序,使得程序能顺利运行,且结果如下:
speedlimit=120
running safely with 100kmph
running safely with 100kmph

根据提示,在下面编辑器中根据编译的错误提示修改源程序。
  1. /* 请在下面的 Begin 和 end之间修改源程序。
  2.   不可以修改 final 变量的值
  3.   final方法,不可以重写
  4.   不可以定义final 类
  5. */
  6. public class Main {
  7.     public static void main(String args[]) {
  8.         Bike1 obj = new Bike1();
  9.         obj.run();

  10.         Honda honda = new Honda();
  11.         honda.run();

  12.         Yamaha yamaha = new Yamaha();
  13.         yamaha.run();
  14.     }
  15. }

  16. /********** Begin *********/
  17. class Bike1 {
  18.     final int speedlimit = 90;

  19.     void run() {
  20.         speedlimit = 120;
  21.         System.out.println("speedlimit=120");
  22.     }
  23. }

  24. class Bike2 {
  25.     final void run() {
  26.         System.out.println("running");
  27.     }
  28. }

  29. class Honda extends Bike2 {
  30.     void run() {
  31.         System.out.println("running safely with 100kmph");
  32.     }
  33. }

  34. final class Bike3 {
  35. }

  36. class Yamaha extends Bike3 {
  37.     void run() {
  38.         System.out.println("running safely with 100kmph");
  39.     }
  40. }
  41. /********** end *********/
复制代码
最佳答案
2023-11-25 09:48:45
错误如下:

在Bike1类中,speedlimit被声明为final,这意味着它的值一旦被初始化就不能再改变。所以,我们不能在run方法中修改speedlimit的值。

在Honda类中,程序试图重写Bike2类的run方法,但是run方法在Bike2类中被声明为final,这意味着它不能被重写。

在Yamaha类中,程序试图继承Bike3类,但是Bike3类被声明为final,这意味着它不能被继承。

代码:

  1. public class Main {
  2.     public static void main(String args[]) {
  3.         Bike1 obj = new Bike1();
  4.         obj.run();

  5.         Honda honda = new Honda();
  6.         honda.run();

  7.         Yamaha yamaha = new Yamaha();
  8.         yamaha.run();
  9.     }
  10. }

  11. class Bike1 {
  12.     final int speedlimit = 90;

  13.     void run() {
  14.         System.out.println("speedlimit=" + speedlimit);
  15.     }
  16. }

  17. class Bike2 {
  18.     void run() {
  19.         System.out.println("running");
  20.     }
  21. }

  22. class Honda extends Bike2 {
  23.     @Override
  24.     void run() {
  25.         System.out.println("running safely with 100kmph");
  26.     }
  27. }

  28. class Bike3 {
  29.     void run() {
  30.         System.out.println("running");
  31.     }
  32. }

  33. class Yamaha extends Bike3 {
  34.     @Override
  35.     void run() {
  36.         System.out.println("running safely with 100kmph");
  37.     }
  38. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-25 09:09:52 | 显示全部楼层
/* 请在下面的 Begin 和 end之间修改源程序。
  不可以修改 final 变量的值
  final方法,不可以重写
  不可以定义final 类
*/
public class Main {
    public static void main(String[] args) {
        Bike1 obj = new Bike1();
        obj.run();

        Honda honda = new Honda();
        honda.run();

        Yamaha yamaha = new Yamaha();
        yamaha.run();
    }
}

/********** Begin *********/
class Bike1 {
    final int speedlimit = 90;

    void run() {
        // 不可以修改final变量的值
        System.out.println("speedlimit=" + speedlimit);
    }
}

class Bike2 {
    final void run() {
        System.out.println("running");
    }
}

class Honda extends Bike2 {
    // 不可以重写final方法
    // void run() {
    //     System.out.println("running safely with 100kmph");
    // }
}

final class Bike3 {
}

class Yamaha extends Bike3 {
    void run() {
        System.out.println("running safely with 100kmph");
    }
}
/********** end *********/
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-25 09:48:45 | 显示全部楼层    本楼为最佳答案   
错误如下:

在Bike1类中,speedlimit被声明为final,这意味着它的值一旦被初始化就不能再改变。所以,我们不能在run方法中修改speedlimit的值。

在Honda类中,程序试图重写Bike2类的run方法,但是run方法在Bike2类中被声明为final,这意味着它不能被重写。

在Yamaha类中,程序试图继承Bike3类,但是Bike3类被声明为final,这意味着它不能被继承。

代码:

  1. public class Main {
  2.     public static void main(String args[]) {
  3.         Bike1 obj = new Bike1();
  4.         obj.run();

  5.         Honda honda = new Honda();
  6.         honda.run();

  7.         Yamaha yamaha = new Yamaha();
  8.         yamaha.run();
  9.     }
  10. }

  11. class Bike1 {
  12.     final int speedlimit = 90;

  13.     void run() {
  14.         System.out.println("speedlimit=" + speedlimit);
  15.     }
  16. }

  17. class Bike2 {
  18.     void run() {
  19.         System.out.println("running");
  20.     }
  21. }

  22. class Honda extends Bike2 {
  23.     @Override
  24.     void run() {
  25.         System.out.println("running safely with 100kmph");
  26.     }
  27. }

  28. class Bike3 {
  29.     void run() {
  30.         System.out.println("running");
  31.     }
  32. }

  33. class Yamaha extends Bike3 {
  34.     @Override
  35.     void run() {
  36.         System.out.println("running safely with 100kmph");
  37.     }
  38. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 22:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表