java-引用数组、继承、super关键字

1.引用类型数组:

  1)   Cell[] cells = new Cell[4];

      cells[0] = new Cell(2,5);

         cells[1] = new Cell(2,6);

         cells[2] = new Cell(2,7);

         cells[3] = new Cell(3,6);

  2)Cell[] cells = new Cell[]{

        new Cell(2,5),

           new Cell(2,6),

           new Cell(2,7),

           new Cell(3,6)

    };

  3)  int[][] arr = new int[3][];

        arr[0] = new int[2];

         arr[1] = new int[3];

         arr[2] = new int[2];

         arr[1][0] = 100; //给arr中第2个元素中的第1个元素赋值为100

  4)int[][] arr = new int[3][4];

    for(int i=0;i<arr.length;i++){

           for(int j=0;j<arr[i].length;j++){

             arr[i][j] = 100;

           }

         }

2.继承:

  1)作用:实现代码的复用

  2)通过extends来实现继承

  3)父类:所有子类所共有的属性和行为

     子类:子类所特有的属性和行为

  4)子类继承父类后,子类具有:子类+父类的所有属性

  5)一个父类可以有多个子类,一个子类只能继承一个父类----单一继承不能多继承

  6)继承具有传递性------父类的属性子类同时拥有

  7)java规定:构造子类之前必须先构造父类,在子类构造中若自己不调用父类的构造,则默认super()调父类的无参构造

         若在子类构造中调用了父类的构造,则不再默认提供 super()调用父类构造方法,必须位于子类构造的第一行

3.super:指代当前对象的父类对象

  super的用法:

    1)super.成员变量名----------访问父类的成员变量

         2)super.方法名()------------调用父类的方法

         3)super()-------------------调用父类的构造方法

4.向上造型:

  1)父类型的引用指向了子类的对象

  2)能点(.)出来什么,看引用的类型

 

 

 

 继承示例:

class Person{ //父类

  String name;

  int age;

  String address;

  void eat(){}

  void sleep(){}

}

class Student extends Person{ //子类

  String className;

  void study(){}

}

class Teacher extends Person{ //子类

  double salary;

  void teach(){}

}

class Doctor extends Person{ //子类

  String level;

  void cut(){}

}

 

 

我是初学者,如有更新不好的,欢迎这位大神指出,谢谢大家!

更多精彩以后更新,转载注明!

posted @ 2017-08-10 21:59  Einsam  阅读(780)  评论(0编辑  收藏  举报