Java novice learning 2021-1-22 record the daily learning content (if there is any infringement, please contact to delete!!!)

2021-1-22

1. Polymorphism overview

Insert picture description here

package com.wc.test;

/**
 * @author wc
 * @Date: 2021/01/23/9:06
 */
public class Test {
    
    
    public static void main(String[] args) {
    
    
      Animal a=new Cat();
      a.eat();
    }
}

class Animal{
    
    
    public void eat(){
    
    
        System.out.println("动物吃饭");
    }
}

class Cat extends Animal{
    
    
    @Override
    public void eat(){
    
    
        System.out.println("猫吃鱼");
    }
}

2. Features of polymorphic member access

Insert picture description here
Insert picture description here

3. Transformation in polymorphism

Insert picture description here
Insert picture description here

4. Multi-station forced transfer risks and solutions

Insert picture description here
Insert picture description here

package com.wc.test;

/**
 * @author wc
 * @Date: 2021/01/23/9:06
 */
public class Test {
    
    
    public static void main(String[] args) {
    
    
        useAnimal(new Cat());
        useAnimal(new Dog());
    }

    private static void useAnimal(Animal a) {
    
    
        a.eat();
        //狗中的特有方法想要调用可以通过强转,强转风险
        //变量不能确定,通过instanceof关键字进行判断
        if (a instanceof Dog) {
    
    
            Dog dog = (Dog) a;
            dog.drink();
        }
    }
}

abstract class Animal {
    
    
    //抽象方法,不知道执行的谁的吃
    public abstract void eat();
}

class Cat extends Animal {
    
    
    @Override
    public void eat() {
    
    
        System.out.println("猫吃鱼");
    }
}

class Dog extends Animal {
    
    
    @Override
    public void eat() {
    
    
        System.out.println("狗吃肉");
    }

    public void drink() {
    
    
        System.out.println("狗喝水");
    }
}

5. Member inner class (understand)

Insert picture description here
Insert picture description here
Insert picture description here
1. Private member inner class
Insert picture description here
2. Static member inner class
Insert picture description here

6. Local inner class (understand)

Insert picture description here
Insert picture description here

7. Anonymous inner class

Insert picture description here
Insert picture description here

Insert picture description here

8. The use of Lanbda expressions

Insert picture description here
Insert picture description here
Insert picture description here
Case study
Insert picture description here

package com.wc.show;

/**
 * @author wc
 * @Date: 2021/01/23/17:37
 */
public class ShowHandler {
    
    
    public static void main(String[] args) {
    
    
        ShowHandlerDemo(new ShowHandlerMethod() {
    
    
            @Override
            public void show() {
    
    
                System.out.println("展示");
            }
        });

        ShowHandlerDemo(()->{
    
    
            System.out.println("展示");
        });
    }

    public static void ShowHandlerDemo(ShowHandlerMethod showHandlerMethod) {
    
    
        showHandlerMethod.show();
    }
}

interface ShowHandlerMethod {
    
    
    void show();
}

Case study
Insert picture description here

package com.wc.show;

/**
 * @author wc
 * @Date: 2021/01/23/17:58
 */
public class CalculatorDemo {
    
    
    public static void main(String[] args) {
    
    
        useCalculator(new Calculator() {
    
    
            @Override
            public int calc(int a, int b) {
    
    
                return a + b;
            }
        });

        useCalculator((int a, int b)->{
    
    
            return a+b;
        });
    }

    public static void useCalculator(Calculator calculator) {
    
    
        int result = calculator.calc(2, 3);
        System.out.println(result);

    }
}

interface Calculator {
    
    
    int calc(int a, int b);
}

Insert picture description here

9. The difference between anonymous inner classes and Lambda expressions

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49221590/article/details/113028027