美文网首页
this关键字的使用

this关键字的使用

作者: 卓昂芭比 | 来源:发表于2018-11-05 10:49 被阅读0次

    This关键字的使用:

    package test;

    public class Person {

       Stringname;

       private int age;

       public void show(){

         System.out.println("我的名字是"+name+",我今年"+age+"岁.");

         } 

       public void setAge(int age){

         this.age=age;

       }

       public int getAge(){

         return age;

       }

       public boolean compare(Person s){//传递了一个Person类的形参(引用类型)

         if(s.age==this.age){//p.age调用它的成员变量。要访问它的成员必须通过对象取得,

         return true;

        }

          return false;

          }

    }

    package test;

    public class PersonDemo {

       public static void main(String[] args) {

         Personp1=new Person();

         p1.setAge(18);

         Personp2=new Person();

         p1.setAge(20);

         int x=p1.compare(p2);//类作为参数类型,其实需要的是他的对象

         System.out.println(x);//true

       }

    }

    600,118{����c<

    相关文章

      网友评论

          本文标题:this关键字的使用

          本文链接:https://www.haomeiwen.com/subject/mbpcxqtx.html