一、Java基础语言

1)变量申明

复制代码
public class TestVar {
    public static void main(String[] args) {
        // 变量的声明
        int age;
        // 变量的赋值
        age = 18;
        int e,f = 30; // e没有赋值。f 赋值为 30;
        // 变量的使用
        System.out.println(age);
    }
}
复制代码

2)基本数据类型

复制代码
public class TestType {
    public  static void main(String[] args) {
        // 基本数据类型
        byte a = 10; // 表数范围:-128~127
        short b = 10; // 表数范围 正负3万
        int c = 10; // 表数范围;正负21亿
        long d = 10; // 很大
        long dL = 12345678910L; // 很大

        // 浮点类型
        float e = 3.14f; // 如果用float类型标识一个小数,后面必须加上f
        double f = 3.14;

        // 字符型
        char g = 'a'; // 单引号引起来的单个字符
        System.out.println("nihao...");

        // 布尔类型
        boolean flag = true;
    }
}
复制代码

3.1)常规算术运算

复制代码
public class TestOpe {
    public static void main(String[] args) {
        // 算术运算符
        // 加工;作用 1: 表示正数, 2表示相加操作,字符串拼接
        System.out.println(+10);
        System.out.println(5+6);
        System.out.println(5+6 + "abc");
        System.out.println("abc"+5+6);
        // 只要+左右两侧任意一侧是字符串,结果就一定是字符串

        // ++自增
        int a = 5;
        a++;
        ++a;
        System.out.println(a);

        a = 5;
        int m = a++ +7;  // 如果++在变量的后面,先运算后加1;m=a+7, a=a+1
        System.out.println(a); // 6
        System.out.println(m); // 12

        a = 5;
        int n = ++a + 7;  // 如果++在变量的前面,先运算先加1;a=a+1,m=a+7
        System.out.println(a); // 6
        System.out.println(n); // 13
    }
}
复制代码

3.2)常规运算符

复制代码
public class TestOpe2 {
    public static  void main(String[] args) {
        // 赋值运算符
        int num1 = 10;
        int num2 = 20;

        int sum = 0;
        sum += num1;
        System.out.println(sum);
        sum = sum + num2;
        System.out.println(sum);
    }
}
复制代码

3.3)关系运算符

复制代码
public class TestOpe3 {
    public static void main(String[] args) {
        // 关系运算符
        System.out.println( 5 == 6);
        // 逻辑运算符
        System.out.println(true && false);
        System.out.println(false && false);
        System.out.println(false || false);
        System.out.println(true || false);
    }
}
复制代码

 4.1)if 条件判断

复制代码
public class Testif01 {
    public static void main(String[] args) {
        // 给定一个数
        int num = 13;
        if (num >10 ) {
            System.out.println("大于10的数字 " + num);
        }
    }
}
复制代码

4.2)if else

复制代码
public class Testif02 {
    public static void main(String[] args) {
        // 给定一个数
        int num = 13;
        if (num >15 ) {
            System.out.println("大于15的数字 " + num);
        } else {
            System.out.println("小于15的数字 " + num);
        }
    }
}
复制代码

4.3) 多分支if

复制代码
public class Testif03 {
    public static void main(String[] args) {
        // 给定一个数
        int score = 68;
        if (score > 90) {
            System.out.println("A");
        } else if (score > 80) {
            System.out.println("B");
        } else {
            System.out.println("C");
        }
    }
}
复制代码

5)while and for 循环

复制代码
public class TestWhile {
    public static  void main(String[] args) {

        int num = 1;
        int sum = 0;
        while (num < 6) {
            sum += num;
            num++;
        }
        System.out.println(num);
        System.out.println(sum);

        sum = 0;
        for (num = 1;num < 6;num++) {
            sum += num;
        }
        System.out.println(num);
        System.out.println(sum);
    }
}
复制代码

6)数组使用

复制代码
public class testArray {
    public static void main(String[] args) {
       // 数组声明,以 int 类型数组为案例
        int[] arr; // 定义一个int类型的数组,名字是arr
        // 数组的创建
        arr = new int[4];  // 在创建的时候要给定数组的长度,创建一个长度为4的int类型的数组,默认值为 0
        arr[0] = 15;
        arr[1] = 16;
        arr[2] = 17;
        arr[3] = 18;

        for (int i = 0; i <= 3;i++) {
            System.out.println(arr[i]);
        }

        // 增强for循环
        for (int num:arr) {
            System.out.println(num);
        }
    }
}
复制代码

7)方法调用(重载)

复制代码
public class TestMethod {
    public static  int addNum(int num1,int num2) {
        return num1 + num2;
    }

    public static void  addNum2(int num1,int num2) {
        System.out.println(num1 + num2);
    }
    public static void  addNum(int num1,int num2,int num3) {
        System.out.println(num1 + num2 + num2);
    }

    public static void main(String[] args) {
        int a = addNum(10,20);
        System.out.println(a);
    }

    /*
    *  public static 方法的修饰符
    * int 方法的返回值类型,方法的返回值对应的数据类型
    * void 没有返回值
    * int num1,int num2  形式参数
    * 方法的重载,只好方法名,形参列表有关;方法名相同,形成列表必须不同(类型不同,顺序不同,个数不同)
    * */
}
复制代码

 二、面向对象的使用

1)类的编写(以及显示构造器的使用)

复制代码
public class Person {
    // 特性-属性-名词(只定义和业务逻辑相关的代码)
    String name;
    int age;
    double height;

    // 行为-方法-动词(只定义和业务逻辑相关的代码)
    public void  study() {
        System.out.println("学习: "+name);
    }

    // 显示编写的空构造器
    public Person() {
        System.out.println("调用空构造器1");
    }


    public Person(String name,int age,double height) {
        System.out.println("调用空构造器2");
        this.name = name;
        this.age = age;
        this.height = height;
    }
}
复制代码

测试使用类以及构造器

复制代码
public class Test {
    public static void main(String[] args) {
        // 创建一个Person 类
        Person p1 = new Person(); //  调用空构造器,进行初始化操作,将对象的地址返回给p1
        p1.name = "张三";
        p1.age = 23;
        p1.height = 180.5;
        p1.study();

        Person p2 = new Person("lisi",1,170.5); //  调用构造器,
        p2.study();
    }
}
复制代码

2)私有属性构造读取方法

复制代码
public class Girl {
    private int age;

    // 给age提供赋值方法
    public void setAge(int age) {
        if (age > 30) {
            this.age = 18;
        } else {
            this.age = age;
        }
    }

    // 给age提供读取方法
    public int getAge() {
        return age;
    }
}
复制代码

测试使用

public class Test {

    public static void main(String[] args) {
        Girl g = new Girl();
        g.setAge(33);
        System.out.println(g.getAge());  // 18
    }
}

3)类的继承(并重写父类方法)

先定义父类Person类

复制代码
public class Person {
    private int age;
    private String  name;
    private double height;

    public void setAge(int age) {
        this.age = age;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public double getHeight() {
        return height;
    }

    public String getName() {
        return name;
    }

    public void eat() {
        System.out.println("吃");
    }

    public void sleep() {
        System.out.println("睡");
    }

    public void shout() {
        System.out.println("叫");
    }
}
View Code
复制代码

再定义子类Student类型,重写父类方法

复制代码
public class Student  extends Person{
    private int sno;

    public void setSno(int sno) {
        this.sno = sno;
    }

    public int getSno() {
        return sno;
    }

    public void study() {
        System.out.println("学习");
    }

    public void shout() {
        System.out.println("学生叫");
    }
}
复制代码

测试

public class Test {
    public static void main(String[] args) {
        Student s = new Student();
        s.setSno(10010);
        s.setAge(20);
        s.shout();  // 调用子类方法
    }
}

三、多态的使用

1)定义基础类 Animal

public class Animal {
    public void shout() {
        System.out.println("动物叫");
    }
}

2)定义子类 Cat 和 Dog 继承 Animal

public class Cat extends Animal{
    public void shout() {
        System.out.println("猫叫");
    }
    public void scratch() {
        System.out.println("猫饶人");
    }
}

Dog类

复制代码
public class Dog extends Animal {
    public void shout() {
        System.out.println("狗叫");
    }

    public void guard() {
        System.out.println("咬人");
    }
}
复制代码

3)多态的使用,定义 Girl 类传 Animal 累的方法,但调用的是子类的内容

public class Girl {

    public void play(Animal an) {
        an.shout();
    }
}

4)测试用例

复制代码
public class Test {
    public static void main(String[] args) {
        //创建女孩的实例对象
        Girl g = new Girl();
        // 创建猫的实例对象
        Animal an;
        Cat c = new Cat();
        an = c;
        g.play(an);
    }
}
复制代码

四、异常

1)异常处理

复制代码
public class Test {
    public static void main(String[] args) {
        try {
            int num1 = 12;
            int num2 = 0;
            System.out.println(num1/num2);
        } catch (Exception ex) {
            System.out.println("程序出错: "+ex);
        }

        try {
            int num1 = 12;
            int num2 = 0;
            System.out.println(num1/num2);
        } catch (InputMismatchException ex) {
            System.out.println("程序出错: "+ex);
        } finally {
            System.out.println("无论是否异常都执行");
        }
    }
}
复制代码

2)自定义异常

复制代码
public class Test02 {

    public static void main(String[] args) {
        devide();
        try {
            devide2();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public static void devide() {
        int num1 = 12;
        int num2 = 0;
        if (num2 == 0) {
            // 人为制造异常
            try {
                throw new Exception();
            } catch (Exception e) {
                System.out.println("自己处理异常");
            }
        } else {
            System.out.println("两个数的商是: " + num1/num2);
        }
    }

    public static void devide2() throws Exception {
        // 向外抛出异常
        int num1 = 12;
        int num2 = 0;
        if (num2 == 0) {
            // 人为制造异常
            throw new Exception();
        } else {
            System.out.println("两个数的商是: " + num1/num2);
        }
    }
}
复制代码

 五、数组使用

数组一旦指定了长度,就不可以更改
删除,增加元素效率低
数组中实际元素的数量是没有办法获取的,没有提供对应的方法或者属性来获取
数组存储:有效,可重复

代码演示

复制代码
public class Test {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("ddd");
        System.out.println(list);

        // 删除元素
        list.remove("bbb");
        // 修改元素
        list.set(0,"hhh");

        System.out.println(list.get(0));

        for (int i = 0; i <= list.size() -1; i++) {
            System.out.println(list.get(i));
        }
    }
}
复制代码

 

posted on   可口_可乐  阅读(8)  评论(0编辑  收藏  举报
努力加载评论中...

点击右上角即可分享
微信分享提示