Java (195-214) [final, permission, inner class]

Keywords: Java

1. The concept and four methods of final keyword

Today is the last day of basic learning! ~

2. The final keyword is used to modify a class

3. final keyword to decorate member methods

4. final to decorate local variables

package cn.itcast.day11.demo01;

/*
final Keywords represent final and immutable.

There are four common uses:
1. Can be used to modify a class
2. Can be used to modify a method
3. It can also be used to modify a local variable
4. It can also be used to modify a member variable
 */
public class Demo01Final {

    public static void main(String[] args) {
        int num1 = 10;
        System.out.println(num1); // 10
        num1 = 20;
        System.out.println(num1); // 20

        // Once final is used to decorate a local variable, the variable cannot be changed.
        // "One assignment, life-long"
        final int num2 = 200;
        System.out.println(num2); // 200

//        num2 = 250; / / wrong writing! Can't change!
//        num2 = 200; / / wrong writing!

        // Write correctly! As long as there is only one assignment
        final int num3;
        num3 = 30;

        // For basic types, immutability means that the data in variables cannot be changed
        // For reference types, immutability means that the address value in variables cannot be changed
        Student stu1 = new Student("Zhao Liying");
        System.out.println(stu1);
        System.out.println(stu1.getName()); // Zhao Liying
        stu1 = new Student("Wallace Huo");
        System.out.println(stu1);
        System.out.println(stu1.getName()); // Wallace Huo
        System.out.println("===============");

        final Student stu2 = new Student("Gao Yuanyuan");
        // Wrong way to write! The reference type variable of final, where the address cannot be changed
//        stu2 = new Student("Zhao Youting");
        System.out.println(stu2.getName()); // Gao Yuanyuan
        stu2.setName("High circle circle circle circle");
        System.out.println(stu2.getName()); // High circle circle circle circle
    }

}

5. The final keyword is used to decorate member variables

package cn.itcast.day11.demo01;

/*
For a member variable, if the final keyword is used to modify it, the variable is also immutable.

1. Because the member variable has a default value, it must be assigned manually after final is used, and the default value will not be given any more.
2. For the member variables of final, either direct assignment or construction method assignment is used. Choose one of them.
3. It must be ensured that all overloaded construction methods in the class will eventually assign values to the member variables of final.
 */
public class Person {

    private final String name/* = "Lu Han "*/;

    public Person() {
        name = "Guan Xiaotong";
    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

6. Four permission modifiers

7. The concept and classification of inner class

8. Definition of member inner class

9. Use of member inner classes

package cn.itcast.day11.demo03;

/*
If the inner part of one thing contains another thing, then this is the inner part of one class contains another class.
For example: the relationship between the body and the heart. Another example: the relationship between automobile and engine.

Classification:
1. Member inner class
2. Local inner class (including anonymous inner class)

Definition format of member inner class:
Modifier class external class name{
    Modifier class inner class name{
        // ...
    }
    // ...
}

Note: for internal use and external use, access is optional; for external use and internal use, internal class objects are required.

==========================
How do I use member inner classes? There are two ways:
1. Indirect way: in the method of external class, use internal class; then main just calls the method of external class.
2. Direct method, formula:
Class name object name = new class name ();
[External class name. Internal class name object name = new external class name (). New internal class name ();]
 */
public class Demo01InnerClass {

    public static void main(String[] args) {
        Body body = new Body(); // Objects of external classes
        // Call the methods of the external class through the objects of the external class, in which the internal class Heart is used indirectly
        body.methodBody();
        System.out.println("=====================");

        // Write according to the formula:
        Body.Heart heart = new Body().new Heart();
        heart.beat();
    }

}
package cn.itcast.day11.demo03;

public class Body { // External class

    public class Heart { // Member inner class

        // Methods of inner class
        public void beat() {
            System.out.println("Heart beat: bounce!");
            System.out.println("My name is:" + name); // Write correctly!
        }

    }

    // Member variables of external classes
    private String name;

    // Methods of external classes
    public void methodBody() {
        System.out.println("Methods of external classes");
        new Heart().beat();
    }

    public String getName() {
        return name;
    }

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

10. Variable access with the same name of inner class

  

package cn.itcast.day11.demo03;

// If duplicate names appear, the format is: external class name. this. External class member variable name
public class Outer {

    int num = 10; // Member variables of external classes

    public class Inner /*extends Object*/ {

        int num = 20; // Member variable of inner class

        public void methodInner() {
            int num = 30; // Local variable of inner class method
            System.out.println(num); // Local variable, principle of proximity
            System.out.println(this.num); // Member variable of inner class
            System.out.println(Outer.this.num); // Member variables of external classes
        }

    }

}

11. Definition of local inner class

It is to call a method of an external class, then define a local internal class in the method, and use it in the method

Only your own method can be used

class Outer {

    public void methodOuter() {
        class Inner { // Local inner class
            int num = 10;
            public void methodInner() {
                System.out.println(num); // 10
            }
        }

        Inner inner = new Inner();
        inner.methodInner();
    }

}

12. The final problem of local inner class

public class MyOuter {

    public void methodOuter() {
        int num = 10; // Local variable of the method

        class MyInner {
            public void methodInner() {
                System.out.println(num);
            }
        }
    }

}

13. Anonymous inner class

 

Anonymous inner class has no name

It does not need to write the implementation class, but directly uses the anonymous inner class of the interface

14. Notes on using anonymous inner classes

 

public class DemoMain {

    public static void main(String[] args) {
//        MyInterface obj = new MyInterfaceImpl();
//        obj.method();

//        MyInterface some = new MyInterface(); / / error!

        // Use anonymous inner class, but not anonymous object. The object name is objA
        MyInterface objA = new MyInterface() {
            @Override
            public void method1() {
                System.out.println("Anonymous inner class implements method! One hundred and eleven-A");
            }

            @Override
            public void method2() {
                System.out.println("Anonymous inner class implements method! Two hundred and twenty-two-A");
            }
        };
        objA.method1();
        objA.method2();
        System.out.println("=================");

        // Anonymous inner class is used, object name is omitted, and anonymous object is also used
        new MyInterface() {
            @Override
            public void method1() {
                System.out.println("Anonymous inner class implements method! One hundred and eleven-B");
            }

            @Override
            public void method2() {
                System.out.println("Anonymous inner class implements method! Two hundred and twenty-two-B");
            }
        }.method1();
        // Because anonymous objects cannot call the second method, you need to create another anonymous object of anonymous inner class
        new MyInterface() {
            @Override
            public void method1() {
                System.out.println("Anonymous inner class implements method! One hundred and eleven-B");
            }

            @Override
            public void method2() {
                System.out.println("Anonymous inner class implements method! Two hundred and twenty-two-B");
            }
        }.method2();
    }

}

15. Class as member variable type

Analogy with string

package cn.itcast.day11.demo06;

public class DemoMain {

    public static void main(String[] args) {
        // Create a hero character
        Hero hero = new Hero();
        // Name the hero and set his age
        hero.setName("Galen");
        hero.setAge(20);

        // Create a weapon object
        Weapon weapon = new Weapon("AK-47");
        // Equip heroes with weapons
        hero.setWeapon(weapon);

        // Galen, 20, attacked the enemy with a Doran sword.
        hero.attack();
    }

}
package cn.itcast.day11.demo06;

// Heroes in the game
public class Hero {

    private String name; // Hero's name
    private int age; // Age of hero
    private Weapon weapon; // Hero's weapon

    public Hero() {
    }

    public Hero(String name, int age, Weapon weapon) {
        this.name = name;
        this.age = age;
        this.weapon = weapon;
    }

    public void attack() {
        System.out.println("Age is" + age + "Of" + name + "use" + weapon.getCode() + "Attack the enemy.");
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public Weapon getWeapon() {
        return weapon;
    }

    public void setWeapon(Weapon weapon) {
        this.weapon = weapon;
    }
}
package cn.itcast.day11.demo06;

public class Weapon {

    private String code; // Code of weapon

    public Weapon() {
    }

    public Weapon(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

16. Interface as member variable type

package cn.itcast.day11.demo07;

public class DemoGame {

    public static void main(String[] args) {
        Hero hero = new Hero();
        hero.setName("AI Xi"); // Set the name of the hero

        // Set hero skills
//        hero.setSkill(new SkillImpl()); / / use a separately defined implementation class

        // You can also use anonymous inner classes instead
//        Skill skill = new Skill() {
//            @Override
//            public void use() {
//                System.out.println("Pia~pia~pia~");
//            }
//        };
//        hero.setSkill(skill);

        // Further simplification, using both anonymous inner classes and anonymous objects
        hero.setSkill(new Skill() {
            @Override
            public void use() {
                System.out.println("Biu~Pia~Biu~Pia~");
            }
        });

        hero.attack();
    }

}
package cn.itcast.day11.demo07;

public class Hero {

    private String name; // Name of hero
    private Skill skill; // Hero skills

    public Hero() {
    }

    public Hero(String name, Skill skill) {
        this.name = name;
        this.skill = skill;
    }

    public void attack() {
        System.out.println("My name is" + name + ",Start casting skills:");
        skill.use(); // Abstract methods in call interface
        System.out.println("Cast skill completed.");
    }

    public String getName() {
        return name;
    }

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

    public Skill getSkill() {
        return skill;
    }

    public void setSkill(Skill skill) {
        this.skill = skill;
    }
}
package cn.itcast.day11.demo07;

public interface Skill {

    void use(); // An abstract way to release skills

}
package cn.itcast.day11.demo07;

public class SkillImpl implements Skill {
    @Override
    public void use() {
        System.out.println("Biu~biu~biu~");
    }
}

17. Interface as parameter and return value of method

It's ok

Left father and right son

package cn.itcast.day11.demo07;

import java.util.ArrayList;
import java.util.List;

/*
java.util.List It's the interface that ArrayList implements.
 */
public class DemoInterface {

    public static void main(String[] args) {
        // On the left is the interface name, and on the right is the implementation class name. This is polymorphic writing
        List<String> list = new ArrayList<>();

        List<String> result = addNames(list);
        for (int i = 0; i < result.size(); i++) {
            System.out.println(result.get(i));
        }
    }

    public static List<String> addNames(List<String> list) {
        list.add("Di Ali Gerba");
        list.add("Guli Nazha");
        list.add("Marr Zaha");
        list.add("Sha Yang Na La");
        return list;
    }

}

18. Cases of sending red envelopes

 

Published 28 original articles, won praise 6, visited 579
Private letter follow

Posted by Yetalia on Thu, 30 Jan 2020 21:05:42 -0800