| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 568 人关注过本帖
标题:版主看到,请帮忙解决一下
只看楼主 加入收藏
lironghua
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2005-9-7
收藏
 问题点数:0 回复次数:2 
版主看到,请帮忙解决一下
请看一下这个程序
public class Person6
{
    static int count=0;
    protected String name;
    protected int age;
    public Person6(String n1,int a1)
    {
        name = n1;
        age = a1;
        this.count++;                          //超类对象计数
    }
    public String toString()
    {
        return this.name+", "+this.age;
    }
    public void print()
    {
        System.out.println("本类名="+this.getClass().getName()+"  "+
             "超类名="+this.getClass().getSuperclass().getName()+"  ");
        System.out.print("Person6.count="+this.count+"  ");
        System.out.print("Student6.count="+Student6.count+"  ");
        Object s1=this;
        if (s1 instanceof Person6)             //判断对象属于哪个类
            System.out.println(s1.toString()+"是Person6类对象。");
        if (s1 instanceof Student6)
            System.out.println(s1.toString()+"是Student6类对象。");
    }
}
class Student6 extends  Person6
{
    static int count=0;                        //隐藏了超类的count
    protected String dept;
    protected Student6(String n1,int a1,String d1)
    {
        super(n1,a1);                          //调用超类的构造方法
        dept = d1;
        this.count++;                          //子类对象计数
    }   
    public String toString()                   //覆盖超类的同名方法
    {
        return super.toString() +", " + dept;  //调用超类的同名方法
    }
    public void print()
    {
        super.print();                         //调用超类的方法
        System.out.println("super.count = "+super.count); //引用超类变量
        System.out.println("this.count  = "+this.count);
    }
    public static void main(String args[])
    {
        Person6 p1 = new Person6("王小明",21) ;
        p1.print();
        Student6 s1 = new Student6("陈小瑞",19,"计算机系");
        s1.print();
    }
}

当调用完s1.print()后person6中的System.out.print("Person6.count="+this.count+"  ");中的this为什么是指向person6这个类,而System.out.println("本类名="+this.getClass().getName()+"  "+
             "超类名="+this.getClass().getSuperclass().getName()+"  ");和 Object s1=this;
中的this却是指向student5这个类,我想问一下哪位大哥,这里的几个this 有何不同
2005-09-23 22:16
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
我将没有必要的 this 都去掉了。
当在 contructor 内是没有必要用this 的。

在某个函数内, 如果用到 this , 那个这个 this 是指当前调用该函数的 object

一般在用IDE 写程序的时候,很多程序员由于记不住那些Api 的名称,即便是自己开发的,函数一多,就很难记忆,
但是IDE 提供了一个提示的方法,那就是先写出 Object 名称,然后加一点,这时你就看到一个 可以 调用的 Api表,这样程序员只要从中挑一个出来就可以,是不是很方便?所以很多程序员就利用了这个方便。用一个this然后加一点,他就可以看到那些可以调用的函数名了,同样的, 利用super 也可以起到这样的效果。
[CODE]
class Person6
{
  static int count=0;
  protected String name;
  protected int age;
  public Person6(String n1,int a1)
  {
    name = n1;
    age = a1;
   count++;                         //超类对象计数
  }
  public String toString()
  {
    return name+", "+age;
  }
  public void print()
  {
    System.out.println("本类名="+getClass().getName()+"  "+
                      "超类名="+getClass().getSuperclass().getName()+"  ");
    System.out.print("Person6.count="+ count+"  ");
    System.out.print("Student6.count="+Student6.count+"  ");
   
    if (this instanceofPerson6)            //判断对象属于哪个类
      System.out.println(this.toString()+"是Person6类对象。");
    if (this instanceof Student6)
      System.out.println(this.toString()+"是Student6类对象。");
  }
}

class Student6 extends  Person6
{
  static intcount=0;                       //隐藏了超类的count
  protected String dept;
  protected Student6(String n1,int a1,String d1)
  {
   super(n1,a1);                         //调用超类的构造方法
    dept = d1;
   count++;                         //子类对象计数
  }   
  public StringtoString()                  //覆盖超类的同名方法
  {
    return super.toString() +", " + dept;  //调用超类的同名方法
  }
  public void print()
  {
   super.print();                        //调用超类的方法
    System.out.println("super.count = "+super.count); //引用超类变量
    System.out.println("this.count  = "+count);
  }
  public static void main(String args[])
  {
    Person6 p1 = new Person6("王小明",21) ;
    p1.print();
    Student6 s1 = new Student6("陈小瑞",19,"计算机系");
    s1.print();
  }
}
[/CODE]

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-23 23:14
lironghua
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2005-9-7
收藏
得分:0 
多谢版主提示,,以后应该多向你们学习
2005-09-24 11:52
快速回复:版主看到,请帮忙解决一下
数据加载中...
 
   



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

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