黑马程序员技术交流社区

标题: super关键字用法 [打印本页]

作者: 姿_`态    时间: 2014-6-14 18:50
标题: super关键字用法
本帖最后由 姿_`态 于 2014-6-16 17:21 编辑

子类中的下面两句话是不是重复啊,我怎么感觉重复啊,但是去掉下面的有不对 到底是什么原因啊super(i);this.i=i;


class test{
        public static void main(String[] args){
               
                A a1=new A(20);
                A a2=new A(20);
                System.out.println("a1.equals(a2)="+a1.equals(a2));
               
                B b1=new B(10);
                B b2=new B(100);
                System.out.println("b1.equals(b2)="+b1.equals(b2));
               
        }
}
class A{
                private int i;
                A(int i){
                        this.i=i;
                }
                public boolean equals(A a){
                        
                        if(this.i==a.i){
                                return true;
                        }
                                
                        else{
                                return false;
                        }

                }
        }
        
class B extends A{
                private int i;
                B(int i){
                        super(i);
                        this.i=i;
                }
                public boolean equals(A a){
                        B b=(B)a;
                        if(this.i==b.i){
                                return true;
                        }
                                
                        else{
                                return false;
                        }
                }
        }


作者: tiuwing    时间: 2014-6-14 19:56
肯定不会重复的啦!首先,super(i),是不能省略的,因为构造函数如果省略super(i),它会默认调用父类的无参构造函数,而你的A类没定义无参构造函数,所以注定失败。而变量i,已经被定义为私有,也就是子类并无法访问它。B中的i和A中的i并不是同一个东西!!
作者: 姿_`态    时间: 2014-6-14 20:16
tiuwing 发表于 2014-6-14 19:56
肯定不会重复的啦!首先,super(i),是不能省略的,因为构造函数如果省略super(i),它会默认调用父类的无参 ...

你的意思是 不定义为私有 就可以访问啦?
作者: tiuwing    时间: 2014-6-14 20:57
本帖最后由 tiuwing 于 2014-6-14 20:58 编辑
姿_`态 发表于 2014-6-14 20:16
你的意思是 不定义为私有 就可以访问啦?

恩,你这程序完全可以写成
  1. class test{
  2.         public static void main(String[] args){
  3.                
  4.                 A a1=new A(20);
  5.                 A a2=new A(20);
  6.                 System.out.println("a1.equals(a2)="+a1.equals(a2));
  7.                
  8.                 B b1=new B(100);
  9.                 B b2=new B(100);
  10.                 System.out.println("b1.equals(b2)="+b1.equals(b2));
  11.                
  12.         }
  13. }
  14. class A{
  15.                 protected int i;  //换成protected,子类可以访问
  16.                 A(int i){
  17.                         this.i=i;
  18.                 }
  19.                 public boolean equals(A a){
  20.                         
  21.                         if(this.i==a.i){
  22.                                 return true;
  23.                         }
  24.                                 
  25.                         else{
  26.                                 return false;
  27.                         }

  28.                 }
  29.         }
  30.         
  31. class B extends A{
  32.                 B(int i){
  33.                         super(i);
  34.                 }
  35.         }
复制代码

作者: 西门吹风    时间: 2014-6-14 21:10
父类中的i为私有,不能被继承,所以不重复
作者: crazylong    时间: 2014-6-14 23:11
哥们你学的真快
作者: 姿_`态    时间: 2014-6-15 10:21
tiuwing 发表于 2014-6-14 20:57
恩,你这程序完全可以写成

懂了 ,权限的问题。
作者: 姿_`态    时间: 2014-6-15 10:22
西门吹风 发表于 2014-6-14 21:10
父类中的i为私有,不能被继承,所以不重复

谢谢啊,明白了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2