this关键字
来源:互联网 发布:实变函数和实分析 知乎 编辑:程序博客网 时间:2024/06/05 15:00
package exercise;public class Teacher2 { private String name; private int age; public String getname(){ return name; //返回的是Teacher2类的name,要不要this都可以。 //因为编译器会知道这个name就指的是Teacher2的name而不是其他的name。 } public void setName(String name){ this.name = name;//第一个name是Teacher2的name属性 //第二个name是setName方法的参数name } public int getAge(){ return age; //返回的是Teacher2类的age,要不要this都可以。 //因为编译器会知道这个name就指的是Teacher2的age而不是其他的age。 } public void setAge(int age) { this.age = age; } public String introduction() { return "大家好!我是"+name+",今年我"+age+"岁"; }}
package exercise;public class teacherTest { public static void main(String[] args) { Teacher2 tea = new Teacher2(); tea.setName("Simple"); tea.setAge(25); System.out.println(tea.introduction()); }}
this关键字是什么意思?
- 一个方法需要引用调用它的对象
- this是在对象内部指代自身的引用
- this可以使你直接引用对象,解决实例变量和局部变量之间发生的任何同名的冲突 -
阅读全文
0 0
- 关键字this
- this关键字
- this关键字
- this 关键字
- this 关键字
- this关键字
- this关键字
- this 关键字
- this 关键字
- this 关键字
- this关键字
- 关键字this
- 关键字this
- this 关键字
- this 关键字
- this关键字
- this关键字
- This关键字
- MySQL的存储过程declare语句报错 游标
- Java基础--标识符,关键字,数字类型
- shell脚本题
- spring整合mybatis
- excel中如何将数字转换成文本
- this关键字
- 蓝桥杯 算法提高 身份证号码升级
- python函数
- LitJson使用范例
- RxJava简单入门
- 谷歌账号手机验证失败的一些解决方法
- 为什么写博客
- 在FPGA的ROMIP核中导入由txt建立的coe数据文件并仿真
- 【 Tomcat 】tomcat8.0 基本参数调优配置