JAVA学习笔记——基础语法

1 .类型转换

1.1 强制转换

(类名)变量名 高-->低

1 public static void main(String[] args) {
2         int i=100;
3         byte j=(byte)i;
4         System.out.println(j);

1.2 自动转换 

低-->高

1 public static void main(String[] args) {
2         int i=100;
3         double j=i;
4         System.out.println(i);
5         System.out.println(j);

常量要加 final

2.命名规范

3.包机制

4.Scanner对象

  Scanner类来获取用户的输入

  基本语法:

   1 Scanner scanner = new Scanner(System.in); 

  通过Scanner类的next()与nextLine()方法来获取输入的字符串,在读取之前一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。

  next()方法: 

 1 package Study.Scanner;
 2 
 3 import java.util.Scanner;
 4 
 5 public class demo1 {
 6     public static void main(String[] args) {
 7         //创建一个扫描器对象,用来接收键盘数据
 8         Scanner scanner = new Scanner(System.in);
 9 
10         System.out.println("使用next方法接收:");
11 
12         //判断用户有没有使用字符串
13         if(scanner.hasNext()){
14             //使用next方式接收
15             String str = scanner.next();
16             System.out.println("输入的内容为:"+ str);
17         }
18         //属于IO流的类会一直占用资源,用完就关掉
19         scanner.close();
20 
21 
22     }
23 }

  nextLine()方法:

 1 package Study.Scanner;
 2 
 3 import java.util.Scanner;
 4 
 5 public class demo2 {
 6     public static void main(String[] args) {
 7         Scanner scanner = new Scanner(System.in);
 8         System.out.println("使用nextLine方法接收:");
 9 
10         if (scanner.hasNextLine()){
11             String str=scanner.nextLine();
12             System.out.println("输入的内容为"+ str);
13         }
14 
15         scanner.close();
16     }
17 }

5.方法

5.1 方法定义

  方法是语句的集合,一起执行一个功能。

  设计方法的原则:

          保持方法的原子性,就是一个方法只完成一个功能。

 1 package Study.method;
 2 
 3 public class demo1 {
 4     public static void main(String[] args) {
 5         int sum = add(8, 9);
 6         System.out.println(sum);
 7     }
 8     
 9     //加法
10     public static int add(int a, int b) {
11         return a + b;
12     }
13 }

  方法格式:

        修饰符 返回值类型 方法名(参数类型 参数){

          ...

          方法体

          return 返回值  

        }

package Study.method;

public class demo2 {
    public static void main(String[] args) {
        int num = compare(5, 5);
        System.out.println(num);
    }

    //比大小
    public static int compare(int a, int b) {
        int result = 1;
        if (a > b) {
            result = a;
        }else if (a == b) {
            System.out.println("两数相等");
            return 0;
        } else {
            result = b;
        }
        return result;
    }
}

5.2 方法的调用

  调用方法:对象名.方法名(实参列表)

       当方法返回一个值得时候,方法调用通常被当作一个值

       如果方法返回值是void,方法调用是一条语句

5.3 方法的重载

  重载就是在一个类中,有相同的函数名称,但形参不同的函数

  规则:

    方法名称必须相同

    参数列表必须不同

    仅仅返回类型不同不足以构成方法的重载

 1 package Study.method;
 2 
 3 public class demo2 {
 4     public static void main(String[] args) {
 5         int num = compare(5, 5);
 6         double num=compare(1,5);
 7         System.out.println(num);
 8     }
 9 
10     //比大小
11     public static int compare(int a, int b) {
12         int result = 1;
13         if (a > b) {
14             result = a;
15         }else if (a == b) {
16             System.out.println("两数相等");
17             return 0;
18         } else {
19             result = b;
20         }
21         return result;
22     }
    //比大小
23 public static double compare(double a, double b) { 24 double result = 1; 25 if (a > b) { 26 result = a; 27 }else if (a == b) { 28 System.out.println("两数相等"); 29 return 0; 30 } else { 31 result = b; 32 } 33 return result; 34 } 35 }

5.4 可变参数

  Java支持传递同类型的可变参数给一个方法。

  在方法声明中,在指定参数类型后加省略号。

  一个方法中只能指定一个可变参数,必须是方法的最后一个参数,任何普通的参数必须在他之前声明。

 1 package Study.method;
 2 
 3 public class demo3 {
 4     public static void main(String[] args) {
 5         demo3 demo3=new demo3();
 6         demo3.test(1,2,5);
 7     }
 8     public static void test(int...i){
 9         System.out.println(i[0]);
10     }
11 }

5.5 递归

  递归就是:方法自己调用自己

  递归结构包括两个部分:

            1)递归头:什么时候不调用自身方法,如果没有头会陷入2死循环。

            2)递归体:什么时候需要调用自身方法。

 1 package Study.method;
 2 
 3 public class demo4 {
 4     public static void main(String[] args) {
 5         System.out.println(f(30));
 6     }
 7     public static int f(int n){
 8         if(n==1){
 9             return 1;
10         }else{
11             return n*f(n-1);
12         }
13     }
14 }

6.数组

6.1 数组的创建

 1 package Study.array;
 2 
 3 public class Demo1 {
 4     public static void main(String[] args) {
 5         //动态初始化
 6         int[] nums = new int[10];
 7         nums[0]=0;
 8         nums[1]=1;
 9         nums[2]=2;
10         nums[3]=3;
11         nums[4]=4;
12         nums[5]=5;
13         nums[6]=6;
14         nums[7]=7;
15         nums[8]=8;
16         nums[9]=9;
17 
18         //静态初始化
19         int[] a={1,2,3,4,5,6,7,8,9};
20     }
21 }

6.2 数组的使用

 1 package Study.array;
 2 
 3 public class demo2 {
 4     public static void main(String[] args) {
 5         int[] arrays = {1, 2, 3, 4, 5, 6, 7, 8, 98};
 6 
 7         //打印数组元素
 8         for (int i = 0; i < arrays.length; i++) {
 9             System.out.println(arrays[i]);
10         }
11 
12         System.out.println("===========");
13 
14         //计算所有元素的和
15         int sum = 0;
16         for (int i = 0; i < arrays.length; i++) {
17             sum += arrays[i];
18         }
19         System.out.println("和为:" + sum);
20 
21         System.out.println("===========");
22 
23         //查找最大元素
24         int max = arrays[0];
25         for (int i = 0; i < arrays.length; i++) {
26             if (arrays[i] > max) {
27                 max = arrays[i];
28             }
29         }
30         System.out.println("最大值为" + max);
31 
32     }
33 }

 

 1 package Study.array;
 2 
 3 public class demo3 {
 4     public static void main(String[] args) {
 5         int[] arrays = {1, 5, 79, 6, 4};
 6         printArrays(arrays);
 7         System.out.println("======");
 8         int[] rev = reverse(arrays);
 9         printArrays(rev);
10     }
11 
12     //打印数组元素
13     public static void printArrays(int[] array) {
14         for (int i = 0; i < array.length; i++) {
15             System.out.println(array[i]);
16         }
17     }
18 
19     //反转数组元素
20     public static int[] reverse(int[] array) {
21         int[] rev = new int[array.length];
22         for (int i = 0, j = rev.length - 1; i < array.length; i++, j--) {
23             rev[j] = array[i];
24         }
25         return rev;
26     }
27 }

6.3 二维数组

 1 package Study.array;
 2 
 3 public class array4 {
 4     public static void main(String[] args) {
 5         int[][] arrays={{1,2},{2,3}};
 6         /* System.out.println(arrays[0][0]);
 7         System.out.println(arrays[1][1]);*/
 8 
 9         for (int i = 0; i < arrays.length; i++) {
10             for (int j = 0; j < arrays[i].length; j++) {
11                 System.out.println(arrays[i][j]);
12             }
13         }
14     }
15 }

6.4 Arrays类

 1 package Study.array;
 2 
 3 import java.util.Arrays;
 4 
 5 public class demo5 {
 6     public static void main(String[] args) {
 7         int[] a = {4, 88, 5664, 52, 7, 3, 5688, 63};
 8 
 9         //打印数组元素Array.tostring()
10         System.out.println(Arrays.toString(a));
11         System.out.println("=======");
12 
13         //数组元素排序:升序
14         Arrays.sort(a);
15         System.out.println(Arrays.toString(a));
16 
17         //数组填充
18         Arrays.fill(a, 0);
19         System.out.println(Arrays.toString(a));
20     }
21 }

  冒泡排序:

  

 1 package Study.array;
 2 
 3 import java.util.Arrays;
 4 
 5 public class demo6 {
 6     public static void main(String[] args) {
 7         int[] nums = {145, 54, 72, 92, 4, 65, 16, 46, 46554, 45, 4, 2};
 8         int[] num = sort(nums);
 9         System.out.println(Arrays.toString(num));
10 
11     }
12 
13     public static int[] sort(int[] array) {
14         int temp = 0;
15         //外层循环,判断要交换多少次
16         for (int i = 0; i < array.length - 1; i++) {
17             //内层循环,比较相邻两个数大小
18             for (int j = 0; j < array.length - 1; j++) {
19                 if (array[j + 1] < array[j]) {
20                     temp = array[j];
21                     array[j] = array[j + 1];
22                     array[j + 1] = temp;
23                 }
24             }
25         }
26         return array;
27     }
28 }

 

7.面向对象编程

7.1 类与对象的创建

 1 package oop.demo1;
 2 
 3 public class Application {
 4     public static void main(String[] args) {
 5         //类:抽象的,需要实例化
 6         //类实例化后会返回一个自己的对象
 7 
 8         Student student1 = new Student();
 9 
10         student1.name = "李华";
11         student1.age = 15;
12 
13         student1.study();
14 
15     }
16 }
17 
18 
19 
20 package oop.demo1;
21 
22 //学生类
23 public class Student {
24     //属性:字段
25     String name;
26     int age;
27 
28     //方法
29     public void study() {
30         System.out.println(this.name + "在学习");
31     }
32 }

7.2 构造器

  构造器:

     1.和类名相同

     2.没有返回值

  作用:

    1.new本质在调用构造方法

    2.初始化对象值

  注意:

    1.定义有参构造之后,如果想使用无参构造,需要显示的定义一个无参构造

    2.快捷键:alt+insert

 1 package oop.demo2;
 2 
 3 public class Application {
 4     public static void main(String[] args) {
 5         //无参
 6         Person person = new Person();
 7         //有参
 8         Person person1 = new Person("李四");
 9         
10         System.out.println(person.name);
11         System.out.println(person1.name);
12     }
13 }
14 
15 
16 
17 
18 package oop.demo2;
19 
20 //Person类
21 public class Person {
22     String name;
23 
24     //实例化初始值
25     public Person() {
26         this.name="张三";
27     }
28 
29     //有参构造
30     public Person(String name) {
31         this.name = name;
32     }
33 }

7.3 封装

  属性私有 get/set

  封装意义:

      1.提高程序的安全性,保护数据

      2.隐藏代码的实现细节

      3.统一接口

      4.增加系统的可维护性

 1 package oop.demo3;
 2 
 3 public class Application {
 4     public static void main(String[] args) {
 5         Student S1 = new Student();
 6         S1.setName("张三");
 7         S1.setId(123);
 8         S1.setSex('男');
 9         S1.setAge(855);
10 
11         System.out.println(S1.getName());
12         System.out.println(S1.getAge());
13 
14 
15     }
16 }
17 
18 
19 
20 package oop.demo3;
21 
22 //学生类
23 public class Student {
24     private String name;
25     private int age;
26     private int id;
27     private char sex;
28 
29     //alt+insert快捷键
30 
31     public String getName() {
32         return name;
33     }
34 
35     //set给这个数据设置值
36     public void setName(String name) {
37         this.name = name;
38     }
39 
40     //get获取数据的值
41     public int getAge() {
42         return age;
43     }
44 
45     public void setAge(int age) {
46         if (age <= 120 || age < 0) {
47             this.age = age;
48         } else {
49             System.out.println("年龄错误");
50         }
51     }
52 
53     public int getId() {
54         return id;
55     }
56 
57     public void setId(int id) {
58         this.id = id;
59     }
60 
61     public char getSex() {
62         return sex;
63     }
64 
65     public void setSex(char sex) {
66         this.sex = sex;
67     }
68 }

 7.4 继承

  7.4.1 extends

 1 package oop.demo4;
 2 
 3 public class Application {
 4     public static void main(String[] args) {
 5         Student s1 = new Student();
 6         s1.say();
 7         System.out.println(s1.money);
 8 
 9     }
10 }
11 
12 
13 package oop.demo4;
14 
15 public class Person {
16     public int money = 100;
17 
18     public void say() {
19         System.out.println("说了一句话");
20     }
21 }
22 
23 
24 
25 package oop.demo4;
26 
27 //子类继承了父类,就会拥有父类所有的方法
28 public class Student extends Person{
29 }

  7.4.2 super

    注意:

      1.super调用父类的构造方法,必须在构造方法的第一个

      2.super只能出现在子类的方法或者构造方法中

      3.super和this不能同时调用构造方法

 1 package oop.demo4;
 2 
 3 
 4 public class Application {
 5     public static void main(String[] args) {
 6         Student student = new Student();
 7         student.test("xname");
 8         System.out.println("========================");
 9         student.test2();
10 
11     }
12 }
13 
14 
15 
16 
17 package oop.demo4;
18 
19 public class Person {
20     public String name="pname";
21 
22     public void print(){
23         System.out.println("pp");
24     }
25 }
26 
27 
28 
29 
30 package oop.demo4;
31 
32 //子类继承了父类,就会拥有父类所有的方法
33 public class Student extends Person {
34     public String name = "sname";
35 
36     public void test(String name) {
37         System.out.println(name);
38         System.out.println(this.name);
39         System.out.println(super.name);
40     }
41 
42     public void print() {
43         System.out.println("sp");
44     }
45 
46     public void test2() {
47         print();
48         this.print();
49         super.print();
50     }
51 }

  7.4.3 方法的重写

    为什么要重写:

          1.父类的功能子类不一定需要,或者不一定满足

          alt+insert:override

 1 package oop.demo05;
 2 
 3 public class Application {
 4     public static void main(String[] args) {
 5         //静态方法
 6         //方法的调用只和左边,定义的数据类型有关
 7         A a = new A();
 8         a.test();
 9         //父类的引用指向了子类
10         B b = new A();
11         b.test();
12 
13         //非静态方法
14         A a1 = new A();
15         a1.test1();
16 
17         B b1 = new A();//子类重写了父类的方法
18         b1.test1();
19 
20     }
21 }
22 
23 
24 
25 
26 
27 
28 package oop.demo05;
29 
30 public class B {
31 
32     public static void test() {
33         System.out.println("B==>test");
34     }
35 
36     public void test1(){
37         System.out.println("B==>test1");
38     }
39 }
40 
41 
42 
43 
44 
45 package oop.demo05;
46 
47 public class A extends B{
48     public static void test(){
49         System.out.println("A==>test");
50     }
51 
52     @Override //重写子类的方法必须和父类的一致,方法体不同
53     public void test1() {
54         System.out.println("A==>test1");
55     }
56 }

7.5 多态

  7.5.1 多态注意事项:  

        1.多态是方法的多态,属性没有

        2.父类和子类有联系

        3.存在条件:继承关系,方法重写,父类引用指向子类对象

 1 package oop.demo6;
 2 
 3 public class Application {
 4     public static void main(String[] args) {
 5 
 6         //一个对象的实际类型是确定的
 7         //new Student();
 8         //new Person();
 9 
10         //可以指向的引用类型就不确定了:父类的引用指向了子类
11 
12         //Student能调用的方法都是自己的或者继承父类的
13         Student s1 = new Student();
14         //Person 父类型,可以指向子类,但不能调用子类独有的方法
15         Person s2 = new Student();
16         Object s3 = new Student();
17 
18         //对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
19         s1.run();
20         s2.run();
21         s1.eat();
22         
23 
24     }
25 }
26 
27 
28 
29 package oop.demo6;
30 
31 public class Person {
32     public void run(){
33         System.out.println("RUNP");
34     }
35 }
36 
37 
38 
39 package oop.demo6;
40 
41 import javax.swing.plaf.PanelUI;
42 
43 public class Student extends Person {
44 
45 
46     @Override
47     public void run() {
48         System.out.println("RUNS");
49     }
50 
51     public void eat(){
52         System.out.println("EAT");
53     }
54 }

 

  7.5.2 instandof和类型转换

  

 1 package oop.demo7;
 2 
 3 import org.omg.CORBA.Object;
 4 
 5 public class Application {
 6     public static void main(String[] args) {
 7         java.lang.Object object = new Student();
 8 
 9         System.out.println(object instanceof Student);//程序能否编译成功,是看是否存在继承关系
10         System.out.println(object instanceof Person);
11         System.out.println(object instanceof Object);
12         System.out.println(object instanceof Teacher);
13         System.out.println("============");
14 
15         Person person = new Student();
16         System.out.println(person instanceof Student);
17         System.out.println(person instanceof Person);
18         System.out.println(person instanceof Object);
19         System.out.println(person instanceof Teacher);
20 
21 
22     }
23 }
24 
25 
26 
27 
28 
29 package oop.demo7;
30 
31 public class Person {
32 }
33 
34 package oop.demo7;
35 
36 public class Student extends Person{
37 }
38 
39 
40 package oop.demo7;
41 
42 public class Teacher extends Person{
43 }

  多态总结:

  1.父类引用指向子类的对象

  2.把子类转换为父类:向上转型

  3.把父类转换为子类:向下转型:强制装换

 

7.6 抽象类

 1 package oop.demo8;
 2 //抽象类
 3 public abstract class Action {
 4 
 5     //约束,有人帮我们实现
 6     //抽象方法,只有方法的名字,没有方法的实现
 7     public abstract void doSomething();
 8 }
 9 
10 
11 
12 
13 package oop.demo8;
14 
15 //子类必须实现抽象父类的所有方法,除非子类也是抽象类
16 public class A extends Action {
17     @Override
18     public void doSomething() {
19 
20     }
21 }

 

7.7 接口

  接口:只有规范,自己无法写方法~专业的约束 关键字:interface

  作用:

    1.约束

    2.定义一些方法,让不同的人实现

    

 1 package oop.demo9;
 2 
 3 
 4 public interface UserService {
 5     //接口中所有的定义都是抽象的,默认public abstract
 6 
 7     void add(String name);
 8     void delete(String name);
 9     void update(String name);
10     void query(String name);
11 }
12 
13 
14 
15 package oop.demo9;
16 
17 public interface TimeService {
18     void timer();
19 }
20 
21 
22 
23 package oop.demo9;
24 //接口的实现类 implement
25 //实现了接口的类,必须要重写接口的方法
26 
27 //多继承
28 public class UserServiceImpl implements UserService,TimeService{
29     @Override
30     public void add(String name) {
31 
32     }
33 
34     @Override
35     public void delete(String name) {
36 
37     }
38 
39     @Override
40     public void update(String name) {
41 
42     }
43 
44     @Override
45     public void query(String name) {
46 
47     }
48 
49     @Override
50     public void timer() {
51 
52     }
53 }

 

异常捕获快捷键:Ctrl+Alt+t 

  

posted @ 2022-03-18 21:42  King_X  阅读(42)  评论(0)    收藏  举报