this用法
来源:互联网 发布:詹姆斯下赛季数据 编辑:程序博客网 时间:2024/06/05 14:39
例 show.setKnowledges(knows).setVideos(videos);
用类名定义一个变量的时候,定义的只是一个引用,外面可以通过这个引用来访问这个类里面的属性和方法。
那们类里面是够也应该有一个引用来访问自己的属性和方法纳?
呵呵,JAVA提供了一个很好的东西,就是 this 对象,它可以在类里面来引用这个类的属性和方法。先来个简单的例子:
public class ThisDemo { String name="Mick"; public void print(String name){ System.out.println("类中的属性 name="+this.name); System.out.println("局部传参的属性="+name); } public static void main(String[] args) { ThisDemo tt=new ThisDemo(); tt.print("Orson"); }}
关于返回类自身的引用,《Thinking in Java》有个很经典的例子。
通过this 这个关键字返回自身这个对象然后在一条语句里面实现多次的操作,还是贴出来。
public class ThisDemo { int number; ThisDemo increment(){ number++; return this; } private void print(){ System.out.println("number="+number); } public static void main(String[] args) { ThisDemo tt=new ThisDemo(); tt.increment().increment().increment().print(); }}
0 0
- this用法
- this用法
- this用法
- this用法
- this的用法
- This、Using、关键字用法
- return this的用法
- javascript this用法
- JAVA this用法
- javascript this用法
- java之this用法
- this的用法
- this用法总结
- this指针的用法
- java中 this用法
- this的深入用法
- this用法解释
- this,super的用法
- UML结构总结
- org-mode
- 苹果开发者账号申请与简介
- scp从服务器下载文件到本地或从本地上传文件到服务器
- 几种常见的Shell
- this用法
- Camera实现滤镜方法探讨-Android
- spark Streaming 存储级别定义StorageLevel
- 滴滴笔试-地下迷宫
- Python filter 和map
- 使用Fragment实现tabhost效果
- Linux下的 FTP
- 【那些年遇到过的面试题】考虑如何将一个vector 赋给另一个vector
- 71. Simplify Path