1: Permission modifier
There are four permission modifiers in JAVA public > protexted > (default) >> private The same class yes yes yes yes Same package yes yes yes no Yes yes no no Different package non subclass yes no no no Note: (default) is not the keyword "default", but does not write at all */
2: Member inner class
package cn.itcast.day10; public class Body {//External class public class Heart{ //Member inner class public void bear(){ System.out.println("Heart beating, skipping!"); System.out.println("My name is:"+ name);//z correct writing } } //Member variables of external classes private String name; public void methodBody(){ System.out.println("Methods of external classes"); new Heart().bear(); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Main class
package cn.itcast.day10; /* If one thing contains another thing inside, then this is one class contains another class inside. 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{ //... } //... } matters needing attention: Internal use and external use, free access. For external use and internal use, internal class objects must be used. ================================== 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 object name = new external class name (). New internal class name (); */ public class Demo01InnerClass { public static void main(String[] args) { Body body = new Body();//External class object body.methodBody();//Call the methods of the external class through the objects of the external class, in which the internal class Heart is used indirectly System.out.println("============================"); Body.Heart heart = new Body().new Heart(); heart.bear(); } }
3: Variable access with the same name of inner class
Class Outer
package cn.itcast.day10; import javax.sound.midi.Soundbank; //If duplicate names appear, the format is: external class. this. External class variable name. public class Outer { int num =10;//Member variables of external classes public class Inner{ int num =20;//Member variable of inner class public void MethodInner(){ int num =30;//The local variable of the inner class method. System.out.println(num);//Local variable, principle of proximity System.out.println(Outer.this.num);//Member variables of external classes //System.out.println(super.num); } } }
Demo02InnerClass class
package cn.itcast.day10; public class Demo02InnerClass { public static void main(String[] args) { //External class name. Internal class name object name = new external class name (). New internal class name (); Outer.Inner obj = new Outer().new Inner(); obj.MethodInner();; } }
4: Local inner class
Class Outer
package cn.itcast.day10.demo1; /* If a class is defined within a method, then this is a local inner class "Local '. Only the current method can be used. It cannot be used outside this method Definition format: Modifier class external class name{ Modifier return value type external class method name (parameter list){ class Local inner class name{ //.. } } } The permission modifiers of the following classes are as follows: public >protected >(default) > private When defining a class, the permission modifier rule: 1:External class: public /(default) 2:Member internal class public > protected > (default) > private 3:Local inner class, nothing to write */ public class Outer { public void methodOutr(){ class Inner{//Local inner class int num = 10; public void methodInner(){ System.out.println(num);//10 } } Inner inner = new Inner(); inner.methodInner();; } }
Class DemoMain
package cn.itcast.day10.demo1; public class DemoMain { public static void main(String[] args) { Outer obj = new Outer(); obj.methodOutr(); } }
5: The final problem of local inner class
package cn.itcast.day10.demo1; /* Local inner class. If you want to access the local variable of the method, the local variable must be a valid finald Note: starting from java8 +, the final keyword can be omitted as long as the fact of local variables remains unchanged. Reason: 1:new The object is in the heap memory, 2:Local variables follow the method, in stack memory. 3;When the method is finished, the stack will be released immediately and the local variables will disappear immediately. 4:new The objects will continue to exist in the heap until garbage collection */ public class MyOuter { public void methodOutr(){ int num = 10; //Local variable of the method class MyInner{ public void methodInner(){ System.out.println(num); } } } }
6: Anonymous inner class
MyINterface interface
package cn.itcast.day10.Demo2; public interface MyINterface { void method();//Abstract methods. }
MyInterfaceImpl class
package cn.itcast.day10.Demo2; public class MyInterfaceImpl implements MyINterface { @Override public void method() { System.out.println("The method of class override!"); } }
Main class
package cn.itcast.day10.Demo2; /* If the implementation class of the interface (or the subclass of the parent class) only needs to be used once. In this case, you can omit the definition of this class and use anonymous inner class instead Definition format inside anonymous class: Interface name object name = new interface name (){ //Override overrides all abstract methods. }; */ public class DemoMain { public static void main(String[] args) { /* MyInterfaceImpl impl = new MyInterfaceImpl(); impl.method(); //MyInterface some = new MyInterface();//Wrong writing //Use anonymous inner class*/ MyINterface obj = new MyINterface() { @Override public void method() { System.out.println("Anonymous inner class implements method!"); } }; obj.method(); } }
7: Class as member variable type
Class Weapon
package cn.itcast.day10.Demo2; 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; } }
Class Hero
package cn.itcast.day10.Demo2; //Heroes in the game public class Hero { private String name;//Hero name private int age;//Age of hero private Weapon weapon;//Hero's weapon public Hero() { } public Hero(String name) { this.name = name; } 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 void attack(){ System.out.println("Age is"+age+"Of"+name+"use"+weapon.getCode()+"Attack enemy"); } public Weapon getWeapon() { return weapon; } public void setWeapon(Weapon weapon) { this.weapon = weapon; } }
Main class
package cn.itcast.day10.Demo2; public class DemoMain1 { public static void main(String[] args) { //Create a role 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("Sword of Dolan"); //Equip heroes with weapons hero.setWeapon(weapon); hero.attack(); } }
8: Interface as parameter of method
package cn.itcast.day10.Demo2; import java.util.ArrayList; import java.util.List; /* java.util.List It's the interface that ArrayList implements. */ public class DemoInteerface { 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("One"); list.add("Two"); list.add("Three"); return list; } }