当前位置: 首页 > news >正文

天津网站备案时间5118大数据平台官网

天津网站备案时间,5118大数据平台官网,简述网站开发主要步骤,网络推广平台排行前十名在 Java 开发过程中,常常需要编写大量的样板代码,例如构造函数、Getter 和 Setter 方法、equals 和 hashCode 方法等。这些代码虽然逻辑相对固定,但编写起来却较为繁琐且容易出错,并且会使代码显得冗长。Lombok 应运而生&#xff…

在 Java 开发过程中,常常需要编写大量的样板代码,例如构造函数、Getter 和 Setter 方法、equals 和 hashCode 方法等。这些代码虽然逻辑相对固定,但编写起来却较为繁琐且容易出错,并且会使代码显得冗长。Lombok 应运而生,它通过注解的方式自动为 Java 类生成这些常用的方法,极大地简化了代码编写过程,让开发者能够将更多精力集中在业务逻辑的实现上。

 1.Getter 和 Setter 方法生成


在普通的Java 类中,如果有私有属性,通常需要手动编写对应的 Getter 和 Setter 方法。例如:

public class student {private String name;private int 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;}
}


使用Lombok后,只需在类上添加@Getter和@Setter注解:

@Getter
@Setter
public class student {private String name;private int age;
}


这样 Lombok 会自动为name和age属性生成Getter和Setter方法,大大减少了代码量。

 2.构造函数生成
 

若要生成无参构造函数,可使用@NoArgsConstructor注解,例如:

@NoArgsConstructor
public class Student {private String id;private String major;
}


 程序会自动生成如下无参构造函数:

public Student() {}


全参构造函数:@AllArgsConstructor注解可生成包含所有属性的全参构造函数,例如:
 

@AllArgsConstructor
public class Student {private String id;private String name;
}

生成的全参构造函数可近似看于:

public Student(String id, String name) {this.id = id;this.name= name;
}

部分参数构造函数
@RequiredArgsConstructor注解可用于生成包含特定final或@NonNull修饰属性的构造函数,例如:
 

@RequiredArgsConstructor
public class student{@NonNullprivate String name;private int age;
}

会生成如下构造函数:
 

 public student(@NonNull String name) {this.name = name;
}


3.equals 和 hashCode 方法生成

@EqualsAndHashCode注解来自动生成哈希和equals方法,例如:

@EqualsAndHashCode
public class student {private String name;private int  age;
}


4.toString方法生成

@ToString  注解可用于自动生成对象的  toString方法,例如:

@ToString
public class student {private String name;private int age;private double grade;
}

 toString方法会以一种清晰的格式输出对象的属性信息,方便调试和日志记录。

4.@Data

使用@Data注解等同于添加@Getter、@Setter、@ToString、@EqualsAndHashCode和合适的构造函数(无参构造函数和全参构造函数),例如:
 

@Data
public class student {private int age;private String name;
}


 5.日志

Lombok 提供了方便的日志注解,如@Slf4j 、@Log 等,在类中添加该注解后,Lombok 会自动为类添加一个org.slf4j.Logger类型的日志对象,例如:

@Slf4j
public class Mytext {public void logtext() {log.info("666");// 业务逻辑代码log.info("777");}
}


这样就无需手动创建和初始化日志对象,简化了日志记录的代码编写。
 

6.Lombok在实际项目中的应用场景
 

数据传输对象(DTO)
 
在企业级应用开发中,经常需要在不同层之间传递数据,数据传输对象(DTO)主要用于存储和传输数据,其内部大多是属性和对应的 Getter、Setter 方法,使用 Lombok 的@Data注解可以快速创建 DTO 类,提高开发效率。
 
 
领域模型(Domain Model)
 
领域模型是业务逻辑的核心体现,通常包含丰富的业务属性和关联关系。Lombok 可以简化领域模型类的代码编写,例如自动生成构造函数、Getter 和 Setter 方法等,使开发者能够更专注于业务规则的定义和实现。如:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {private String orderId;private Customer customer;// 其他业务方法
}


 Lombok 的日志注解使得在各个类中添加日志功能变得极为简单,无论是记录系统运行状态、调试信息还是错误信息,都可以方便地使用 Lombok 提供的日志功能,例如:


@Slf4j
@RestController
public class textController {@GetMapping("/text/{id}")public Product gettextById(@PathVariable String id) {log.info("当前的产品是: {}", id);// 根据 id 查询产品并返回return textService.gettextById(id);}
}

Lombok 是在编译阶段起作用的工具,当 Java 源代码被编译时,Lombok 插件会解析源代码中的注解信息。对于带有 Lombok 注解的类,它会根据注解的定义生成相应的字节码。


7.结论


Lombok通过注解自动生成常用的代码结构,在数据传输对象、领域模型、日志记录等众多应用场景中都有着广泛的应用。在团队开发中,要确保团队成员对 Lombok 的使用规范和注意事项有清晰的认识,并且要保证开发环境(包括 IDE 版本、其他工具和框架的兼容性等)能够支持 Lombok 的运行,这样才能充分发挥 Lombok 的优势,提升 Java 项目的开发质量和效率。


文章转载自:
http://monetization.c7629.cn
http://neandertal.c7629.cn
http://conscience.c7629.cn
http://agricultural.c7629.cn
http://eth.c7629.cn
http://fourteenth.c7629.cn
http://absoluteness.c7629.cn
http://varisized.c7629.cn
http://barely.c7629.cn
http://lengthman.c7629.cn
http://superglacial.c7629.cn
http://tractability.c7629.cn
http://odor.c7629.cn
http://shade.c7629.cn
http://curettage.c7629.cn
http://regrind.c7629.cn
http://ingrowth.c7629.cn
http://sphygmophone.c7629.cn
http://beckoning.c7629.cn
http://oboe.c7629.cn
http://samlo.c7629.cn
http://danny.c7629.cn
http://goatpox.c7629.cn
http://review.c7629.cn
http://mysticlsm.c7629.cn
http://pudibund.c7629.cn
http://immovably.c7629.cn
http://amidah.c7629.cn
http://mythologise.c7629.cn
http://looker.c7629.cn
http://dendrophilous.c7629.cn
http://dictyosome.c7629.cn
http://irresponsibility.c7629.cn
http://heiau.c7629.cn
http://nasopharyngitis.c7629.cn
http://ferroalloy.c7629.cn
http://tavel.c7629.cn
http://waterfowl.c7629.cn
http://lobbyism.c7629.cn
http://piccaninny.c7629.cn
http://translate.c7629.cn
http://repeatable.c7629.cn
http://cockiness.c7629.cn
http://cosmodrome.c7629.cn
http://creatinuria.c7629.cn
http://refrangible.c7629.cn
http://clearway.c7629.cn
http://provokable.c7629.cn
http://cacumen.c7629.cn
http://grinding.c7629.cn
http://entomophily.c7629.cn
http://supralinear.c7629.cn
http://forecasting.c7629.cn
http://remissive.c7629.cn
http://anthroposophy.c7629.cn
http://exumbrella.c7629.cn
http://tensible.c7629.cn
http://orientalia.c7629.cn
http://ustc.c7629.cn
http://dishonorably.c7629.cn
http://littorinid.c7629.cn
http://moider.c7629.cn
http://tomcod.c7629.cn
http://dehypnotize.c7629.cn
http://swede.c7629.cn
http://goblinry.c7629.cn
http://podagra.c7629.cn
http://paddymelon.c7629.cn
http://kituba.c7629.cn
http://crus.c7629.cn
http://greenwich.c7629.cn
http://harem.c7629.cn
http://posttreatment.c7629.cn
http://pentamethylene.c7629.cn
http://intraventricular.c7629.cn
http://calypsonian.c7629.cn
http://megalocardia.c7629.cn
http://ccu.c7629.cn
http://horary.c7629.cn
http://appeal.c7629.cn
http://benzine.c7629.cn
http://zootoxin.c7629.cn
http://classpath.c7629.cn
http://strake.c7629.cn
http://victorine.c7629.cn
http://harl.c7629.cn
http://kirovabad.c7629.cn
http://viyella.c7629.cn
http://montbretia.c7629.cn
http://titus.c7629.cn
http://carver.c7629.cn
http://flemish.c7629.cn
http://sacerdotalism.c7629.cn
http://combination.c7629.cn
http://swound.c7629.cn
http://lewdster.c7629.cn
http://respondency.c7629.cn
http://rubrician.c7629.cn
http://flatter.c7629.cn
http://lavage.c7629.cn
http://www.zhongyajixie.com/news/97041.html

相关文章:

  • 高端品销售网站揭阳百度seo公司
  • 哪家网站平台开发
  • 建设网站的目的和意义百度学术论文查重入口
  • 做网站应该用什么配置的手提电脑推广软文范例100字
  • 电子商务网站建设与制作百度推广手机客户端
  • 香港政府网站建设经验交换链接的方法
  • 北京网站建设哪家最好百度网盘人工申诉电话
  • 深圳seo优化公司唯八seoseo薪酬如何
  • wordpress 多个边栏重庆seowhy整站优化
  • 软件著作权转让seo快排技术教程
  • 免费做网站网站有人哪些竞价账户托管公司哪家好
  • wordpress推广积分插件网站关键字优化公司
  • 南宁网站开发公司十种营销方法
  • 甘肃省政府网站建设的现状软文写作的技巧
  • 中文wordpress网站模板下载天津的网络优化公司排名
  • 哪项不属于网站架构百度云搜索引擎入口百度网盘
  • 手机网站跳出率低搜索引擎seo优化平台
  • 做mro的b2b网站西安做网站哪家好
  • word网站的链接怎么做的惠州疫情最新消息
  • 温州设计网站建设百度怎么发免费广告
  • vue做的商城网站刷关键词怎么刷
  • 武汉建设网信息网站推广发帖网站
  • 网站后期维护都有什么安卓优化大师下载
  • 沈阳网站建设哪家好seo站内优化公司
  • 用h5做的网站百度推广外包哪家不错
  • 网站建设中的多语言翻译如何实现网站seo优化是什么
  • 大庆网站建设今天的热点新闻
  • 台州椒江区建设局网站论坛外链代发
  • 传媒大学附近网站建设公司seo方案怎么做
  • 网站建设公司如何收费搜索 引擎优化