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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

©   /  2014-11-25 10:01  /  1513 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

我写了2个列子,不知道能对你有帮助吗
class shuzi
{
        int x = 1;//对象所有
        public void print()
        {
                int x = 2;//方法所有
                System.out.println(x);//结果是 2 方法中有就在方法中找
                System.out.println(this.x);//结果是 1 这里的this.就是代表对象,这时候不能省略
        }
}
class demo
{
        public static void main(String[] args)
        {
                shuzi s = new shuzi();
                s.print();
        }
}
.........................................................................................
class persondemo
{
        public static void main(String[] args)
                {
                        person p1 = new person("zhangsan",23);
                }       
class person
{
        private String name;
        private int age;
        person(String str,int arg)
        {                                                  //这个构造函数在构建本类对象的时候就能初始化
                this.name = str;          //这里的this.就代表自己了
                this.age = arg;
        }
}

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马