java this 方法,使用“this”用方法(用Java)
what about using "this" with methods in Java? Is it optional or there are situations when one needs to use it obligatory?
The only situation I have encountered is when in the class you invoke a method within a method. But it is optional. Here is a silly example just to show what I mean:
public class Test {
String s;
private String hey() {
return s;
}
public String getS(){
String sm = this.hey();
// here I could just write hey(); without this
return sm;
}
}
解决方案
Three obvious situations where you need it:
Calling another constructor in the same class as the first part of your constructor
Differentiating between a local variable and an instance variable (whether in the constructor or any other method)
Passing a reference to the current object to another method
Here's an example of all three:
public class Test
{
int x;
public Test(int x)
{
this.x = x;
}
public Test()
{
this(10);
}
public void foo()
{
Helper.doSomethingWith(this);
}
public void setX(int x)
{
this.x = x;
}
}
I believe there are also some weird situations using inner classes where you need super.this.x but they should be avoided as hugely obscure, IMO :)
EDIT: I can't think of any examples why you'd want it for a straight this.foo() method call.
EDIT: saua contributed this on the matter of obscure inner class examples:
I think the obscure case is: OuterClass.this.foo() when accessing foo() of the outer
class from the code in an Inner class that has a foo() method as well.
java this 方法,使用“this”用方法(用Java)相关推荐
- java set 包含_Java Set.contains()方法:判断Set集合是否包含指定的对象
Java 集合类中的 Set.contains() 方法判断 Set 集合是否包含指定的对象.该方法返回值为 boolean 类型,如果 Set 集合包含指定的对象,则返回 true,否则返回 fal ...
- java 判断object类型_Java学习-方法与多态的学习心得
一 1.什么是方法重写 方法的重写或方法的覆盖(overriding) 子类根据需求对从父类继承的方法进行重新编写 重写时,可以用super.方法的方式来保留父类的方法 构造方法不能被重写 2.方法重 ...
- Java培训教程:”==“和 equals 方法究竟有什么区别?
在学习java技术过程中,我们会接触到一些变量值的相关知识,本期小编为大家介绍的教程就是关于"=="和 equals 方法究竟有什么区别?来看看下面的详细介绍. Java培训教程: ...
- 一些防止 Java 代码被反编译的方法
欢迎关注方志朋的博客,回复"666"获面试宝典 由于Java字节码的抽象级别较高,因此它们较容易被反编译.本节介绍了几种常用的方法,用于保护Java字节码不被反编译.通常,这些方法 ...
- 堆、栈、方法区、静态代码块---Java
java 堆.栈.方法区 堆区: 1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令) 2.jvm只有一个堆区(heap)被所有线程共享,堆中不存放基 ...
- Java 查看文件绝对路径,JAVA获取文件绝对路径的方法
本文实例讲述了JAVA获取文件绝对路径的方法.分享给大家供大家参考.具体实现方法如下: /** * 获取一个类的class文件所在的绝对路径. 这个类可以是JDK自身的类,也可以是用户自定义的类,或者 ...
- 【转】Java中获取文件大小的正确方法
[转]Java中获取文件大小的正确方法 本文出处:http://blog.csdn.net/chaijunkun/article/details/22387305,转载请注明.由于本人不定期会整理相关 ...
- java -version cmd_java如何运行步骤cmd?Java执行cmd命令方法有哪些?
写好一个java程序之后,我们的最终目的就是可以正确的运行程序,如果程序运行正确了,那么代码也就没有什么问题了,可是java如何运行步骤cmd?接下来,我们就来给大家讲解一下这方面的内容. 1.首先用 ...
- java 发送邮件 菜鸟_Java发送邮件的方法
1.需要的jar包 2.具体实现方法 1.设置邮箱主机.需要认证.邮箱协议 Properties pro=new Properties(); pro.setProperty("mail.ho ...
- java实验的技术问题及解决方法,2018-2019-2 20175313 实验一《Java开发环境的熟悉》实验报告...
一.实验内容及步骤 使用JDK编译.运行简单的Java程序 cd code进入code文件夹 mkdir 20175313创建20175313文件夹 ls查看当前目录 cd 20175313,mkdi ...
最新文章
- Spring Boot 集成Swagger2生成RESTful API文档
- 常用单词缩写(不断更新)
- java中解析xml解读,java解析xml(JDOM)
- Linux下安装JDK和验证
- java简单课程设计_!高分跪求帮忙写一个简单小程序的JAVA课程设计报告(内详!!)...
- PCL:全程详解 VS2010+PCL配置
- 终于我还是放手了, 但是只想说,暂别 Aptana Studio 3,Phpstorm来吧!(附:个人遇到的小问题的解决方法)...
- 区块链学习笔记:DAY01 区块链的技术原理
- java判断回文用valueof_判断回文数,利用String类
- SVN服务器使用(一)
- 30幅精美的Photoshop三维字体作品欣赏及教程
- SolidWorks2020绘制电脑折叠支架
- 如何封装一个自己的win7系统并安装到电脑做成双系统
- VMware中安装linux系统(可视化界面centOS 7)
- 怎么解决每次打开Office 2013都提示配置进度
- 本地化软件UI部分不显示的一种可能
- 使用outlook及office assitans实现邮件批量发送
- 佳佳mpg格式转换器免费版 v12.4.0.0
- 计算机基础的课程标准,《计算机基础》课程标准
- 电信行业大数据(大数据平台系列)
热门文章
- 1423. 可获得的最大点数
- 计算机毕业设计中用Java编写一个订餐系统(JAVA SWING)
- mysql主主同步稳定吗_MySQL主主同步
- 用python做数据分析教程_Python运用于数据分析的简单教程
- 某音爆火,人物头像动漫化,为女朋友打造独一无二的头像【python实战:人物图片动漫化】
- 为什么手机网速太慢_手机明明是满格信号,为什么网速却非常慢?原来是这些功能在捣鬼...
- php过去文件夹总数,用php获取文件夹内文件的数量
- Python Imaging Library: ImageOps Module(图像运算模块)
- 产品研发过程管理专题——软件工程(软件目的需求开发与管理)
- 计算机考试c语言真题及答案,计算机二级c语言考试试题及答案