Simple review of java (complex)

      Finally, I have learned the basics in general, not enough, and I will make up later. Today, I reviewed it according to the video. It can be regarded as a small summary and purely recorded. Then I will look back and see if it is not dry goods or wet goods.

continue: 跳过本次循环语句,继续下一次循环。

continue的作用范围: 只能适用于循环语句。

一旦执行了continue语句,那么在循环体内continue之后的循环 语句跳过执行。

break: 用于结束一个循环语句或者是一个switch语句.

break作用范围: 只能用于循环语句或者是switch语句。

return 直接结束函数

You can take a look at this special one. I just reviewed it and I forgot to break it. It can also be used with the logo.

*outer:for(int j = 0 ; j<2; j++){ // j=0  j=1
inner:for(int i = 0 ; i<3 ; i++){ //i=0
    System.out.println("hello world");
    break outer; //结束当前所在的循环。 如果配合标识的使用,可以作用于外层的for循环。
            }//意思就是说 结束当前的外部outer

Bubble algorithm:

public static void sort(int[] buffer) {
        for (int i = 0; i < buffer.length-1; i++) {
            for (int j = i+1; j < buffer.length; j++) {
                if(buffer[j]<buffer[i]) {
                    int temp=buffer[j];
                    buffer[j]=buffer[i];
                    buffer[i]=temp;
                }
            }
        }
    }

I have reviewed that algorithm bubbling is the most basic sorting algorithm. Tomorrow, I will sort out other commonly used algorithms. I forgot to pick up what I learned before.

Function overloading: Two or more functions with the same name appearing in a class are called function overloading.

The role of function overloading: A function name can deal with various types of parameters.

Requirements for function overloading
1. The function names are the same.
2. The formal parameter list is inconsistent (the number of formal parameters is inconsistent or the types corresponding to the formal parameters are inconsistent)
3. It has nothing to do with the return value type.

函数重写:子父类出现了同名的函数称作为函数的重写。

函数重写必须要是在继承的关系才存在的。

函数重写的需求: 父类的功能无法满足子类的需求。

函数重写的要求
    1. 子父类的函数名与形参列表必须一致。
    2. 子类的权限修饰符必须 要大于或者等于父类的权限修饰符。
    3. 子类的返回值类型必须要小于或者等于父类的返回类型。
    4. 子类抛出的异常类型必须要小于或者等于父类抛出的异常类型。

Remember: In println, toString() is called by default to
encapsulate polymorphic inheritance. When
inheritance : private members of the parent class cannot be inherited.

Just look at a packaged chestnut:

//封装的步骤:
    //1. 私有化要封装的属性。
    //2. 根据需求提供对应的get或者是set方法。
class Member{

    private String name;

    private String sex;



    public String getName() {
        return name;
    }

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

    public void setSex(String sex){
        if("男".equals(sex)||"女".equals(sex)){
            this.sex = sex;
        }
    }

    public String getSex(){

        return sex;
    }


}

instanceof determines whether the specified object belongs to a certain class.

The premise of using instanceof: There must be an inheritance or implementation relationship between the judged object and the category.

instanceof uses the format:
object instanceof class.

Only polymorphism is possible to use the instanceof keyword.

Main application scenarios: Judgment before data type forced conversion.

class Animal{}
class Dog extends Animal{}
public class One {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Animal animal=new Dog();
        if(animal instanceof Dog) {
            System.out.println("Yes");
        }

    }
}

输出 Yes

Multithreaded chestnuts:

/*
线程:

多线程的存在的意义: 解决了一个进程允许多个任务可以同时执行。

多线程的创建方式:

    方式一:  继承Thread。
        1. 自定义一个类继承Thread.
        2. 重写Thread的run方法,把自定义线程的任务代码放在run方法上。
        3. 创建Thread类的子类对象,并且调用start方法开启线程。


    方式二: 实现Runnable接口。。
        1. 自定义一个类实现Runnable接口.
        2. 实现Runnable的run方法。把自定义线程的任务代码放在run方法上。
        3. 创建Runnable实现类的对象。
        4. 创建Thread的对象,然后把Runnable实现类的对象作为参数传递。
        5. 调用Thread对象的start方法开启线程。

java中的同步机制:

出现线程安全问题的根本原因:
    1. 存在两个或者两个以上的线程共享着资源。
    2. 操作资源的代码块必须有语句。


    1. 同步代码块

        同步代码块的格式:
            synchronized(锁对象){
                需要被同步的代码块...
            }
    同步代码块要注意的细节:
        1.锁对象可以是任意的对象。
        2. 锁对象必须 是多线程共享的资源。否则锁不住。
        3. 没有线程安全问题的时候不要使用锁,因为会导致效率降低。
        4. 调用sleep方法并不会释放锁对象,但是调用wait方法的线程就会释放锁对象。

    2. 同步函数
            修饰符 synchronized 返回值类型 函数名(形参列表..){

            }
    注意: 
        1. 同步函数的锁对象是不能任意的,非静态同步函数的锁对象是this对象,静态函数的锁对象是当前字节码对象。
        2. 同步函数的锁不能由你指定,是固定的。

 */
class MyThread2 implements Runnable{

    @Override
    public void run() {
        for(int i = 0 ; i < 100 ; i++){
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }

}




class MyThread extends Thread{

    @Override
    public void run() {
        //把自定义线程的任务代码代码写在这里。。
        for(int i = 0 ; i < 100 ; i++){
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
     }
}

public class Demo14  {

    public static void main(String[] args) {
        MyThread thread1 = new MyThread();
        thread1.start(); //开启了自定义的线程。线程一旦开启就会执行run方法中的代码块。


        MyThread2 thread2 = new MyThread2();
        //创建Thread的对象
        Thread t = new Thread(thread2);
        //调用Thread对象的start方法
        t.start();

    }
}

Gather chestnuts:

/*

集合

单例集合
----------| Collection 单列集合的根接口
----------------| List 如果是实现了List接口集合类具备的特点: 有序,可重复。
-------------------| ArrayList   底层使用Object数组实现的。 特点: 查询速度快,增删慢。
-------------------| LinkedList  底层是使用了链表数据数据结构实现的。 特点: 查询慢,增删快。
-------------------| Vector(了解)   底层使用Object数组实现的, 实现与ArrayList是一样,只不过是线程安全的,操作效率低。

----------------| Set  如果是实现了Set接口集合类具备的特点: 无序,不可重复。
------------------| HashSet  底层使用的是哈希表实现的。  
------------------| TreeSet  底层使用二叉数实现。 

双列集合:
--------| Map  (只需要把Map接口的方法全部练习一次即可。)
-----------| HashMap  底层使用的是哈希表实现的。  
-----------| TreeMap   底层使用二叉数实现
-----------| HashTable(了解)

 */
class Book{

    String name;

    double price;

    public Book(String name, double price) {
        super();
        this.name = name;
        this.price = price;
    }

    @Override
    public int hashCode() {
        return this.name.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        Book b  = (Book)obj;
        return this.name.equals(b.name);
    }


    @Override
    public String toString() {
        return "[书名:"+ this.name+" 价格:"+this.price+"]";
    }
}

public class Demo15 {

    public static void main(String[] args) {
        //不允许重复的书名存在。
        HashSet<Book> books = new HashSet<Book>();
        books.add(new Book("深入javaweb",34));
        books.add(new Book("java神书",78));

        //修改书名
        Iterator<Book> it = books.iterator();
        while(it.hasNext()){
            Book b = it.next();
            if(b.name.equals("java神书")){
                b.name = "java编程思想";
            }
        }

        //为什么修改名字之后不能删除了呢?
        books.remove(new Book("java神书",78));

        System.out.println("集合的元素:"+ books);




    }

}
/*
从键盘输入一个字母组成字符串,分别统计每个字母出现的次数(10分)
要求输出的效果按照字母的顺序输出  a(7)b(5)...
*/
public class Demo16 {

    public static void main(String[] args) {
        System.out.println("请输入一段字符串:");
        Scanner  scanner = new Scanner(System.in);
        String line = scanner.next();
        char[] arr = line.toCharArray();    //先把字符串转换成字符数组。
        TreeMap<Character, Integer> map = new TreeMap<Character, Integer>(); 
        for(char c : arr){
            if(map.containsKey(c)){ //map集合已经包含了该字符
                int count = map.get(c);
                map.put(c, count+1);
            }else{  //没有包含
                map.put(c, 1);
            }
        }
        System.out.println(map);


    }

}

output:
write picture description here

The format of the ternary operator:
boolean expression? value1:value2 true is value1 false is value2

Construction code block: It is used to initialize the class to create a class and execute it once
Static construction code block: It is executed and executed once when the class is loaded into memory

/*
构造代码块:给对象进行统一的初始化工作。

应用场景: 如何创建任意对象的时候都需要调用某个方法为该对象进行初始化时,这时候就可以使用构造代码块。

静态代码块:

静态代码块是静态代码块所属的类被加载到内存的时候执行的。


静态代码块的应用场景: 以后主要用于准备一个项目的初始化工作。 
    比如: 从配置配置文件中读取数据库用户名与密码。


 */

class Baby{

    int id;

    String name;

    //构造代码块的代码其实是在构造函数中执行的。 
    {

        cry();
    }

    static{
        System.out.println("静态代码块执行了...");
    }

    public Baby(int id, String name) {
        this.id = id;
        this.name = name;

    }

    public Baby(){

    }

    public void cry(){
        System.out.println("哭...");
    }

    @Override
    public String toString() {
        return " 编号:"+this.id+" 姓名:"+ this.name;
    } 
}




public class Demo9 {

    public static void main(String[] args) {
        Baby b1 = new Baby();
        Baby b2 = new Baby(110, "狗娃");
    }

}

Memory analysis:
write picture description here

/*
 多态: 父类的引用类型变量指向了子类的对象,或者是接口的引用类型变量指向接口实现类的对象。 

应用: 
    1. 多态应用于形参类型的时候,可以接收更多类型的参数,
        sort(List  list)
        sort(ArrayList list)
        sort(LinkedList list)

    2. 多态用于返回值类型的时候可以返回更多类型的参数。

迭代器的作用: 用于获取集合中的元素。     

 内部类: 

内部类的好处: 内部类可以直接访问外部类的成员。

 */

interface MyList{

    List subList(int fromIndex, int toIndex);
}






public class Demo12 {

    public static void main(String[] args) {    
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(10);
        list.add(19);
        list.add(12);
        list.add(6);

        Iterable<Integer> it = (Iterable<Integer>) list.iterator();
    }
}
匿名内部类:
/*
匿名对象:没有引用类型变量指向的对象就称作为匿名对象。

匿名内部类:没有类名的类就称作为匿名内部类。

匿名内部类使用前提: 必须存在继承或者实现关系。

因为匿名内部类没有类名,所以创建匿名内部类对象的时候就必须要借助与它父类的名字或者它父接口的名字来创建。 
但是匿名内部类只不过是没有类名,其他的一概成员都是具备的。


匿名内部类的应用场景: 主要是作为参数传递使用。

 */

interface Dao{

    public void add();
}

class Outer{

    public void print(){
        new Dao(){ //这里不是创建接口 的对象,是创建了Dao实现类的对象,但是这个类是一个匿名内部类而已,没有类名借用了父接口 的名字而已。
            //大括号中就写匿名内部类的成员。
            int  num =10;

            @Override
            public void add() {
                System.out.println("添加学生 ..");
            }
        }.add();

    }
}




public class Demo13 {

    public static void main(String[] args) {

        /*Outer outer = new Outer();
        outer.print();*/

        JButton button = new JButton("aa");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("aaa");

            }
        });


    }

}
/*
 匿名对象: 没有引用类型变量指向的对象就称作为匿名对象。

匿名对象的主要作用: 简化书写。

匿名对象主要用于两种应用场景:
    1. 如果一个对象的方法只会调用一次的时候,然后该对象就不再使用了,这时候就就可以使用匿名对象。
    2. 作为参数传递。
 */
    BufferedReader bufferedReader = new BufferedReader(new FileReader("F:\\a.txt"));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325933090&siteId=291194637