body, td {
font-family: 微软雅黑;
font-size: 10pt;
}
Edit
TIJ读书笔记05-this关键字
- TIJ读书笔记05-this关键字
- 概述
- this的用法
- static方法
概述
两个对象同时调用一个方法,那么jvm怎么知道是哪个方法调用的呢. 我们在书写的时候会写成a.fun(1)
或者b.fun(1)
如果按照更容易理解的角度来看的话,或许可以写成: Banana.fun(a,1)
这样就更符合OOP的”发送消息给对象”的原则了. 但是java帮助我们做了很多事情,在调用每个方法的时候,默认第一个传入的参数就是调用该方法的对象的引用. 让我们可以写成a.fun(1)
.
理解了函数调用的方法,那么对于this就很好理解了. this是一个特殊的引用. 它表示调用方法的那个对象.
在一个类的内部调用该类的其他方法,可是省略this,因为编译器会自动添加”this”
埃尔克,埃大爷说在一个类的内部调用该类的其他方法的时候,不建议写this. 因为编译器已经自动帮你做了这个事情,你还去写,你是不是傻. 但是,我还是觉得,至少看上去代码格式比较统一,而且我的eclipse的自动提示触发只保留了”.”,写this.
会自动提示…既然没有什么性能上的区别,这个就看个人风格了.
this的用法
this代表调用方法的那个对象的引用. 理解了这一点this就可以用来做很多很神奇的事情.
返回该对象
比如下面这个例子这样,可以返回当前调用函数的对象.
public class Leaf { int i=0; public Leaf increment(){ i++; return this; } public void print(){ System.out.println("i="+this.i); } public static void main(String[] args) { Leaf x = new Leaf(); x.increment().increment().increment().print(); }}
this作为参数传入给一个方法
比如这个例子
class Person{ public void eat(Apple apple){ Apple peeled = apple.getPeeled(); System.out.println("eat apple..."); }}class Peeler{ public static Apple peel(Apple apple){ System.out.println("peeled..."); return apple; }}class Apple{ public Apple getPeeled(){ return Peeler.peel(this); }}public class PassingThis { public static void main(String[] args) { new Person().eat(new Apple()); }}
在构造方法中调用另外一个构造方法
在构造函数中可以去调用另外一个构造函数. 但是有一点需要注意,这种调用只能使用一次,而且必须写在起始行.
public class PersonTest { private String name; private int age=18; public PersonTest() {
} PersonTest(String name){ this.name = name; } public PersonTest(String name,int age) { this(name); this.age=age; } @Override public String toString() { return "PersonTest [name=" + name + ", age=" + age + "]"; } public static void main(String[] args) { PersonTest pt1 = new PersonTest("tom"); PersonTest pt2 = new PersonTest("jim",20);
System.out.println(pt1); System.out.println(pt2); }}
static方法
理解了this之后,static方法就很好理解. static方法又叫静态方法,有点像全局方法. 埃大爷的说,static方法就是没有this的方法. 也就是说不需要通过实例来调用的方法.
这个解释简直精辟到无以附加,对埃大爷的崇拜突然如滔滔江水连绵不绝. 因为没有this,所以无法调用非静态方法.前面说了实例方法调用的时候默认第一个形参传入的是this. static方法没有传入this,那么在其中是无法调用实例方法的. 同理也没有办法调用实例变量.
反证法,如果static方法可以调用实例方法和实例变量. 因为static方法是属于一个类的. 通过类名来调用. 那么在调用static方法的时候,很有可能根本就没有创建这个类的实例,那么问题来了. 既然没有实例,那么不可能调用到实例方法和实例变量. 所以static方法不能调用实例方法和实例变量.
一个正常的类不应该存在过多的static方法.这玩意儿更像是一个全局方法,java中是禁止全局方法的. 但是有个特例,工具类中的方法绝大部分都应该是static.
%23TIJ%u8BFB%u4E66%u7B14%u8BB005-this%u5173%u952E%u5B57%[email protected]%28034.java%29%5Bjava%7Cjava%u7F16%u7A0B%u601D%u60F3%5D%0A%0A%5BTOC%5D%0A%0A%23%23%u6982%u8FF0%0A%u4E24%u4E2A%u5BF9%u8C61%u540C%u65F6%u8C03%u7528%u4E00%u4E2A%u65B9%u6CD5%2C%u90A3%u4E48jvm%u600E%u4E48%u77E5%u9053%u662F%u54EA%u4E2A%u65B9%u6CD5%u8C03%u7528%u7684%u5462.%20%u6211%u4EEC%u5728%u4E66%u5199%u7684%u65F6%u5019%u4F1A%u5199%u6210%60a.fun%281%29%60%20%u6216%u8005%60b.fun%281%29%60%u5982%u679C%u6309%u7167%u66F4%u5BB9%u6613%u7406%u89E3%u7684%u89D2%u5EA6%u6765%u770B%u7684%u8BDD%2C%u6216%u8BB8%u53EF%u4EE5%u5199%u6210%3A%0A%60Banana.fun%28a%2C1%29%60%u8FD9%u6837%u5C31%u66F4%u7B26%u5408OOP%u7684%22%u53D1%u9001%u6D88%u606F%u7ED9%u5BF9%u8C61%22%u7684%u539F%u5219%u4E86.%20%u4F46%u662Fjava%u5E2E%u52A9%u6211%u4EEC%u505A%u4E86%u5F88%u591A%u4E8B%u60C5%2C%u5728%u8C03%u7528%u6BCF%u4E2A%u65B9%u6CD5%u7684%u65F6%u5019%2C%u9ED8%u8BA4%u7B2C%u4E00%u4E2A%u4F20%u5165%u7684%u53C2%u6570%u5C31%u662F%u8C03%u7528%u8BE5%u65B9%u6CD5%u7684%u5BF9%u8C61%u7684%u5F15%u7528.%20%u8BA9%u6211%u4EEC%u53EF%u4EE5%u5199%u6210%60a.fun%281%29%60.%20%0A%u7406%u89E3%u4E86%u51FD%u6570%u8C03%u7528%u7684%u65B9%u6CD5%2C%u90A3%u4E48%u5BF9%u4E8Ethis%u5C31%u5F88%u597D%u7406%u89E3%u4E86.%20this%u662F%u4E00%u4E2A%u7279%u6B8A%u7684%u5F15%u7528.%20%u5B83%u8868%u793A%u8C03%u7528%u65B9%u6CD5%u7684%u90A3%u4E2A%u5BF9%u8C61.%0A%0A%u5728%u4E00%u4E2A%u7C7B%u7684%u5185%u90E8%u8C03%u7528%u8BE5%u7C7B%u7684%u5176%u4ED6%u65B9%u6CD5%2C%u53EF%u662F%u7701%u7565this%2C%u56E0%u4E3A%u7F16%u8BD1%u5668%u4F1A%u81EA%u52A8%u6DFB%u52A0%22this%22%0A%0A%u57C3%u5C14%u514B%2C%u57C3%u5927%u7237%u8BF4%u5728%u4E00%u4E2A%u7C7B%u7684%u5185%u90E8%u8C03%u7528%u8BE5%u7C7B%u7684%u5176%u4ED6%u65B9%u6CD5%u7684%u65F6%u5019%2C%u4E0D%u5EFA%u8BAE%u5199this.%20%u56E0%u4E3A%u7F16%u8BD1%u5668%u5DF2%u7ECF%u81EA%u52A8%u5E2E%u4F60%u505A%u4E86%u8FD9%u4E2A%u4E8B%u60C5%2C%u4F60%u8FD8%u53BB%u5199%2C%u4F60%u662F%u4E0D%u662F%u50BB.%20%u4F46%u662F%2C%u6211%u8FD8%u662F%u89C9%u5F97%2C%u81F3%u5C11%u770B%u4E0A%u53BB%u4EE3%u7801%u683C%u5F0F%u6BD4%u8F83%u7EDF%u4E00%2C%u800C%u4E14%u6211%u7684eclipse%u7684%u81EA%u52A8%u63D0%u793A%u89E6%u53D1%u53EA%u4FDD%u7559%u4E86%22.%22%2C%u5199%60this.%60%u4F1A%u81EA%u52A8%u63D0%u793A...%u65E2%u7136%u6CA1%u6709%u4EC0%u4E48%u6027%u80FD%u4E0A%u7684%u533A%u522B%2C%u8FD9%u4E2A%u5C31%u770B%u4E2A%u4EBA%u98CE%u683C%u4E86.%20%0A%0A%23%23this%u7684%u7528%u6CD5%0Athis%u4EE3%u8868%u8C03%u7528%u65B9%u6CD5%u7684%u90A3%u4E2A%u5BF9%u8C61%u7684%u5F15%u7528.%20%u7406%u89E3%u4E86%u8FD9%u4E00%u70B9this%u5C31%u53EF%u4EE5%u7528%u6765%u505A%u5F88%u591A%u5F88%u795E%u5947%u7684%u4E8B%u60C5.%20%0A**%u8FD4%u56DE%u8BE5%u5BF9%u8C61**%0A%u6BD4%u5982%u4E0B%u9762%u8FD9%u4E2A%u4F8B%u5B50%u8FD9%u6837%2C%u53EF%u4EE5%u8FD4%u56DE%u5F53%u524D%u8C03%u7528%u51FD%u6570%u7684%u5BF9%u8C61.%0A%60%60%60java%0Apublic%20class%20Leaf%20%7B%0A%09int%20i%3D0%3B%0A%09public%20Leaf%20increment%28%29%7B%0A%09%09i++%3B%0A%09%09return%20this%3B%0A%09%7D%0A%09public%20void%20print%28%29%7B%0A%09%09System.out.println%28%22i%3D%22+this.i%29%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09Leaf%20x%20%3D%20new%20Leaf%28%29%3B%0A%09%09x.increment%28%29.increment%28%29.increment%28%29.print%28%29%3B%0A%09%7D%0A%7D%0A%0A%60%60%60%0A%0A**this%u4F5C%u4E3A%u53C2%u6570%u4F20%u5165%u7ED9%u4E00%u4E2A%u65B9%u6CD5**%0A%u6BD4%u5982%u8FD9%u4E2A%u4F8B%u5B50%0A%60%60%60java%0Aclass%20Person%7B%0A%09public%20void%20eat%28Apple%20apple%29%7B%0A%09%09Apple%20peeled%20%3D%20apple.getPeeled%28%29%3B%0A%09%09System.out.println%28%22eat%20apple...%22%29%3B%0A%09%7D%0A%7D%0Aclass%20Peeler%7B%0A%09public%20static%20Apple%20peel%28Apple%20apple%29%7B%0A%09%09System.out.println%28%22peeled...%22%29%3B%0A%09%09return%20apple%3B%0A%09%7D%0A%7D%0Aclass%20Apple%7B%0A%09public%20Apple%20getPeeled%28%29%7B%0A%09%09return%20Peeler.peel%28this%29%3B%0A%09%7D%0A%7D%0Apublic%20class%20PassingThis%20%7B%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09new%20Person%28%29.eat%28new%20Apple%28%29%29%3B%0A%09%7D%0A%7D%0A%0A%60%60%60%0A%0A**%u5728%u6784%u9020%u65B9%u6CD5%u4E2D%u8C03%u7528%u53E6%u5916%u4E00%u4E2A%u6784%u9020%u65B9%u6CD5**%0A%u5728%u6784%u9020%u51FD%u6570%u4E2D%u53EF%u4EE5%u53BB%u8C03%u7528%u53E6%u5916%u4E00%u4E2A%u6784%u9020%u51FD%u6570.%20%u4F46%u662F%u6709%u4E00%u70B9%u9700%u8981%u6CE8%u610F%2C%u8FD9%u79CD%u8C03%u7528%u53EA%u80FD%u4F7F%u7528%u4E00%u6B21%2C%u800C%u4E14%u5FC5%u987B%u5199%u5728%u8D77%u59CB%u884C.%0A%60%60%60java%0Apublic%20class%20PersonTest%20%7B%0A%09private%20String%20name%3B%0A%09private%20int%20age%3D18%3B%0A%09public%20PersonTest%28%29%20%7B%0A%09%09%0A%09%7D%0A%09PersonTest%28String%20name%29%7B%0A%09%09this.name%20%3D%20name%3B%0A%09%7D%0A%09public%20PersonTest%28String%20name%2Cint%20age%29%20%7B%0A%09%09this%28name%29%3B%0A%09%09this.age%3Dage%3B%0A%09%7D%0A%[email protected]%0A%09public%20String%20toString%28%29%20%7B%0A%09%09return%20%22PersonTest%20%5Bname%3D%22%20+%20name%20+%20%22%2C%20age%3D%22%20+%20age%20+%20%22%5D%22%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09PersonTest%20pt1%20%3D%20new%20PersonTest%28%22tom%22%29%3B%0A%09%09PersonTest%20pt2%20%3D%20new%20PersonTest%28%22jim%22%2C20%29%3B%0A%09%09%0A%09%09System.out.println%28pt1%29%3B%0A%09%09System.out.println%28pt2%29%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%0A%23%23static%u65B9%u6CD5%0A%u7406%u89E3%u4E86this%u4E4B%u540E%2Cstatic%u65B9%u6CD5%u5C31%u5F88%u597D%u7406%u89E3.%20static%u65B9%u6CD5%u53C8%u53EB%u9759%u6001%u65B9%u6CD5%2C%u6709%u70B9%u50CF%u5168%u5C40%u65B9%u6CD5.%20%u57C3%u5927%u7237%u7684%u8BF4%2Cstatic%u65B9%u6CD5%u5C31%u662F%u6CA1%u6709this%u7684%u65B9%u6CD5.%20%u4E5F%u5C31%u662F%u8BF4%u4E0D%u9700%u8981%u901A%u8FC7%u5B9E%u4F8B%u6765%u8C03%u7528%u7684%u65B9%u6CD5.%20%0A%0A%u8FD9%u4E2A%u89E3%u91CA%u7B80%u76F4%u7CBE%u8F9F%u5230%u65E0%u4EE5%u9644%u52A0%2C%u5BF9%u57C3%u5927%u7237%u7684%u5D07%u62DC%u7A81%u7136%u5982%u6ED4%u6ED4%u6C5F%u6C34%u8FDE%u7EF5%u4E0D%u7EDD.%20%u56E0%u4E3A%u6CA1%u6709this%2C%u6240%u4EE5%u65E0%u6CD5%u8C03%u7528%u975E%u9759%u6001%u65B9%u6CD5.%u524D%u9762%u8BF4%u4E86%u5B9E%u4F8B%u65B9%u6CD5%u8C03%u7528%u7684%u65F6%u5019%u9ED8%u8BA4%u7B2C%u4E00%u4E2A%u5F62%u53C2%u4F20%u5165%u7684%u662Fthis.%20static%u65B9%u6CD5%u6CA1%u6709%u4F20%u5165this%2C%u90A3%u4E48%u5728%u5176%u4E2D%u662F%u65E0%u6CD5%u8C03%u7528%u5B9E%u4F8B%u65B9%u6CD5%u7684.%20%u540C%u7406%u4E5F%u6CA1%u6709%u529E%u6CD5%u8C03%u7528%u5B9E%u4F8B%u53D8%u91CF.%20%0A%0A%u53CD%u8BC1%u6CD5%2C%u5982%u679Cstatic%u65B9%u6CD5%u53EF%u4EE5%u8C03%u7528%u5B9E%u4F8B%u65B9%u6CD5%u548C%u5B9E%u4F8B%u53D8%u91CF.%20%u56E0%u4E3Astatic%u65B9%u6CD5%u662F%u5C5E%u4E8E%u4E00%u4E2A%u7C7B%u7684.%20%u901A%u8FC7%u7C7B%u540D%u6765%u8C03%u7528.%20%u90A3%u4E48%u5728%u8C03%u7528static%u65B9%u6CD5%u7684%u65F6%u5019%2C%u5F88%u6709%u53EF%u80FD%u6839%u672C%u5C31%u6CA1%u6709%u521B%u5EFA%u8FD9%u4E2A%u7C7B%u7684%u5B9E%u4F8B%2C%u90A3%u4E48%u95EE%u9898%u6765%u4E86.%20%u65E2%u7136%u6CA1%u6709%u5B9E%u4F8B%2C%u90A3%u4E48%u4E0D%u53EF%u80FD%u8C03%u7528%u5230%u5B9E%u4F8B%u65B9%u6CD5%u548C%u5B9E%u4F8B%u53D8%u91CF.%20%u6240%u4EE5static%u65B9%u6CD5%u4E0D%u80FD%u8C03%u7528%u5B9E%u4F8B%u65B9%u6CD5%u548C%u5B9E%u4F8B%u53D8%u91CF.%20%0A%0A%u4E00%u4E2A%u6B63%u5E38%u7684%u7C7B%u4E0D%u5E94%u8BE5%u5B58%u5728%u8FC7%u591A%u7684static%u65B9%u6CD5.%u8FD9%u73A9%u610F%u513F%u66F4%u50CF%u662F%u4E00%u4E2A%u5168%u5C40%u65B9%u6CD5%2Cjava%u4E2D%u662F%u7981%u6B62%u5168%u5C40%u65B9%u6CD5%u7684.%20%u4F46%u662F%u6709%u4E2A%u7279%u4F8B%2C%u5DE5%u5177%u7C7B%u4E2D%u7684%u65B9%u6CD5%u7EDD%u5927%u90E8%u5206%u90FD%u5E94%u8BE5%u662Fstatic.%20%0A