This article is about the relationships between Java classes and Java inner classes.

Relationships between Java classes

Java Implementation Interface

A class implements the interface

Java rely on

Class A is said to depend on class B if the method return value type, method parameter type, and local variable type of class A use class B.

Java associated

Member variables are defined using class B in class A, which is called class A associated with class B.

Java polymerization

Aggregation is A kind of association. If class A consists of several classes B, class A cannot determine the life cycle of class B, so class A is called aggregation.

Java combination

Combination is A kind of association. If class A is composed of several classes B, class A can determine the life cycle of class B, so class A is called combination class

Is a, class A is a class B, is an inheritance relationship class A is a kind of class B, B is a general class, class A is a special class, and only if two classes conform to is A relationship can you use inheritance.

Like A, a class like a interface, is an implementation relationship.

Has A, class A has a, class B is an association.

Java inner class

package com.wkcto.chapter01.innerclass.demo01; /** * member inner classes * 1) Member inner classes, like instance variables/instance methods, belong to an Outer object * 2) In general, we use member inner classes to create objects in instance methods * 3) In Java, all classes are compiled to generate a separate bytecode file, Class * * */ public class Outer {int xx = 10; Public void m1() {system.out.println (" instance method "); xx = 123; Inner1 obj = new Inner1(); Inner1{}}Copy the code
package com.wkcto.chapter01.innerclass.demo02; Public class Outer {static int yy = 20; public class Outer {static int yy = 20; Public static void sm() {system.out.println (" static method "); yy = 202 ; Inner2 obj = new Inner2(); Inner2 = new Inner2(); // Static inner class Inner2{}}Copy the code
package com.wkcto.chapter01.innerclass.demo03; /** * Local inner class * 1) The inner class defined in the method body is the local inner class * 2) the scope of the local inner class is defined from the position of the inner class, * */ public class Outer {public void m3() {int zz = 10; // Inner3{} // Inner3{}Copy the code
package com.wkcto.chapter01.innerclass.demo04; /** * Anonymous inner class * 1) Anonymous inner class, which has no class name, * 2) Because anonymous inner class has no class name, the definition of anonymous inner class must be used with the creation of the object * 3) When assigning a reference to an interface/abstract class, we can assign an anonymous inner class object. You can use anonymous inner classes * often when subclasses of interface implementation classes/abstract classes are used only once, Public class Test {public static void main(String[] args) {// The reference to the interface can only be assigned to the implementation class object, // If the implementation class of the interface is used only once, it will never be used again. There is no need to define a separate implementation class. // If the implementation class of the interface is used only once, it will never be used again. Flyable Flyable = new Flyable() {// Override public void fly() { System.out.println(" I am a flight operation overridden in an anonymous inner class "); }}; flyable.fly(); // The class can not instantiate the object, the abstract class reference needs to assign the subclass object, define the subclass to inherit the abstract class, override the abstract method of the abstract class, then create the subclass object to assign the abstract class reference. Animal Animal = new Animal() {Override public void walk() {Override public void walk() { System.out.println(" This is an amazing animal that can walk without legs "); }}; animal.walk(); }}Copy the code

Conclusion:

Remembering the relationships between classes, you can use the Rose tool to draw the relationships between classes.

Know what inner classes are, know what inner classes are, know interface references/abstract class references can assign anonymous inner class objects.

  • The relationship between Java classes and classes, Java inner classes, is shared. Next, the introduction to Java Exceptions and Java Exception Handling is summarized.
  • Also welcome everybody exchange discussion, if this article has incorrect place, hope everybody many forgive.
  • Your support is my biggest motivation, if you help out, give me a thumbs up