java(doc)基础知识

Object里面的所有方法:

clone:

复制对象,副本
wait:

让当前线程等待。
notify:

唤醒当前线程
equals:

判断两个值是否相等
toString:

显示当前对象的地址
notifyAll

唤醒所有等待线程。
finalize:

垃圾回收最后一次判断该堆是否被引用。
hashCode:

返回本对象的hash码。
getClass

返回其对象的   类   运行时类   对象
                         人        魂          你

java共有48个关键字(关键字都是小写的,但小写的不一定是关键字)

java基本类型:primitive type(只有栈)

整数类型:byte字节(8位),short短的(16位-2字节),int(32位-4字节),long长(64位-8字节)

浮点类型:float单精度浮点(32-4字节),double双精度浮点(64 - 8字节)

字符类型:char字符(16位-2字节)

布尔类型:boolean布尔(1位)

java的引用类型:reference type(栈和堆都有)

指针类型,null,array,class,interface,enum(枚举)......

栈:stack

堆:heap

全局变量:global variable

局部变量:local  variable

局部变量的优先级大于全局变量

全局变量的初始默认值:int:0,char字符:'\ u0000's',boolean布尔:false

局部变量使用前必须初始化

1 javac HelloWorld.java(doc下编译)

2 java HelloWorld(doc下解释)

3 javadoc -d api -windowtitle title -header page brow -footer页脚HelloWorld.java(生成文档注释)

final:


  变量:最终变量,不能被修改
  方法:不能被重写
  类:不能被继承 (String,Math)

抽象类(abstract):没有方法体,不能被实例化,非私有,和类的关系是继承,可以有变量也可以有常量。

1.如果方法没有方法体,body,我们把这个方法声明为abstract.
2.抽象类不能被实例化。
3.抽象类的抽象成员必须是非私有的,否则不能被继承
4.一般方法可以有4个作用域修饰符。private default protected public
5.类和抽象类关系是继承
6.没有抽象方法的抽象类可以存在,但是没有必要,因为他不能实例化。
7.抽象类既可以有变量,又可以有常量


 接口(interface):都是抽象方法,所有方法必须是公共的,类可以实现多个接口,先继承后实现,接口与接口是继承,                                   接口可以继承多个接口,接口中的变量都是常量。


没有一般方法,都是抽象方法,
所有方法必须是公共的。
类和接口关系是实现implements,且可以实现多个接口,完成了灵活的特性
类和类,接口关系如果放在一起,必须是先继承在实现。
接口与接口关系是继承
接口可以继承多个接口
接口和类没关系
接口中的变量都是常量。

多态:所谓多态,就是指一个引用(类型)在不同的情况下的多种状态。也可以理解为,多态是指通过指向父类的指针,               来调用在不同子类中实现的方法。


多态:子类继承父类,一个类对象既可以指自己,也可以指其子类对象。
三种练习:解决了大量具有继承关系的方法的个数问题
a.向上转型,虚方法调用【编译类型和运行类型不同】。如果你想使用儿子特殊的方法,必须强转。
b.形参多态。
c.返回值多态

场景假设:

一个主人养了猫和狗,猫和狗都有自己爱吃的东西,主人在喂它们的时候,如果既要判断是猫还是狗,再判断他们分别爱吃什么,就显得很麻烦。如果主人养了很多种动物,这样的重复判断,就会浪费很多时间。有什么办法,能让主人拿到一种食物就知道这是哪种动物的,就好了。

1.首先,创造动物类:

// 动物类
class Animal {
    int age;
    String name;

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    // 动物类里面有叫和吃两个方法
    public void cry() {
        System.out.println("我不知道叫什么");
    }

    public void eat() {
        System.out.println("我不知道吃什么");
    }
}

2.其次,分别创造猫类和狗类(他们继承于动物类):

// 狗类继承于动物类
class Dog extends Animal {
    // 覆盖(重写)方法
    public void cry() {
        System.out.println("旺旺");
    }

    public void eat() {
        System.out.println("我是狗,我爱吃骨头");
    }
}

// 猫类继承于动物类
class Cat extends Animal {
    // 覆盖(重写)方法
    public void cry() {
        System.out.println("喵喵");
    }

    public void eat() {
        System.out.println("我是猫,我爱吃鱼");
    }
}

3.再者,创建食物类:

// 食物类
class Food {

    String name;

    public String getName() {
        return name;
    }

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

    // 食物类里面让它有一个方法
    public void showName() {

    }
}

4.再者,猫和狗都有自己不同的爱吃的食物(他们继承于食物类):

// 鱼(食物的一种)继承于食物
class Fish extends Food {
    public void showName() {
        System.out.println("食物:鱼");
    }
}

// 骨头(食物的一种)继承于食物
class Bone extends Food {
    public void showName() {
        System.out.println("食物:骨头");
    }
}

5.主人类(就可以将动物和对应的食物统一起来):

// 主人类 存在一種餵食方法
class Master {
    // 给动物喂食物,如果没有多态,他要写给猫喂食和给狗喂食两个方法
    // 有了多态,以后即使再来好多动物,用这一个函数就可以了
    public void feed(Animal an, Food f) {
        an.eat();
        f.showName();

    }
}

6.最后,方法的调用(测试):

public class DuoTaiDemo {

    public static void main(String args[]) {

        Master master = new Master();
        master.feed(new Dog(), new Bone());

        // hin方便,可以再试试
        master.feed(new Cat(), new Fish());

    }
}

7.运行结果:

匿名内部类:(立刻实现接口)

重写equals方法:

package com.lijiaxing.A;

public class D {
    private int month;
    private int year;
    private int day;

    public D(int year, int month, int day) {
        this.month = month;
        this.year = year;
        this.day = day;
    }
//重写equals方法
    @Override
    public boolean equals(Object obj) {
        if (this==obj){
            return true;
        }
        if (this.getClass()!=obj.getClass()){//运行时类(eg:人类!=动物类)
            return false;
        }
        if (obj==null){//没有堆
            return false;
        }
        D d =(D)obj;
        if ((this.year!=d.year)||(this.month!=d.month)||(this.day!=d.day)){
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        D d = new D(2018, 11, 6);
        D d1 = new D(2018, 11, 6);
        System.out.println(d.equals(d1));
    }
}

concat:连接字符串

  流:

  FileInputStream ---FileOutputStream     //文件输入输出流
  BufferedInputStream ---BufferedOutputStream    //缓存流
  BufferedReader ---BufferedWriter
  InputStreamReader ---OutputStreamWriter
  System.in  ---Scannner
  ByteArrayInputStream ---ByteArrayOutputStream
  RandomAccessFile ---seek ---Merge
  Serializable:序列化

用流读/写(writeFile)文件并在控制台输出文件(readFile)并拷贝文件(copyFile):

package com.lijiaxing.F;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class B {
    //写主方法太麻烦,采用注解方式进行测试
    @Test
    public void copyFile() {//拷贝文件
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream("c://mrbzms.mp4");//拷贝路径
            fileOutputStream = new FileOutputStream("d://1.mp4");//拷贝到路径
            int n = -1;
            byte[] buffer = new byte[1024 * 1024 * 10];//10M
            long start = System.currentTimeMillis();//获取开始时间
            while ((n = fileInputStream.read(buffer)) != -1) {//如果等于-1证明文件已经读完
                fileOutputStream.write(buffer, 0, n);//写入内容
            }
            long end = System.currentTimeMillis();//获取结束时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");//用SimpleDateFormat输出mm:ss的时间格式
            System.out.println(simpleDateFormat.format(end - start));//输出结束时间-开始时间(用时)
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void writeFile() {//写文件
        FileOutputStream fileOutputStream = null;//需要处理FileOutputStream的异常
        try {
            fileOutputStream = new FileOutputStream("d://33.txt");//要写入(保存)的文件位置
            try {
                fileOutputStream.write("你好,世界".getBytes());//写入内容,write方法需要处理异常
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();//调用close()方法时必须处理它的异常
                System.out.println("已关闭");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Test
    public void readFile() {//读文件
        FileInputStream fileInputStream = null;//需要处理FileInputStream的异常
        try {
            byte[] buffer = new byte[1024];//一次1K
            int n = -1;
            fileInputStream = new FileInputStream("d://122.sql");//文件位置
            while ((n = fileInputStream.read(buffer)) != -1) {//如果等于-1证明文件已经读完
                System.out.println(new String(buffer));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();//调用close()方法时必须处理它的异常
                System.out.println("已关闭文件输入流!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

HashMap转成List并排序!!!

package com.lijiaxing.F;

import org.junit.Test;

import java.util.*;

public class D {
    @Test
    public void go(){
        HashMap<String,Integer> hashMap = new HashMap();
        hashMap.put("A",100);
        hashMap.put("B",200);
        hashMap.put("C",300);
        hashMap.put("E",400);
        hashMap.put("F",500);
        hashMap.put("G",600);
        hashMap.put("H",700);
        hashMap.put("I",800);
        hashMap.put("J",900);
        hashMap.put("K",1000);
        hashMap.put("L",1100);
        hashMap.put("M",1200);
        Collection<Integer> collection = hashMap.values();
//        Iterator iterator = collection.iterator();
//        while (iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
        for (Integer integer:collection){
            System.out.println(integer);
        }
        //*******
        //把hashMap转成list做排序
        List<Map.Entry<String,Integer>> list = new ArrayList(hashMap.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
        System.out.println("-------------------------------------------------------");
        for (int i=0;i<list.size();i++){
            System.out.println(list.get(i));
        }
    }
}

线程方法 

猜你喜欢

转载自blog.csdn.net/qq_41558854/article/details/83583808