JAVA 基础 小结

long 类型 long类型数字可以在数字后加L或者l 加以区分类型

package hk.base.long1;

public class Deom3 {

    public static void main(String[] args) {
        long a=10000l;
        long b=1000L;
        long c=1;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}

 float 类型        float类型数字必须加F或者f 加以区分类型        否则会编译错误

package hk.base.float1;

public class Deom3 {

    public static void main(String[] args) {
        float a=1.1F;
        float b=1.2f;
        //float c=1.1;编译错误

        System.out.println(a);
        System.out.println(b);
    }
}

 进制                0b二进制        0八进制        0x十六进制

package hk.base.int1;

public class Deom3 {

    public static void main(String[] args) {
        int a=10;
        int b=010;
        int c=0b10;
        int d=0x10;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}

 小数                在十进制转二进制 如果出现无线小数 信息会变

package hk.base.o;

public class Deom3 {

    public static void main(String[] args) {
        float f=1.5f;
        double d1=1.5;
        double d2=3.0/2;
        System.out.println(f==d1);
        System.out.println(f==d2);

        f=0.1f;
        d1=0.1;
        d2=1/10.0;
        System.out.println(f==d1);
        System.out.println(f==d2);
        System.out.println(d2);
        System.out.println(d2);
        System.out.println(f);
    }
}

 float        有舍入误差        详情查找小数在计算机常见存储方式帮助理解

package hk.base.float2;

public class Deom3 {

    public static void main(String[] args) {
        float f=123456789123456f;
        double d=1234567891;
        System.out.println(f==f+1);
        System.out.println(d==d+1);
    }
}

 不建议使用浮点数进行比较

类型转换

jdk7数字之间可以用下划线分割 

package hk.base.select;

public class Deom3 {

    public static void main(String[] args) {
        int a=10000_0000;
        float b=100_0000.012f;
        double c=10_0000.236;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}

package hk.base.type;

public class Deom3 {

    public static void main(String[] args) {
        byte b=7;  //-128~127(1)
        short s=32765;//-32768~32767(2)
        int i=47483647;   //-2147483648~2147483647(4)
        long l=1123123113223L;//-922337036854775808~922337036854775807(8)
        float f=1.012f;//(4)
        double d=100.236;//(8)

        System.out.println(b+s+i+l);//long
        System.out.println(b+s+i);//int
        System.out.println(b+s);//int
    }
}

 Scanner

package hk.base.scanner;

import java.util.Scanner;

public class Deom2 {

    public static void main(String[] args) {
        System.out.println("使用nextline接收:");

        Scanner scanner = new Scanner(System.in);

        if (scanner.hasNextLine()){
            String str= scanner.nextLine();
            System.out.println(str);
        }
        scanner.close();




        Scanner scanner=new Scanner(System.in);
        System.out.println("使用next接收:");
        if (scanner.hasNext()){
            System.out.println(scanner.next());
        }


        scanner.close();

    }
}

字符串比较        str1.equals(str2)

package hk.base.select;

import java.util.Scanner;

public class Deom1_if {
    public static void main(String[] args) {

        Scanner scanner=new Scanner(System.in);
        String str=scanner.nextLine();
        if (str.equals("Hello.")){
            System.out.println("^_^ /@");
        }
        else{
            System.out.println("-_-=*");
        }
        scanner.close();
    }
}

for        for(int i=0;i<10;i++)        for(int i,array1)

package hk.base.loop;

public class Deom3 {
    public static void main(String[] args) {

        int oddSum=0;
        int evenSum=0;
        for (int i=0;i<=100;i++){
            if (i%2==1){
                oddSum+=i;
            }else{
                evenSum+=i;
            }
        }


        int[] i0={1,2,3,4,5,6,7,8,9,10,11,22,33,44,55,66,77,88,99};

        for (int i1 : i0) {
            System.out.println(i1);
        }
        System.out.println(" oddSum= "+oddSum);
        System.out.println("evenSum= "+evenSum);
    }
}

break ; continue        可以在循环中实现goto的效果

package hk.base.loop;

public class GotoContinueDeom {

    public static void main(String[] args) {

        outer:for (int i = 0; i < 100; i++) {
            end:for (int i1 = 0; i1 < 10; i1++) {
                if (i==1&&i1==1){
                    continue outer;
                }
                else if (i==2&&i1==3){
                    break end;
                }
            }
        }

        System.out.println("111");
    }
}

方法重载        可以写方法名字相同参数不同的多个方法,编译器会根据参数去匹配最合适的方法

如下:sum(int.... num)为可变参数

package hk.base.method;

public class Deom3 {
    public static void main(String[] args) {
        int num[]={1,2,3};
        Deom3 deom3=new Deom3();
        System.out.println(deom3.sum(1,2,3,4,5,6,7,8,9,10));
        System.out.println(deom3.sumc(num));
        System.out.println(deom3.add(8,9));
//        System.out.println(add(1,8));
    }

    public int add(int a,int b){
        return a+b;
    }

    public static int sum(int... num){
        int sum=0;
        for (int i:num) {
            sum+=i;
        }
        return sum;
    }
    public static int sumc(int[] num){
        int sum=0;
        for (int i:num) {
            sum+=i;
        }
        return sum;
    }
}

方法传入数组变量名称,相当于传入数组地址,能改变数组本身;数组是引用数据类型,对象都是引用数据类型,传进去的是堆的地址;

一般数据类型存放在栈中,传递时是值本身

package hk.base.array;

public class Deom2 {

    public static void main(String[] args) {

        int[] arrays={1,2,3,4,6,7};
        printarray(arrays);
        System.out.println("====================");
        //reverse() 传入arrays,相当于传入array的地址;在函数中改变参数,会影响本身;;
        int[] news=reverse(arrays);
        printarray(news);
        System.out.println("====================");
        printarray(arrays);
    }
    public static void printarray(int[] array){
        for (int i : array) {
            System.out.println(i);
        }
    }

    public static int[] reverse(int[] array){
        int len=array.length;
        for (int i = 0; i < len/2; i++) {
            array[i]=array[len-1-i]^array[i];
            array[len-1-i]=array[len-1-i]^array[i];
            array[i]=array[len-1-i]^array[i];
        }
        return array;
    }
}

数组的常见方法

package hk.base.array;

import java.util.Arrays;

public class Deom3 {

    public static void main(String[] args) {

        int[][] array={{1,2},{4,5},{7,8}};
        int[] nums={1,4,5,78,8,9,6,5,87,5,6,74,564,854,48654,848};
        int[] a=new int[10];
        int[] c=new int[10];
        //Arrays 的方法;

        Arrays.fill(a,2,5,10240);//数组填充数字
        Arrays.fill(c,2,6,10240);
        System.out.println(Arrays.toString(a));

        System.out.println(Arrays.equals(a,6,8,c,7,9));
        Arrays.sort(a);
        a[8]=100000;
        System.out.println(Arrays.binarySearch(a,100000));//二分法查找;



        String arrays= Arrays.toString(nums);//数组字符串化

        Arrays.sort(nums);//数组排序(升序)

        String newArray= Arrays.toString(nums);

        System.out.println(arrays);
        System.out.println("===========================================");
        System.out.println(newArray);
    }
}

static【上】        static方法是和类一起加载的        非static方法只有在该类实例化后才存在

package hk.oop.func;

public class Deom1 {

    public static void main(String[] args) {
///
        HelloWorld.sayStatic();
        //调用静态方法

        System.out.println(HelloWorld.b);

//
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.say();
        //调用非静态方法

        System.out.println(helloWorld.a);


    }
}
package hk.oop.func;

public class HelloWorld {
    int a=100;
    static int b=1;


    //非静态方法:::
    public void say(){
        System.out.println("Hello,World!");
    }
    //静态方法:::
    public static void sayStatic(){
        System.out.println("Hello,World!(Static)");
    }
}

构造器        每当一个类被实例化,该类的构造器会启动

构造器一般是用来初始化,构造器名称必须和方法名相同,当然构造器也可以重载,一般写了有参构造,默认需要些无参构造

package hk.oop.op;

public class Creat {
    String name;
    //这是构造器,实例化初始值
    //当这个类被实例化时;即使用new ,会启动该方法
    public Creat(){//无参构造
        this.name="罗非鱼";
    }

    public Creat(String name){//有参构造
        this.name=name;
    }
}
package hk.oop.op;

public class Application2 {
    public static void main(String[] args) {
        Creat creat = new Creat();
        System.out.println(creat.name);

        Creat creat1 = new Creat("章北海");
        System.out.println(creat1.name);
    }
}
优先级
public
protected
default
private

将类中属性改为private,外部就无法通过 类.属性 来直接访问        可以写get,srt方法来访问,修改

package hk.oop.op.Private;

public class Private {
    public static void main(String[] args) {

        Pr p = new Pr();
        //public
        p.name="vg";
        //private
//        p.id=0;   会报错

        System.out.println(p.getSex());
        p.setSex('x');
        System.out.println(p.getSex());

        System.out.println(p.getN_ame0());
        p.setName("章北海");
        System.out.println(p.getN_ame0());
    }
}
package hk.oop.op.Private;

public class Pr {
    public String name;
    //属性私有
    private int id;
    private  char sex;
    //get  获取这个数据
    public String getN_ame0() {
        return this.name;
    }
    public char getSex(){
        return this.sex;
    }
    //set  给数据设置值
    public void setName(String name){
        this.name=name;
    }
    //可以设置输入的条件;如:符合条件的数值才会传入;
    public void setSex(char sex){
        if (sex=='f'||sex=='m'||sex=='F'||sex=='M'){
            this.sex=sex;
        }else{
            System.out.println("ERROR!  sex should in ['f','F','m','M']");
        }
    }

    public void setId(int id) {
        this.id = id;
    }
}

extends        Java只能单继承

package hk.oop.op.Extends;
//  在对应类的界面
//  ctrl  +  h
//  查看继承树

//Person1 继承  Person0
public class Person1 extends Person0{
}
package hk.oop.op.Extends;

public class Person0 {

    public int many=10_0000_0000;
    private String name="叶文洁";

    public void say(){
        System.out.println("我 说 话 了   ~ _ ~");
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
package hk.oop.op.Extends;
//   只能单继承
public class Extends {
    public static void main(String[] args) {
        Person1 p1 = new Person1();
        p1.say();

        System.out.println(p1.getName());
        p1.setName("罗辑");
        System.out.println(p1.getName());

        System.out.println(p1.many);
        p1.many=10_9999;
        System.out.println(p1.many);
    }
}

super注意点:
    1.super调用父类的构造方法,必须在构造方法发的第一个
    2.super 必须只能出现在子类方法 或者 构造方法中
    3.super 和 this 不能同时调用构造方法

this:
本身调用者这个对象
没有继承也可以使用
本类构造
super:
代表父类对象的应用
只能在继承条件下使用
父类构造

this()   调用自己的构造器,必须在该类构造器的第一行
super()  调用父类的构造器,必须在子类构造器的第一行
结论:super()/this() 只能调用一个
package hk.oop.op.Super2;

public class Person0 {
    public Person0(String name) {
        System.out.println("Person0无参构造");
    }

    String name="叶文洁";
    void print(){
        System.out.println("Person0");
    }

}
package hk.oop.op.Super2;

//Person1 继承  Person0
public class Person1 extends Person0 {
    public Person1() {
        //this();//调用自己的构造器,必须在该类构造器的第一行
        super("name");//调用父类的构造器,必须在子类构造器的第一行
        //结论:super()/this() 只能调用一个

        //调用了父类的无参构造
        System.out.println("Person1无参构造");
    }
//    public Person1(String name) {
//        //调用了父类的无参构造
//        System.out.println("Person1无参构造");
//    }

    private String name="罗非鱼";
    public void print(){
        System.out.println("Person1");
    }
    public void text(String name){
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);
    }
    public void text1(){
        print();
        this.print();
        super.print();
    }
}
package hk.oop.op.Super2;

public class Extends {
    public static void main(String[] args) {
        Person1 p1 = new Person1();
    }
}

如果不写super()或者this()默认super()

重写                重写是方法的重写,与属性无关

重写:
需要有继承关系
子类重写父类方法
方法名必须相同
参数列表必须相同
修饰符:范围可以扩大但不能缩小
抛出异常:范围,可以被缩小,但不能扩大

为什么需要重写?
父类的功能子类不一定需要,或者不一定满足

静态方法的重写        static 方法没有被重写        无法重写

package hk.oop.op.Super3__re_fun_static;

//重写是方法的重写,与属性无关
public class Application {

    public static void main(String[] args) {

        A a=new A();
        a.text();//调用A类静态方法 A=>text()

//  父类的引用指向了子类
        B b=new A();
        b.text();//调用B类静态方法 B=>text()
//  方法的调用只和左边定义的数据类型有关

        //static 方法没有被重写
    }
}
package hk.oop.op.Super3__re_fun_static;

public class B {
    public static void text(){
        System.out.println("B=>text()");
    }

}
package hk.oop.op.Super3__re_fun_static;

public class A extends B {

    public static void text() {
        System.out.println("a=>text()");
    }
}

重写非静态方法                 子类重写了父类的方法

package hk.oop.op.Super3__re_fun_unstatic;

//重写是方法的重写,与属性无关
public class Application {
    public static void main(String[] args) {
//非静态方法
        A a=new A();
        a.text();//调用A类非静态方法 A=>text()

        B b=new A();
        b.text();//调用B类非静态方法 A=>text()
        //子类重写了父类的方法
    }
}
package hk.oop.op.Super3__re_fun_unstatic;

public class B {
    public void text(){
        System.out.println("B=>text()");
    }
}
package hk.oop.op.Super3__re_fun_unstatic;

public class A extends B{

//  快捷键 alt+insert  选择Override
    @Override
    public void text() {
        System.out.println("a=>text()");
    }
}

 多态

* 多态注意事项:
1.多态是方法的多态,没有属性的多态
*
2.子类与父类,有联系  类型转换异常
*
3.存在条件:继承关系,方法需要重写,父类的引用指向子类   father f1=new son();
*
*=======================================
*    ----------  无法重写的类型  ---------
*  1. static  方法,属于类,它不属于实例
*  2.final  常量
*  3.private方法
一个对象的实际类型时是确定的,可以指向的引用类型就不确定:父类的引用指向子类

        //student 可以调用自己或者继承父类的方法
        Student p1=new Student();
        //person 父类型,可以指向子类,但不能调运子类独有的方法
        Person p2=new Student();
        Person p2_2=new Person();
        Object p3=new Student();
强制转换,由低类型向高类型转        Person => Student ;就可以使用子类都有方法
        ((Student)p2).eat();//强制转换,由低类型向高类型转
        //Person => Student ;就可以使用子类都有方法
对象能执行哪些方法,主要看左边类型,和右边类型关系不大
        p1.eat();
        p1.run();
        p2.run();
        p2_2.run();

instanceof                x instanceof y ;y与x的‘实际’类型是否有关系        左边是对象,右边是类

左边的对象是否是右边类或者该类的子类创建的实例对象

当该测试对象创建时右边的声明类型和左边的类其中的任意一个跟测试类必须得是继承树的同一分支或存在继承关系,否则编译器会报错

public static void main(String[] args) {
        Object object =new Student();

        System.out.println(object instanceof Student);
        System.out.println(object instanceof Person);
        System.out.println(object instanceof Object);
        System.out.println(object instanceof Teacher);
        System.out.println(object instanceof String);

        System.out.println("===================================");
        Person person =new Student();

        System.out.println(person instanceof Student);
        System.out.println(person instanceof Person);
        System.out.println(person instanceof Object);
        System.out.println(person instanceof Teacher);
        //System.out.println(person instanceof String);//编译错误
        System.out.println("===================================");

        Student student =new Student();

        System.out.println(student instanceof Student);
        System.out.println(student instanceof Person);
        System.out.println(student instanceof Object);
        //System.out.println(student instanceof Teacher);//编译错误
        //System.out.println(student instanceof String);//编译错误

        System.out.println("===================================");

    }

        Object object0 =new Person();

        System.out.println(object0 instanceof Student);
        System.out.println(object0 instanceof Person);
        System.out.println(object0 instanceof Object);
        System.out.println(object0 instanceof Teacher);
        System.out.println(object0 instanceof String);
        System.out.println("===================================");
        Person person0 =new Person();

        System.out.println(person0 instanceof Student);
        System.out.println(person0 instanceof Person);
        System.out.println(person0 instanceof Object);
        System.out.println(person0 instanceof Teacher);
        //System.out.println(person0 instanceof String);//编译错误
        System.out.println("===================================");

        //Student student0 =new Person();

        //System.out.println(student0 instanceof Student);
        //System.out.println(student0 instanceof Person);
        //System.out.println(student0 instanceof Object);
        //System.out.println(student0 instanceof Teacher);//编译错误
        //System.out.println(student0 instanceof String);//编译错误

 类的类型转换

强制转换;将父类型向子类型强制转换,以达到使用子类型独有方法
将子类型向父类型转换;不用强转        可能会丢失自己本来的一些方法
package hk.oop.op.Instanceof;

public class Trans {
//类型之间转换:父  子
public static void main(String[] args) {
//  高:(父)        低:(子)
    Person s1=new Student();
    //s1.go();//会报错
    //强制转换;将父类型向子类型强制转换,以达到使用子类型独有方法
    ((Student)s1).go();

    System.out.println("================================");
    //或者
    Student s1_1=(Student)s1;
    s1_1.go();
    //将子类型向父类型转换;不用强转
    //可能会丢失自己本来的一些方法
    Student s2=new Student();
    System.out.println("================================");
    Person s2_1=s2;//父类的引用指向子类的对象
    //s2_1.go();//会报错
    ((Student)s2_1).go();
}
}

static【下】

静态导入包
package hk.oop.op.Static;
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;

public class Test {
    public static void main(String[] args) {
        System.out.println(random());
        System.out.println(PI);
    }
}
static 方法与该类一起加载,非静态还没有加载出来;所以无法让无法让静态方法调运非静态方法

非静态方法可以调用静态方法;静态方法可以调用静态方法,但不能调运非静态方法
//  final  类型无法继承(常量)
package hk.oop.op.Static;

public class Student extends Person{
    private static int age;
    private float score;
    //非静态方法可以调用静态方法
    //静态方法可以调用静态方法,但不能调运非静态方法
    public  static void run(){
        //go();//编译错误

    }
    public void go(){
        run();
    }
    public static void main(String[] args) {
        Student s1=new Student();

        s1.run();
        s1.go();

        Student.run();
        //Student.go();//编译错误
        System.out.println("========================");
        System.out.println(Student.age);
        //System.out.println(Student.score);//编译错误
        System.out.println(s1.age);
        System.out.println(s1.score);
    }
}

匿名代码块        运行顺序在构造器之前 静态代码块之后

静态匿名代码块        运行顺序构造器与匿名代码块之前 且在程序中只执行一次

package hk.oop.op.Static;

public class Person {
    {//2
        //代码块(匿名代码块)
        System.out.println("匿名代码块");
    }//在构造器之前   静态代码块之后
    static {//1
        //静态代码块
        System.out.println("静态代码块");
    }//构造器与匿名代码块之前   且只执行一次

    public Person() {//3
        System.out.println("构造器");
    }
    public static void main(String[] args) {
        Person p=new Person();
        System.out.println("=====================");
        Person p1=new Person();
    }
}

 抽象类        abstract

含有抽象方法的类必须是抽象类        抽象类可以写普通方法

抽象方法  可以不用写具体方法,只需写名称
public abstract class Abstract {
        public Abstract() {
                System.out.println("Abstract");
        }
        //        抽象方法  可以不用写具体方法,只需写名称
        public abstract void doSomething();

        public void f(){
                System.out.println("hhh");
        }
        }
抽象类不能new出来,只能靠子类其实现它;约束!
package hk.oop.op.Abstract;

public class Test {
    public static void main(String[] args) {
        //new Abstract();//编译错误
        //  抽象类不能new出来,只能靠子类其实现它;约束!
    }
}
非抽象类 继承 抽象类,必须要实现抽象类中所有抽象方法,除非是抽象类继承抽象类,就不用实现非抽象方法
public class TestAbstract extends Abstract{
    @Override
    public void doSomething() {
    }
}
package hk.oop.op.Abstract;

public abstract class TestAbstract1 extends Abstract{

}

interface        接口        接口多继承

接口都需要有实现类        接口中所有的定义其实默认public abstract                         接口中的属性    默认public static final类型
package hk.oop.InterFace;
//interface 定义关键字   ,   接口都需要有实现类
public interface Test {
//接口中所有的定义其实是抽象的 public
    public abstract void run();
    void go();
//    接口中的属性    public static final类型
    public static final int age=18;
    int days=1;
}
implements        一个类可以通过implements关键字实现接口                              实现接口的类,就需要重写接口中的方法

package hk.oop.InterFace;

//一个类可以通过   implements 关键字实现接口
//实现接口的类,就需要重写接口中的方法

//接口多继承
public class TestImpl implements Test,Text2{
    @Override
    public void run() {
    }
    @Override
    public void go() {
    }
    @Override
    public void text2() {
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑驴_找马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值