码迷,mamicode.com
首页 > 编程语言 > 详细

Java学习二(面向对象)day03

时间:2018-03-21 23:18:52      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:有一个   ++   java学习   for   构造方法   类继承   对象   分享图片   代码   

技术分享图片day03

1.引用类型数组
  1)先声明后赋值
    Student[] stus = new Studenr[3];
      stus[0] = new Student(“zhangsan”,25,”LF”);
      stus[1] = new Student(“lisi”,26,”JMS”);
      stus[2] = new Student(“wangwu”,27,”SD”);
      stus[1].age = 36; //给 stus中第2个元素的年龄赋值为36
  2)声明的同时赋值
    Student[] stus = new Studenr[]{
      new Student(“zhangsan”,25,”LF”),
      new Student(“lisi”,26,”JMS”),
      new Student(“wangwu”,27,”SD”)
    };
  3)int[]型的数组------------数组的数组
    int[][] arr = new int[3][];
    arr[0] = new int[2];
    arr[0] = new int[2];
    arr[0] = new int[2];
    a[1][0] = 100; // 给 arr中第2个元素的第1个元素赋值为100
  4)遍历数组的数组
    int[][] arr = new int[3][4];
    for(int i = 0;i<arr.length;i++){
      for(j = 0;j<arr[i].length;j++){
        a[i][j] = 100;
      }
    }

技术分享图片技术分享图片

 

 

//引用类型数组测试类
public class StuTest {
    public static void main(String[] args) {
        Stu[] stus = new Stu[3]; //创建Stu数组对象
        stus[0] = new Stu("zhangsan",25,"LF"); //创建Stu对象
        stus[1] = new Stu("lisi",26,"JMS");
        stus[2] = new Stu("wangwu",28,"SD");
        System.out.println(stus[1].age); //26
        for(int i=0;i<stus.length;i++){
            System.out.println(stus[i].name);
            stus[i].sayHi();
        }
        
        Stu[] ss = new Stu[]{
            new Stu("zhangsan",25,"LF"),
            new Stu("lisi",26,"JMS"),
            new Stu("wangwu",28,"SD")
        };
        
        int[][] arr = new int[3][];
        arr[0] = new int[2];
        arr[1] = new int[3];
        arr[2] = new int[2];
        arr[1][0] = 100;
        
        int[][] as = new int[3][4];
        for(int i=0;i<as.length;i++){
            for(int j=0;j<as[i].length;j++){
                as[i][j] = 100;
            }
        }
        
    }
}

 

2.继承
  1)作用:代码复用
  2)通过extends来实现继承
  3)超类/父类:所有派生类所共有的属性和行为
    派生类/子类:派生类所特有的属性和行为
  4)派生类继承超类后,派生类具有:超类的+派生类的
  5)一个超类可以有多个派生类
    一个派生类只能有一个超类 -----------单一继承
  6)继承具备传递性
  7)java规定:构造派生类之前必须先构造超类
    在派生类的构造方法中若没有调用超类的构造方法
    -----则默认super()调用超类的无参构造方法
    在派生类的构造方法中若调用超类的构造方法
    -----则不再默认提供
    super()调用超类构造方法必须放在最上面

3.super:指代当前对象的超类对象
  super的用法:
  1)super.成员变量名 ------------------访问超类的成员变量
  2)super .方法名 ------------------------调用超类的方法
  3)super() --------------------------------调用超类的构造方法

//super的演示
public class SuperDemo {
    public static void main(String[] args) {
        Boo o = new Boo();
    }
}


class Coo{
    Coo(int a){
    }
}
class Doo extends Coo{
    Doo(){
        super(5);
    }
    /*
    //如下代码,写不写都有
    Doo(){
        super();
    }
    */
}

class Aoo{
    Aoo(){
        System.out.println("超类的构造方法");
    }
}
class Boo extends Aoo{
    Boo(){
        //super(); //默认的(调用超类的构造方法)
        System.out.println("派生类的构造方法");
    }
}

 

Java学习二(面向对象)day03

标签:有一个   ++   java学习   for   构造方法   类继承   对象   分享图片   代码   

原文地址:https://www.cnblogs.com/lj-6112/p/8620260.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!