zoukankan      html  css  js  c++  java
  • this关键字

    -----------siwuxie095

       

       

       

    this 关键字:

       

    (1)表示类中的属性和调用方法

       

    2)表示本类中的构造方法

       

    3)表示当前对象

       

       

       

    代码1

       

    package com.siwuxie095.thisdemo;

       

    class People{

    private String name;

    private int age;

     

    public People(String name,int age) {

    //代表构造方法,且必须放在首行,否则无法通过编译

    this();

    this.name=name;

    this.age=age;

    }

     

    public People() {

    System.out.println("无参构造方法");

    }

     

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public int getAge() {

    return age;

    }

    public void setAge(int age) {

    this.age = age;

    }

     

    public void tell() {

    System.out.println("姓名:"+this.getName()+" 年龄:"+this.getAge());

    }

     

    }

       

    public class ThisDemo01 {

       

    public static void main(String[] args) {

    People p=new People("张三",30);

    p.tell();

    }

       

    }

       

       

    运行一览:

       

       

       

       

    代码2

       

    package com.siwuxie095.thisdemo;

       

    class PeopleX{

     

    public void tell() {

    //this 表示当前对象

    System.out.println(this);

    }

    }

       

    public class ThisDemo02 {

       

    public static void main(String[] args) {

    PeopleX p=new PeopleX();

    //输出一致,可以通过这种方式比较两个对象是不是同一对象

    System.out.println(p);

    p.tell();

    }

       

    }

       

       

    运行一览:

       

       

       

       

       

       

    【made by siwuxie095】

  • 相关阅读:
    详解ASP.NET的内置对象
    如何架设FTP服务器
    输出JSON问题
    new , virtual , override
    Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)
    Java回顾之I/O
    数据结构之栈和队列
    Java回顾之多线程同步
    设计模式之行为型模式
    Java回顾之网络通信
  • 原文地址:https://www.cnblogs.com/siwuxie095/p/6561997.html
Copyright © 2011-2022 走看看