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

javase部分3

时间:2018-08-15 17:44:48      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:数据   []   override   自动   people   内容   重写   数列   oid   

3.1函数

  修饰符 返回值类型 函数名(参数类型 形式参数 1,参数类型 形式参数2...){

  执行语句;

  return 返回值;

}

void 空 返回值类型

 

3.2重载与重写

重载:同一个类中,两个或多个函数名相同,返回值类型可以不相同,但参数列表不同(参数个数,参数类型)   与变量名无关

public class Overload {

    /**
     * @param args
     */
    public static void main(String[] args) {
        add(1, 2);
        add(0.1, 2.1, 3.1);

    }

    public static int add(int x, int y) {
        System.out.println(x + y);
        return x + y;
    }

    public static int add(double x, double y, double z) {
        System.out.println((int) (x + y + z));//为了达到效果强制转换成int类型
        return (int) (x + y + z);//返回int类型参数
    }
}
//结果 为3 5

重写:子类继承父类方法,可重写父类方法。在接口中实现接口必须实现接口方法 只有方法体不同

                class People {
    public void eat() {
        System.out.println("people 吃饭!");
    }

    public void say() {
        System.out.println("people 说话!");
    }
}

class Children extends People {
    public void eat() {
        System.out.println("Children 吃饭!");// 重写父类方法
    }

    public void say() {
        System.out.println("Children 说话!");
    }
}

public class Override {

    public static void main(String[] args) {
        Children cd = new Children();
        cd.eat();
        cd.say();
        People pp = new People();
        pp.eat();
        pp.say();
    }

}
//结果为
            Children 吃饭!
            Children 说话!
            people 吃饭!
            people 说话!
                

3.3 数组(引用数据类型中的一种)

    数组:存储同一种类型数据的集合       内存:栈空间:数据使用完毕,会自动释放(存储局部变量)   堆空间:存储对象,new出来的东西  是一块内存空间 

       eg: int [] x=new int[3]; 首先先在堆空间申请一块内存 ,内存被划分为3块存储默认数据,再在空间申请一块名为x的内存,存储堆空间中的内存地址

       当堆内存中的实体与栈内存中变量没有联系时会自动回收内存

    数组初始化:1.静态初始化数组:int[] a={ 1,2,3,4}   缺点  数组的内容已经明确

          2.动态初始化数组:int[] a=new int[4];   a[1]=1;... 

    数组遍历:通过属性length for循环打印数组

    求最值:冒泡法

    排序:选择排序 (一与多比较 卡住游标求最小值) 冒泡排序(相邻比,多轮后)

    查找:二分法  

    拓展:二维数组 是一维数组中存放的数据为一维数组           定义的话也分静态与动态          遍历的话也是a.length,a[i].length 

class ArraysEg {
/*
 * 选择排序
 */
    public void sort(int[] a) {
        
        for (int x = 0; x < a.length - 1; x++) {//控制比较的次数 
            for (int y = x + 1; y < a.length; y++) {//控制和那些比较
                int temp = 0;
                if (a[x] > a[y]) {//将当前游标位置和其他位置比较求出最小值
                    temp = a[x];
                    a[x] = a[y];
                    a[y] = temp;
                }
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "\t");
        }
    }

}

public class ArraysText {
    public static void main(String args[]) {
        int[] a = { 2, 1, 5, 7, 3 };
        ArraysEg ae = new ArraysEg();
        ae.sort(a);
    }

}
//结果 1    2    3    5    7    
/*
 *冒泡排序
 */
class bubble {
    public void sort(int[] a) {
        for (int x = 0; x < a.length - 1; x++) {// 控制比较次数
            for (int y = 0; y < a.length - 1 - x; y++) {// 每一轮冒泡,都会少一次比较
                if (a[y] > a[y + 1]) {
                    int temp = 0;
                    temp = a[y];
                    a[y] = a[y + 1];
                    a[y + 1] = temp;
                }
            }
        }
    }

}

public class ArrayText2 {
    public static void main(String args[]) {
        int[] a = { 4, 1, 5, 7, 3 };
        ArraysEg ae = new ArraysEg();
        ae.sort(a);
    }
}
//结果: 1    3    4    5    7

  注:以上内容是博主觉得比较关键的地方,仅供有一定编程基础的朋友参考,欢迎纠错。

javase部分3

标签:数据   []   override   自动   people   内容   重写   数列   oid   

原文地址:https://www.cnblogs.com/mayprayer/p/9482670.html

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