A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王烽棋 中级黑马   /  2015-8-6 13:36  /  298 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

this:Java关键字
含义:代表其所在方法所属类的对象的引用。
说明:this关键字只能在方法内部使用,表示对“调用方法的那个对象”的引用。this的用法和其他对象的引用并无不同。注意,如果在方法内调用同一个类的            另 一个方法,就不必使用this。
使用:
        局部变量隐藏成员变量:
  1. /**
  2.         定义人
  3. */
  4. abstract class Person
  5. {
  6.         /**姓名年龄*/
  7.         private String name;
  8.         private int age;
  9.         /**吃饭*/
  10.         public abstract void eat();
  11.        
  12.         /**无参构造方法*/
  13.         Person(){}
  14.         /**带参构造方法*/
  15.         Person(String name,int age){
  16.                 this.name = name;
  17.                 this.age = age;
  18.         }
  19.         /**set和get方法*/
  20.         public void setName(String name){
  21.                 this.name = name;
  22.         }
  23.         public void setName(int age){
  24.                 this.age = age;
  25.         }
  26.         public String getName(){
  27.                 return name;
  28.         }
  29.         public int getAge(){
  30.                 return age;
  31.         }
  32. }
复制代码
       返回当前引用:
      
  1. public class Leaf{
  2.       int  i  = 0 ;
  3.       Leaf increment(){
  4.              i ++ ;
  5.              return this ;
  6.       }
  7.       void print(){
  8.               System . out . println("i ="+ i);
  9.       }
  10.       public static void main(String[] args){
  11.                 Leaf x = new Leaf() ;
  12.                 x . increment() . increment() . increment() . print() ;
  13.         }
  14. }
复制代码

           作为参数传递给其他方法:
  1. /**创建人类*/
  2. class Person
  3. {
  4.         public void eat(Apple apple){
  5.                 Apple peeled = apple.getPeeled();
  6.                 System.out.println("Yummy");
  7.         }
  8. }

  9. /**创建削苹果类*/
  10. class Peeler
  11. {
  12.         static Apple peel(Apple apple){
  13.                 return apple;//Peeled
  14.         }
  15. }

  16. /**创建苹果类*/
  17. class Apple
  18. {
  19.         Apple getPeeled(){
  20.                 return Peeler.peel(this);
  21.         }
  22. }

  23. /**创建测试类*/
  24. class PassingThis
  25. {
  26.         public static void main(String[] args){
  27.                 new Person().eat(new Apple);
  28.         }
  29. }
复制代码
        在构造方法中调用构造方法:
  1. /**创建花类*/
  2. class Flower
  3. {
  4.         /**成员属性*/
  5.         int peatalCount = 0;
  6.         String s = "initial value";
  7.     /**空参构造函数*/
  8.         Flower(){
  9.                 this("hi",47);
  10.                 System.out.println("default constructor (no args)");
  11.         }
  12.         /**一个参数构造函数*/
  13.         Flower(int petals){
  14.                 peatalCount = petals;
  15.                 System.out.println("constructor w/int arg only,peatalCount ="+peatalCount);
  16.         }
  17.         /**一个参数构造函数*/
  18.         Flower(String ss){
  19.                 s = ss;
  20.                 System.out.println("constructor w/String arg only,s ="+s);
  21.         }
  22.         /**两个参数构造函数*/
  23.         Flower(String s,int petals){
  24.                 this(petals);
  25.                 //this(s); //Can't call two!
  26.                 this.s = s;
  27.                 System.out.println("String and int args only,s =");
  28.         }
  29.         /**打印属性*/
  30.         void printPetalCount(){
  31.                 //this.(11); //Not inside non constructor!
  32.                 System.out.println("peatalCount ="+peatalCount+"s ="+s);
  33.         }

  34.         public static void main(String[] args){
  35.                 Flower x = new Flower();
  36.                 x.printPetalCount();
  37.         }
  38. }
复制代码

注意:this调用构造方法只能用在构造方法内的第一行,不可以在非构造方法内如此调用构造方法。



                     

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马