代码模块化-方法(method)

本文详细介绍了Java中的方法,包括其目的——模块化、复用性和简化应用,以及方法的语法结构。讲解了访问控制符`public`和静态修饰符`static`的使用,以及方法的返回类型、名称和参数列表。同时阐述了方法的形参和实参的概念,并通过示例说明方法重载的规则和应用场景。此外,还探讨了不定参数的使用,并提供了一个判断闰年的方法作为示例。最后,深入讨论了递归算法的概念及其执行流程,并给出了一个递归实现的斐波那契数列的例子。
摘要由CSDN通过智能技术生成

代码模块化-方法method


**1、method的概念**

Java方法的目的:

  • 模块化
  • 复用性
  • 应用简单

2、语法

public static int add (int a,int b){
   
  int c = a+b;
  return c;
}
  • public: 访问控制符
  • static: 静态
  • int: 方法的返回类型
  • add: 方法名称
  • int a,int b: 参数列表
  • 大括号{ } : 作用域

3、方法的形参和实参
在这里插入图片描述

(int a, int b ); // a, b 为方法的形参–形式上的参数可以任意名称
int x = add(4, 6); // 4,6为方法的实参, a=4, b =6

4、方法重载(overload)
方法的重载是指一个类中可以定义有相同的名字,但参数不同的多个方法。调用时,会根据不同的参数表选择对应的方法

两同三不同

——同一个类,同一个方法名

——不同:参数列表不同(类型,个数,顺序不同)

——只有返回值不同不构成方法重载

——只有形参的名称不同,不构成方法重载

——与普通方法一样,构造函数也可以重载

4.1
类中复用方法的名称,避免大量的方法名称命名
例如:

add(1,2) //两个数相加
add(1,3,3) //三个数相加

4.2
在同一个类中
方法名称完全相同
方法的参数类型或者数量不同
例如:

public int add(int a , int b){ }
public int add(int a, int b, int c ){ }

4.3 重载实例

/*
 * 重载(解决名字一样问题)
 * add_int
 * add_int_int
 * add_double_double
 * add_double_int
 * add_int_double
 */
	public static int add(int a) {
   // 重载只和参数类型、数量、顺序有关
		
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Spring Cloud模块化开发的示例: 假设我们有一个需求,需要开发一个电商平台,包括商品服务、用户服务和订单服务三个模块,其中商品服务和用户服务需要调用订单服务的接口。 1. 创建父工程 首先,我们需要创建一个父工程,用于管理所有子模块的依赖。 在父工程的pom.xml文件中,添加如下内容: ```xml <groupId>com.example</groupId> <artifactId>ecommerce</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>product-service</module> <module>user-service</module> <module>order-service</module> </modules> <properties> <spring-cloud.version>2021.0.0</spring-cloud.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` 在该文件中,我们定义了三个子模块,分别是product-service、user-service和order-service。同时,我们也定义了Spring Cloud的版本号,并将其管理在dependencyManagement中,以便所有子模块都可以继承该版本号。 2. 创建子模块 接下来,我们需要创建三个子模块,分别是product-service、user-service和order-service。 在每个子模块的pom.xml文件中,添加如下内容: ```xml <parent> <groupId>com.example</groupId> <artifactId>ecommerce</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <groupId>com.example.ecommerce</groupId> <artifactId>product-service</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 在该文件中,我们定义了当前子模块的父工程、groupId、artifactId、版本号和打包方式。同时,我们也定义了该子模块的依赖,包括Spring Cloud Eureka Client和Spring Boot Web。 3. 配置Eureka Server 在该示例中,我们使用Eureka作为服务注册中心。我们需要在一个单独的项目中配置Eureka Server,其他服务将会注册到该服务上。 在Eureka Server的pom.xml文件中,添加如下内容: ```xml <parent> <groupId>com.example</groupId> <artifactId>ecommerce</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <groupId>com.example.ecommerce</groupId> <artifactId>eureka-server</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> ``` 在该文件中,我们定义了当前子模块的父工程、groupId、artifactId、版本号和打包方式。同时,我们也定义了该子模块的依赖,包括Spring Cloud Eureka Server。 接下来,在Eureka Server的启动类上添加@EnableEurekaServer注解,开启Eureka Server的功能。 ```java @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } } ``` 4. 配置服务 在每个服务的配置文件中,我们需要配置该服务的名称和注册中心的地址。 ```yml spring: application: name: product-service eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ ``` 在该配置文件中,我们定义了当前服务的名称为product-service,并指定了注册中心的地址为http://localhost:8761/eureka/。 5. 实现服务 在每个服务中,我们需要实现该服务的业务逻辑,并暴露对外的接口。 以product-service为例,我们需要实现商品服务的业务逻辑,并暴露对外的接口。我们可以在该服务的启动类上添加@EnableDiscoveryClient注解,将该服务注册到Eureka Server上。 ```java @SpringBootApplication @EnableDiscoveryClient public class ProductServiceApplication { public static void main(String[] args) { SpringApplication.run(ProductServiceApplication.class, args); } } ``` 同时,我们也需要实现商品服务的Controller,并在该Controller中调用其他服务的接口。 ```java @RestController public class ProductController { @Autowired private RestTemplate restTemplate; @GetMapping("/users/{userId}/orders") public List<Order> getUserOrders(@PathVariable Long userId) { List<Order> orders = restTemplate.exchange("http://order-service/orders/users/{userId}", HttpMethod.GET, null, new ParameterizedTypeReference<List<Order>>(){}, userId).getBody(); return orders; } } ``` 在该Controller中,我们使用RestTemplate调用订单服务的接口,并获取该用户的所有订单。 6. 启动服务 最后,我们需要按照如下顺序启动服务: 1. 启动Eureka Server 2. 启动订单服务 3. 启动用户服务 4. 启动商品服务 启动后,我们可以在Eureka Server的管理界面中查看所有注册到该服务上的服务。同时,我们也可以通过商品服务的接口,获取当前用户的所有订单信息。 以上就是一个简单的Spring Cloud模块化开发的示例。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值