林浩然与杨凌芸的编程奇缘:封装、重载与递归的一天

2024-03-11 20:30

本文主要是介绍林浩然与杨凌芸的编程奇缘:封装、重载与递归的一天,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述

林浩然与杨凌芸的编程奇缘:封装、重载与递归的一天

Lin Haoran and Yang Lingyun’s Programming Adventure: A Day of Encapsulation, Overloading, and Recursion


在一个阳光明媚的日子里,程序员林浩然正在为他的Java王国添砖加瓦。他是一个热衷于类封装和方法调用的大师,而杨凌芸则是他隔壁工位上的算法女神,尤其擅长解决递归难题。

On a sunny day, the programmer Lin Haoran was busy contributing to his Java kingdom. He was a master enthusiast of class encapsulation and method invocation. Yang Lingyun, his neighbor at the adjacent workstation, was an algorithm goddess, especially skilled at solving recursive puzzles.

一日,林浩然正对着他的“Demo”王国沉思,这个王国由一个个封装得严丝合缝的Java类组成,每个类里的成员变量都像珍宝一样被private保护起来,只有通过精心设计的getter和setter方法才能触及。他一边想象着自己是王国的守门人,对任何试图窥探私密数据的外部世界大喊:“想访问我的秘密?先过我这关!”然后满意地笑了。

One day, Lin Haoran was contemplating his “Demo” kingdom, a realm composed of meticulously encapsulated Java classes. Each class had its member variables protected like treasures, shielded by the fortress of private access, accessible only through carefully designed getters and setters. He imagined himself as the guardian of the kingdom, shouting to the external world attempting to pry into the private data, “Want to access my secrets? Pass through me first!” and then smiled satisfactorily.

与此同时,杨凌芸正在研究一个名为“跳阶挑战”的问题,她打算使用递归来解决青蛙在无限台阶上跳跃的场景。她把青蛙比作勇闯迷宫的小勇士,每次能跳1级或2级台阶,看似简单的游戏背后却隐藏着递归的魔法。正当她深陷逻辑循环时,突然冒出一句:“如果青蛙会编程,它一定能理解递归就是‘自己调用自己的艺术’。”

Simultaneously, Yang Lingyun was tackling a problem called the “Jumping Stairs Challenge.” She planned to use recursion to solve the scenario of a frog leaping on infinite stairs. She likened the frog to a brave warrior navigating a maze, able to jump 1 or 2 steps at a time. Despite the apparent simplicity of the game, it concealed the magic of recursion. Lost in the loop of logic, she suddenly exclaimed, “If the frog could code, it would surely understand that recursion is the ‘art of calling oneself.’”

林浩然听闻此言,心中一动,决定用他的封装技巧帮助杨凌芸优化代码结构。于是,他提议创建一个名为“FrogJumper”的封装类,里面包含各种跳台阶的方法,其中就包括一个重载版本的jumpSteps(int currentStep, int target)方法,用来处理不同起始步数到达目标的各种情况。

Upon hearing this, Lin Haoran felt a spark within and decided to use his encapsulation skills to assist Yang Lingyun in optimizing the code structure. He proposed creating an encapsulation class named “FrogJumper,” containing various methods for jumping stairs, including an overloaded version of the jumpSteps(int currentStep, int target) method to handle different starting steps reaching the goal.

两人合作无间,林浩然负责封装实现,确保类的内部状态安全可靠;杨凌芸则利用她的递归思维,巧妙地实现了jumpRecursive(int stepsLeft)方法,让青蛙以优雅的姿态在虚拟台阶上翩翩起舞。

The collaboration between the two was seamless. Lin Haoran took charge of encapsulation implementation, ensuring the internal state of the class was secure and reliable. Meanwhile, Yang Lingyun, with her recursive mindset, cleverly implemented the jumpRecursive(int stepsLeft) method, allowing the frog to dance gracefully on virtual stairs.

最终,他们的程序在编译器的见证下成功运行,青蛙按照递归算法欢快地跃过了无数台阶。看着屏幕上跳出的成功信息,林浩然不禁赞叹:“封装之美在于其严谨,而递归之妙在于其无穷。”杨凌芸回应道:“就像我们的友谊,既有界限分明的尊重,又有层层深入的理解。”

In the end, their program successfully ran under the watchful eye of the compiler. The frog joyfully leaped over countless stairs following the recursive algorithm. Looking at the success message on the screen, Lin Haoran couldn’t help but admire, “The beauty of encapsulation lies in its rigor, and the charm of recursion lies in its infinity.” Yang Lingyun responded, “Just like our friendship, with clear boundaries of respect and layers of deep understanding.”

从此,在编程的世界里,林浩然与杨凌芸的故事成为了他们同事之间传颂的佳话,而他们的合作也让Java代码拥有了生命与智慧,封装、重载与递归在此刻交汇成了一首美妙的程序交响曲。

From then on, in the world of programming, the story of Lin Haoran and Yang Lingyun became a legendary tale passed among their colleagues. Their collaboration infused Java code with life and wisdom, and at that moment, encapsulation, overloading, and recursion converged into a beautiful symphony of code.


示例代码:封装、重载与递归的运用

// 林浩然创建的封装类 - FrogJumper
public class FrogJumper {// 私有成员变量,表示当前所在台阶数private int currentStep;// 构造方法,初始化青蛙起始位置public FrogJumper(int startStep) {this.currentStep = startStep;}// 封装的getter方法public int getCurrentStep() {return this.currentStep;}// 重载的jumpSteps方法public boolean jumpSteps(int target) {return jumpSteps(currentStep, target);}// 重载的jumpSteps方法(处理不同起始步数到达目标)private boolean jumpSteps(int currentStep, int target) {// 基本情况:已经到达目标台阶,返回trueif (currentStep == target) {return true;}// 递归情况:尝试跳1级和2级台阶return jumpSteps(currentStep + 1, target) || jumpSteps(currentStep + 2, target);}// 杨凌芸实现的递归方法 - jumpRecursivepublic boolean jumpRecursive(int stepsLeft) {// 基本情况:剩余0步或1步时,直接返回trueif (stepsLeft <= 1) {return true;}// 递归情况:尝试跳1级台阶后的情况if (jumpRecursive(stepsLeft - 1)) {return true;}// 递归情况:尝试跳2级台阶后的情况return jumpRecursive(stepsLeft - 2);}
}// 使用示例
public class Main {public static void main(String[] args) {FrogJumper frog = new FrogJumper(0); // 初始化青蛙在第0级台阶// 测试用递归方法解决跳跃问题System.out.println("Can the frog reach the 5th step recursively? " + frog.jumpRecursive(5));// 测试用重载方法解决跳跃问题System.out.println("Can the frog reach the 5th step using overloading? " + frog.jumpSteps(5));}
}

这段Java代码中,“FrogJumper”类展示了封装的概念,将青蛙的当前所在台阶数封装为私有变量,并通过getter方法提供外部访问接口。同时,类中实现了重载的方法jumpSteps()来处理青蛙从不同起始台阶数到目标台阶数的跳跃问题。

杨凌芸设计的jumpRecursive()方法则是递归思想的具体应用,通过定义基本情况(剩余0步或1步时可达到目标)以及递归情况(尝试不同的跳跃组合),解决了青蛙如何在无限台阶上跳跃的问题。

林浩然与杨凌芸的合作,使得封装、重载和递归这些编程概念在这段代码中得到了生动体现,也成为了他们共同创造的一首美妙的程序交响曲。


Example Code: Application of Encapsulation, Overloading, and Recursion

// Encapsulation class created by Lin Haoran - FrogJumper
public class FrogJumper {// Private member variable representing the current stepprivate int currentStep;// Constructor, initializes the frog's starting positionpublic FrogJumper(int startStep) {this.currentStep = startStep;}// Encapsulated getter methodpublic int getCurrentStep() {return this.currentStep;}// Overloaded jumpSteps methodpublic boolean jumpSteps(int target) {return jumpSteps(currentStep, target);}// Overloaded jumpSteps method (handles different starting steps to reach the target)private boolean jumpSteps(int currentStep, int target) {// Base case: already reached the target step, return trueif (currentStep == target) {return true;}// Recursive case: try jumping 1 step and 2 stepsreturn jumpSteps(currentStep + 1, target) || jumpSteps(currentStep + 2, target);}// Recursion method implemented by Yang Lingyun - jumpRecursivepublic boolean jumpRecursive(int stepsLeft) {// Base case: when there are 0 or 1 steps left, directly return trueif (stepsLeft <= 1) {return true;}// Recursive case: try the situation after jumping 1 stepif (jumpRecursive(stepsLeft - 1)) {return true;}// Recursive case: try the situation after jumping 2 stepsreturn jumpRecursive(stepsLeft - 2);}
}// Example usage
public class Main {public static void main(String[] args) {FrogJumper frog = new FrogJumper(0); // Initialize the frog at step 0// Test solving the jumping problem recursivelySystem.out.println("Can the frog reach the 5th step recursively? " + frog.jumpRecursive(5));// Test solving the jumping problem using overloadingSystem.out.println("Can the frog reach the 5th step using overloading? " + frog.jumpSteps(5));}
}

In this Java code, the class “FrogJumper” demonstrates the concept of encapsulation by encapsulating the frog’s current step as a private variable and providing external access through a getter method. The class also implements an overloaded method jumpSteps() to address the frog’s jumping problem from different starting step counts to reach the target step.

The jumpRecursive() method designed by Yang Lingyun is a specific application of recursion, solving the problem of how the frog jumps on an infinite number of steps by defining base cases (when 0 or 1 steps are left, the target can be reached) and recursive cases (trying different combinations of jumps).

The collaboration between Lin Haoran and Yang Lingyun vividly demonstrates the concepts of encapsulation, overloading, and recursion in this code, creating a beautiful symphony of programming.

这篇关于林浩然与杨凌芸的编程奇缘:封装、重载与递归的一天的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/798974

相关文章

C#反射编程之GetConstructor()方法解读

《C#反射编程之GetConstructor()方法解读》C#中Type类的GetConstructor()方法用于获取指定类型的构造函数,该方法有多个重载版本,可以根据不同的参数获取不同特性的构造函... 目录C# GetConstructor()方法有4个重载以GetConstructor(Type[]

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

【编程底层思考】垃圾收集机制,GC算法,垃圾收集器类型概述

Java的垃圾收集(Garbage Collection,GC)机制是Java语言的一大特色,它负责自动管理内存的回收,释放不再使用的对象所占用的内存。以下是对Java垃圾收集机制的详细介绍: 一、垃圾收集机制概述: 对象存活判断:垃圾收集器定期检查堆内存中的对象,判断哪些对象是“垃圾”,即不再被任何引用链直接或间接引用的对象。内存回收:将判断为垃圾的对象占用的内存进行回收,以便重新使用。

C++操作符重载实例(独立函数)

C++操作符重载实例,我们把坐标值CVector的加法进行重载,计算c3=c1+c2时,也就是计算x3=x1+x2,y3=y1+y2,今天我们以独立函数的方式重载操作符+(加号),以下是C++代码: c1802.cpp源代码: D:\YcjWork\CppTour>vim c1802.cpp #include <iostream>using namespace std;/*** 以独立函数

Go Playground 在线编程环境

For all examples in this and the next chapter, we will use Go Playground. Go Playground represents a web service that can run programs written in Go. It can be opened in a web browser using the follow

深入理解RxJava:响应式编程的现代方式

在当今的软件开发世界中,异步编程和事件驱动的架构变得越来越重要。RxJava,作为响应式编程(Reactive Programming)的一个流行库,为Java和Android开发者提供了一种强大的方式来处理异步任务和事件流。本文将深入探讨RxJava的核心概念、优势以及如何在实际项目中应用它。 文章目录 💯 什么是RxJava?💯 响应式编程的优势💯 RxJava的核心概念

函数式编程思想

我们经常会用到各种各样的编程思想,例如面向过程、面向对象。不过笔者在该博客简单介绍一下函数式编程思想. 如果对函数式编程思想进行概括,就是f(x) = na(x) , y=uf(x)…至于其他的编程思想,可能是y=a(x)+b(x)+c(x)…,也有可能是y=f(x)=f(x)/a + f(x)/b+f(x)/c… 面向过程的指令式编程 面向过程,简单理解就是y=a(x)+b(x)+c(x)

MiniGPT-3D, 首个高效的3D点云大语言模型,仅需一张RTX3090显卡,训练一天时间,已开源

项目主页:https://tangyuan96.github.io/minigpt_3d_project_page/ 代码:https://github.com/TangYuan96/MiniGPT-3D 论文:https://arxiv.org/pdf/2405.01413 MiniGPT-3D在多个任务上取得了SoTA,被ACM MM2024接收,只拥有47.8M的可训练参数,在一张RTX

JavaSE——封装、继承和多态

1. 封装 1.1 概念      面向对象程序三大特性:封装、继承、多态 。而类和对象阶段,主要研究的就是封装特性。何为封装呢?简单来说就是套壳屏蔽细节 。     比如:对于电脑这样一个复杂的设备,提供给用户的就只是:开关机、通过键盘输入,显示器, USB 插孔等,让用户来和计算机进行交互,完成日常事务。但实际上:电脑真正工作的却是CPU 、显卡、内存等一些硬件元件。