Java基础

 

public class Demo07 {
    //类变量 static
    static double salary = 2500;
    //属性:变量
    //实例变量:从属于对象:如果不自行初始化,这个类型的默认值0 0.0
    //布尔值:默认是false
    //除了基本类型,其余的默认值都是null;
    String name;
    int age;
}
    //main方法
    public static void main(String[] args){
        //局部变量:必须声明和初始化值
        int i = 10;
        System.out.println(i);//i
        //变量类型 变量名字 = new com.kuang.base.Demo08();
        Demo07 demo07 = new Demo07();
        System.out.println(demo07.age);//0
        System.out.println(demo07.name);//null
        //类变量
        System.out.println(salary);//2500
    }

类型转换问题:

强制转换 (类型)变量名 高--低
自动转换 低--高
注意点:
1.不能对布尔值进行转换
2.不能把对象类型转换为不相干的类型
short c = 10;
byte d = 8;
System.out.println((String)(c+d));
/*
byte、short、char用运算符运算后自动转型为int类型,如果存在更高类型转为更高类型
报错:Inconvertible types; cannot cast 'int' to 'java.lang.String'
*/
3.在把高容量转换到低容量的时候,强制转换
4.转换的时候可能存在内存溢出,或者精度问题!

短路运算:

//字符串连接符 + String
int a = 10;
int b = 20;
System.out.println(""+a+b);//1020
System.out.println(a+b+"");//30

包机制:

import com.kuang.base.*;导入这个包下所有的类

Java Doc: Java Doc 是一种技术,能将注释生成一个注释文档

生成Doc文档:

1.通过命令行生成:

2.IDEA生成:

1、打开 idea,点击 Tools-> Generate Java Doc,这样会打开生成 java doc 文档的配置页面。

2、进行配置:

 Scanner类:

//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户有没有输入字符串
if(scanner.hasNext()){
    //使用next方式接收
    String str = scanner.next();
    System.out.println("输出的内容为:"+str);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
scanner.close();
//从键盘接收数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
//判断是否还有输入
if(scanner.hasNext()){
    String str = scanner.nextLine();
    System.out.println("输出的内容为:"+str);
}
scanner.close();

 顺序结构:

 case穿透现象:如果不加break

char grade = 'C';
switch(grade){
    case 'A':
        System.out.println("优秀");
    case 'B':
        System.out.println("良好");
    case 'C':
        System.out.println("及格");
    case 'D':
        System.out.println("再接再厉");
    case 'E':
        System.out.println("挂科");
    default:
        System.out.println("未知等级");
  /*
  	及格
	再接再厉
	挂科
	未知等级
  */

 增强for循环:

public static void main(String[] args) {
    int[] numbers = {10,20,30,40,50};//定义了一个数组
    for (int i = 0; i < 5; i++) {
        System.out.println(numbers[i]);
    }
    System.out.println("=================");
    //遍历数组的元素
    for(int x:numbers){
        System.out.println(x);
    }
}

 方法:

 方法重载:

命令行传参:

有时候你希望运行一个程序时候再传递给它消息。这要靠命令行参数给main()函数实现。

package com.kuang.method;

public class Demo03 {
    public static void main(String[] args) {
        //args.length数组长度
        for (int i = 0; i < args.length; i++) {
            System.out.println("args["+ i +"]:"+args[i]);
        }
    }
}

因为是包所以只需要cd到src路径。

可变参数:

public class Demo04 {
    public static void main(String[] args) {
        Demo04 demo04 = new Demo04();
        demo04.test(1,2,3,4,5);
    }
    public void test(int... i){
        System.out.println(i[0]);//1
        System.out.println(i[1]);//2
        System.out.println(i[2]);//3
        System.out.println(i[3]);//4
        System.out.println(i[4]);//5
    }
}

递归:

 数组声明创建:

 

 

多维数组:

 冒泡排序:

//冒泡排序
//1.比较数组中,两个相等的元素,如果第一个数比第二个数大,我们就交换他们的位置
//2.每一次比较,都会产生出一个最大,或者最小的数字;
//3.下一轮则可以少一次排序!
//4.依次排序,直到结束!
public static int[] sort(int[] array){
    //临时变量
    int temp = 0;
    //外出循环,判断我们这个要走多少次
    for (int i = 0; i < array.length - 1; i++) {
            boolean flag = false;//通过flag标识位减少没有意义的比较
            //内层循环,比较判断两个数,如果第一个数,比第二个数大,则交换位置
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j+1]>array[j]){
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                    flag = true;
                }
            }
            if(flag == false){
                break;
            }
        }
    return array;

面向对象: 

值传递和引用传递:

//值传递
public class Demo04 {
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a);//1
        change(a);
        System.out.println(a);//1
    }
    public static void change(int a){
        a = 10;
    }
}

//引用传递:对象,本质还是值传递
//对象内存
public class Demo05 {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);//null
        change(person);
        System.out.println(person.name);//狂神
    }
    public static void change(Person person){
        //person是一个对象:指向的是Person person = new Person();这是一个具体的人,可以改变属性!
        person.name = "狂神";
    }
}
//定义了一个Person类,有一个属性:name
class Person{
    String name;//null
}

构造器:

1.构造器名和类名相同

2.没有返回值,不能写void

3.一个类即使什么都不写,它也会存在一个方法,叫做构造器,可显示定义。

4.使用new关键字,本质是在调用构造器(自动运行构造器)

5.构造器用来初始化值

6.根据是否传入参数来判断是否程序走有参构造还是无参构造

public class Person {
    //一个类即使什么都不写,它也会存在一个方法
    //显示的定义构造器
    String name;
    //实例化初始值
    //1.使用new关键字,本质是在调用构造器
    //2.构造器用来初始化值
    //无参构造
    public Person(){
        this.name = "qinjiang";
    }
    //有参构造:一旦定义了有参构造,无参构造就必须显示定义
    public Person(String name){
        this.name = name;
    }
}
public class Application {
    public static void main(String[] args) {
        //new实例化一个对象
        Person person = new Person();
        System.out.println(person.name);//qinjiang
    }
}
public class Application {
    public static void main(String[] args) {
        //new实例化一个对象
        Person person = new Person("kuangshen");
        System.out.println(person.name);//kuangshen
    }
}

封装:

//类 private:私有
public class Student {
    //属性私有
    private String name;//名字
    private int age;//年龄
    //提供一些可以操作这个属性的方法!
    //提供一些public的get,set方法
    //get 获得这个数据
    public String getName(){
        return this.name;
    }
    public int getAge(){
        return this.age;
    }
    //set 给这个数据设置值
    public void setName(String name){
        this.name = name;
    }
    public void setAge(int age){
        if(age>120||age<0){
            this.age = 3;
        }else{
            this.age = age;
        }
    }
}
/*
    1.提高程序安全性,保护数据
    2.隐藏代码的实现细节
    3.统一接口
    4.系统可维护增加了
 */
public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.setName("秦疆");
        System.out.println(s1.getName());
        s1.setAge(70);//不合法的
        System.out.println(s1.getAge());
    }
}

继承:

public class Person /*extends Object*/{
    //public
    //protected
    //default
    //private
    public int money = 10_0000_0000;
    public void say(){
        System.out.println("说了一句话");
    }
}
//子类继承父类,就会拥有父类的全部方法!
public class Student extends Person{

}
public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.say();//说了一句话
        System.out.println(student.money);//1000000000

    }
}

ctrl+h继承树

在Java中,所有的类,都默认直接或者间接继承Object

Super:

public class Person /*extends Object*/{
    public Person(){
        System.out.println("Person无参构造器");
    }
    protected String name = "kuangshen";
    public void print(){
        System.out.println("Person");
    }
}
//子类继承父类,就会拥有父类的全部方法!
public class Student extends Person{
    public  Student(){
        //隐藏代码:默认调用了父类无参构造器
        //super()调用父类的构造器必须要在类的第一行
        //this()调用自身构造器必须要在类的第一行
        System.out.println("Student无参构造器");
    }
    private String name = "qinjiang";
    public void print(){
        System.out.println("Student");
    }
    public void test1(String name){
        System.out.println(name);//秦疆
        System.out.println(this.name);//qinjiang
        System.out.println(super.name);//kuangshen
    }
    public void test2(){
        print();//Student
        this.print();//Student
        super.print();//Person
    }
}
public class Application {
    public static void main(String[] args) {
        Student student = new Student();//Student无参构造器 Person无参构造器
        student.test1("秦疆");
        student.test2();
    }
}

Super注意点:

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

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

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

VS this:

代表的对象不同:this:本身调用者这个对象 super:代表父类对象的应用

前提:this:没有继承也可以使用 super:只能在继承条件才可以使用

构造方法:this():本类的构造 super():父类的构造

方法重写:

静态方法

public class B {
    public static void test(){
        System.out.println("B=>test()");
    }
}
public class A extends B{
    public static void test() {
        System.out.println("A=>test()");
    }
}
public class Application {
    public static void main(String[] args) {
        A a = new A();
        a.test();//A=>test()
        B b = new A();
        b.test();//B=>test()
    } 
}

非静态方法:

public class B {
    public void test(){
        System.out.println("B=>test()");
    }
}
public class A extends B{
    //Override重写
    @Override//注解:有功能的注释!
    public void test() {
        System.out.println("A=>test()");
    }
}
public class Application {
    //静态的方法和非静态的方法区别
    //静态方法:方法的调用只和左边定义的数据类型有关
    //非静态方法:重写
    public static void main(String[] args) {
        //方法的调用只和左边定义的数据类型有关
        A a = new A();
        a.test();//A=>test()
        //父类的引用指向了子类
        B b = new A();//子类重写了父类的方法
        b.test();//A=>test()
    }
}

即b是A new出来的对象,因此调用了A的方法

因为静态方法是类的方法,而非静态是对象的方法

有static时,b调用了B类的方法,因为b是用B类定义的

没有static时,b调用的是对象的方法,而b是用A类new的

需要有继承关系,子类重写父类的方法!

1.方法名必须相同

2.参数列表必须相同

3.修饰符:范围可以扩大但不能缩小:public>protected>default>private

4.抛出的异常:范围可以被缩小,但不能扩大;ClassNotFoundExecption -->Exception(大)

重写,子类的方法和父类必须要一致:方法体不同!

为什么需要重写:

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

Alt + Insert : override

多态: 

public class Person {
    public void run(){
        System.out.println("run");
    }
}
public class Student extends Person{
    @Override
    public void run(){
        System.out.println("son");
    }
    public void eat(){
        System.out.println("eat");
    }
}
public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //new Student();
        //new Person();
        //可以指向的引用类型就不确定了:父类的引用指向子类对象
        Student s1 = new Student();
        //Person 父类型,可以指向子类,但是不能调用子类独有的方法
        Person s2 = new Student();
        Object s3 = new Student();
        //对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
        s1.run();//son
        s2.run();//son 子类重写了父类的方法,执行了子类的方法
        s1.eat();//eat
        s2.eat();//报错 父类型可以指向子类,但是不能调用子类独有的方法
        ((Student)s2).eat();//eat 强制转换
    }
}

多态注意事项:

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

2.父类和子类,有联系,类型转换异常!ClassCastException!

3.存在条件:继承关系,方法需要重写,父类引用指向子类对象!father f1 = new Son();

static方法属于类,它不属于实例。final常量。private方法不能被重写

public class Person {
}
public class Teacher extends Person{
}
public class Student extends Person{
}
public class Application {
    public static void main(String[] args) {
        //Object > String
        //Object > Person > Student
        //Object > Person > Teacher
        //System.out.println(X instanceof Y);能不能编译通过看X,Y有没有父子关系。X所指向的实例类型是不是Y的子类型
        Object object = new Student();
        System.out.println(object instanceof Student);//true
        System.out.println(object instanceof Person);//true
        System.out.println(object instanceof Object);//true
        System.out.println(object instanceof Teacher);//false
        System.out.println(object instanceof String);//false
        System.out.println("=========================");
        Person person = new Student();
        System.out.println(person instanceof Student);//true
        System.out.println(person instanceof Person);//true
        System.out.println(person instanceof Object);//true
        System.out.println(person instanceof Teacher);//false
        //System.out.println(person instanceof String);编译报错
        System.out.println("=========================");
        Student student = new Student();
        System.out.println(student instanceof Student);//true
        System.out.println(student instanceof Person);//true
        System.out.println(student instanceof Object);//true
        //System.out.println(student instanceof Teacher);编译报错
    }
}
public class Student extends Person{
    public void go(){
        System.out.println("go");
    }
}
public class Application {
    public static void main(String[] args) {
        //类型之间的转化:父类转子类
        //子类转换为父类,可能丢失自己本来一些方法
        Person student = new Student();
        //student.go();报错
        //Student student1 = (student)student
        //student1.go()//go
        ((Student)student).go();//go
        Student student1 = new Student();
        //((Person)student1).go报错
        Person person = student1;
        //person.go报错
    }
}

1.父类引用指向子类的对象,但不能用子类对象的方法

2.把子类转换为父类,向上转型,可能丢失自己本来的方法

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

4.方便方法的调用,减少重复的代码

static:

public class Student {
    private static int age;//静态的变量 多线程
    private double score;//非静态的变量
    public void run(){
        go();
    }
    public static void go(){
    }
    public static void main(String[] args) {
        Student s1 = new Student();
        System.out.println(Student.age);//类变量
        //System.out.println(Student.score);报错
        System.out.println(s1.age);
        System.out.println(s1.score);
        //Student.run()报错
        Student.go();
        go();
    }
}

static跟类一块加载而且是第一加载,main函数也是static,不能直接调用非静态方法是因为静态方法加载完非静态方法还没加载出来

代码块:

public class Person {
    //2.执行先于构造器后于静态代码块  赋初值
    {
        //代码块(匿名代码块)
        System.out.println("匿名代码块");
    }
    //1:执行先于构造器 只执行一次
    static{
        //静态代码块
        System.out.println("静态代码块");
    }
    //3.
    public Person(){
        System.out.println("构造方法");
    }
    public static void main(String[] args) {
        Person person1 = new Person();
        System.out.println("==========");
        Person person2 = new Person();
    }
}

注意加载顺序: 

1:静态代码块执行先于构造器只执行一次
2.代码块执行先于构造器后于静态代码块 

final修饰符不能被继承,断子绝孙

public  final  class Person {
}
public class Student extends Person{//报错
}

抽象类:

public abstract class Action {
    //约束,想要有人帮我们实现
    //abstract.抽象方法,只有方法名字,没有方法的实现!
    public abstract void doSomething();
}
public class A extends Action{
    //报错,需要进行重写
}
//抽象类的所有方法,继承了它的子类,都必须重写它的方法,除非子类也是抽象的
public class A extends Action{
    @Override
    public void doSomething(){
    }
    //1.不能new这个抽象类,只能靠子类去实现它:约束
    //2.抽象类里面可以写普通方法
    //3.抽象方法必须在抽象类中
}
public abstract class A extends Action{
}
public class Application {
    public static void main(String[] args) {
        //Action action = new Action();报错
        A a = new A();
    }
}

1.不能new抽象类,只能靠子类去实现它:约束

2.抽象类里面可以写普通方法

3.抽象方法必须在抽象类中

4.抽象类的所有抽象方法,继承了它的子类,都必须重写它的抽象方法,除非子类也是抽象的

思考:抽象虽然不能new有构造器吗?有

抽象存在的意义:抽象出来 节省代码的开发提高开发效率,

接口:

public interface UserService {
    //属性默认的是常量 默认public static final
    //public static final int AGE = 99 = int AGE = 99;
    //接口中的所有定义的方法默认都是抽象的 默认public abstract
    //public abstract void run() = void run()
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);
}
public interface TimeService {
    void timer();
}
//类 可以实现接口 implements 接口
//实现了接口的类,就需要重写接口中的中的方法
//多继承,利用接口实现多继承
public class UserServiceImpl implements UserService,TimeService{
    @Override
    public void add(String name) {
    }
    @Override
    public void delete(String name) {
    }
    @Override
    public void update(String name) {
    }
    @Override
    public void query(String name) {
    }
    @Override
    public void timer() {
    }
}

接口的作用:

1.约束

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

3.方法public abstract

4.常量public static final

5.接口不能被实例化,接口中没有构造方法

6.implements可以实现多个接口(多继承,很多不用抽象而用接口就是因为接口能够多继承)

7.必须要重写接口中的方法

内部类:

public class Outer {
    private int id;
    public void out(){
        System.out.println("这是外部类的方法");
    }
    public class Inner{
        public void in(){
            System.out.println("这是内部类的方法");
        }
        //获得外部类的私有属性
        public void getID(){
            System.out.println(id);
        }
    }
}
public class Application {
    public static void main(String[] args) {
        //new
        Outer outer = new Outer();
        //通过这个外部类来实例化
        //Outer.Inner inner = new Outer().new Inner();
        Outer.Inner inner = outer.new Inner();
        inner.in();
    }
}

一个java类中可以有多个class类,但是只能有一个public class

public class Outer {
}
//一个java类中可以有多个class类,但是只能有一个public class
//public class A{
//}
class A{
}

局部内部类

public class Outer {
    //局部内部类
    public void method(){
        class Inner{
            public void in(){
            }
        }
        Inner inner = new Inner();
        Inner.in();
    }
}

没有名字初始化类,不用将实例保存到变量中

public class Test {
    public static void main(String[] args) {
        //没有名字初始化类,不用将实例保存到变量中
        new Apple().eat();//1
        //UserService userService = new UserService(){}
        new UserService() {
            @Override
            public void hello() {

            }
        };
    }
}
class Apple{
    public void eat(){
        System.out.println("1");
    }
}

interface UserService{
    void hello();
}

异常:

 

 

 

 

public class Test {
    public static void main(String[] args){
        int a = 1;
        int b = 0;
        try {//try监控区域
            System.out.println(a/b);
        }catch(ArithmeticException e){//catch捕获异常
            System.out.println("程序出现异常,变量不能为0");
        }finally{//处理善后工作
            System.out.println("finally");
        }
        //程序出现异常,变量不能为0 finally
        //try catch必须要有
        //finally 可以不要finally,假设IO,资源,关闭!
    }
}//算术异常ArithmeticException
public class Test {
    public static void main(String[] args){
        int a = 1;
        int b = 0;
        try {//try监控区域
            new Test().a();
        }catch(ArithmeticException e){//catch捕获异常
            System.out.println("异常");
        }finally{//处理善后工作
            System.out.println("finally");
        }
        //try catch必须要有
        //finally 可以不要finally,假设IO,资源,关闭!
    }
    public void a(){
        b();
    }
    public void b(){
        a();
    }
}//不是异常而是错误StackOverflowError

可以有多个catch,假设要捕获多个异常,从小到大

public class Test {
    public static void main(String[] args){
        int a = 1;
        int b = 0;
        //假设要捕获多个异常,从小到大!
        try {//try监控区域
            System.out.println(a/b);
            //new Test().a();
        }catch(Error e){
            System.out.println("Error");
        } catch(Exception e){
            System.out.println("Exception");
        }catch(Throwable e){
            System.out.println("Throwable");
        }finally{//处理善后工作
            System.out.println("finally");
        }
        //Exception finally
    }
    public void a(){
        b();
    }
    public void b(){
        a();
    }
}

选中 快捷键ctrl+alt+t

public class Test {
    public static void main(String[] args){
        try {
            new Test().test(1,0);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }
    }
    public void test(int a,int b) throws ArithmeticException {
        if(b==0) {//throw throws
            throw new ArithmeticException();//主动抛出异常,一般在方法中使用
        }
        System.out.println(a/b);
    }
}

抛出异常在方法中是throw,在方法上是throws,运行时异常不用throw throws,程序自己也会抛出错误

如果不用try catch捕获异常,程序就会停止,而不往下执行

自定义异常:

 

//自定义的异常类
public class MyException extends Exception{
    //传递数字>10
    private int detail;
    public MyException(int a){
        this.detail=a;
    }
    //toString:异常的打印信息
    @Override
    public String toString(){
        return "MyException{"+"detail="+detail+'}';
    }
}

public class Test {
    //可能会存在异常的方法
    static void test(int a) throws MyException {
        System.out.println("传递的参数为:"+ a);
        if(a>10){
            throw new MyException(a);//可以在这里用try catch捕获异常,也可以通过throws抛出去,交给外面去捕获
        }
        System.out.println("OK");
    }
    public static void main(String[] args) {
        try {
            test(11);
        } catch (MyException e) {
            System.out.println("MyException:"+e);
        }//传递的参数为11 MyException:MyException{detail=11}
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值