Discuss / Java / 代码中的this是必要的吗

代码中的this是必要的吗

Topic source

Deler

#1 Created at ... [Delete] [Delete and Lock User]

@BeforeEach 和 @AfterEach 中的calculator都是写为 this.calculator,但我把this去掉仍然没有问题,这种带this的写法有必要吗?

保持热爱

#2 Created at ... [Delete] [Delete and Lock User]

在这个类中不是必要的,没有歧义的情况下,可以省略

廖雪峰

#3 Created at ... [Delete] [Delete and Lock User]

Q: Java程序哪些变量前面不需要加this?

GPT: 在Java程序中,局部变量和形参不需要加this前缀。只有在实例变量和方法参数同名时才需要使用this来区分。


  • 1

Reply