| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 462 人关注过本帖
标题:this问题
只看楼主 加入收藏
蓝天c语言
Rank: 2
等 级:论坛游民
帖 子:52
专家分:45
注 册:2010-8-27
结帖率:82.61%
收藏
已结贴  问题点数:5 回复次数:3 
this问题
程序中  MyDate new_date = new MyDate(this);中的this 在本程序中起到什么作用?麻烦解释一下??(有些说是把本身当做参数赋给其他对象,本身是指哪个??)


public class MyDate
{
    public static void main(String[] args)
    {
        MyDate my_birth=new MyDate(22,7,1964);
        MyDate the_next_week=my_birth.addDays(7);
        the_next_week.print();
    }
   
    public MyDate(int day,int month,int year)
    {
    this.day   = day;
    this.month = month;
    this.year  = year;
    }

    public MyDate(MyDate date) {
    this.day   = date.day;
    this.month = date.month;
    this.year  = date.year;
    }

    public int getDay() {
    return day;
    }

    public void setDay(int day)
    {
    this.day = day;
    }

    public MyDate addDays(int more_days)
    {
    MyDate new_date = new MyDate(this);  //这里的this 是什么意思??
    new_date.day = new_date.day + more_days;
    return new_date;
    }

    public void print()
    {
    System.out.println("MyDate: " + day + "-" + month +    "-" + year);
    }

    private int day; //日
    private int month; //月
    private int year;  //年
     
   
}

搜索更多相关主题的帖子: class public 
2011-04-26 23:09
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
收藏
得分:2 
this 指的就是对象自己本身

如果你定义了一个class A{}要使用它的东西 除类类型的(静态的)成员 没有this  都要进行类的实例化(对象)  this就是值这里的对象
2011-04-27 09:43
风兮飞扬
该用户已被删除
收藏
得分:2 
提示: 作者被禁止或删除 内容自动屏蔽
2011-04-27 16:26
幸馥
Rank: 1
等 级:新手上路
帖 子:9
专家分:2
注 册:2010-12-28
收藏
得分:2 
java里是通过对象调用方法的,你这个类里有两个构造方法,而你那个语句调用的是第二个,就是用那个调用的对象的内容(day,month,year)给你新分配的空间赋值,或者说初始化。我这么觉得
2011-04-27 20:25
快速回复:this问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011488 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved