08.this的使用方法

  • 使用this调用成员变量和成员函数
  1. classPerson{
  2. String name;
  3. void talk(){
  4. System.out.println("my name is "+this.name);
  5. }
  6. }
 
  1. classTestA{
  2. publicstaticvoid main(String args[]){
  3. Person p1 =newPerson();
  4. p1.name ="zhangsan";
  5. Person p2 =newPerson();
  6. p2.name ="lisi";
  7. p1.talk();
  8. p2.talk();
  9. }
  10. }
 
 
  • 使用this调用构造函数
    • 对this的调用函数必须是构造函数的第一个语句
 
  1. classTestA{
  2. publicstaticvoid main(String args[]){
  3. Person p1 =newPerson("zhangsan",20,"beijing");
  4. }
  5. }
 
  1. classPerson{
  2. String name;
  3. int age;
  4. String address;
  5. Person(){
  6. System.out.println("无参数的构造函数");
  7. }
  8. Person(String name,int age){
  9. this();
  10. this.name = name;
  11. this.age = age;
  12. System.out.println("两个参数的构造函数");
  13. }
  14. Person(String name,int age,String address){
  15. this(name,age);
  16. this.address = address;
  17. System.out.println("三个参数的构造函数");
  18. }
  19. }
 





转载于:https://www.cnblogs.com/arroneve/p/5815429.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值