External class. this. Member Method
(2) The internal class of the member can access the members of the External class unconditionally, but the access to members of the internal class of the member is so simple. To access members of a Member's internal class in an external class, you must first create an object of the member's internal class and then access it by referencing the object:
(3) The internal class of a member is attached to an external class. That is to say, to create an object of the internal class of a member, the premise is that an object of the external class must exist. The general method for creating class objects within a Member is as follows:
2nd methods:
(4) Internal classes can have private, protected, public, and package access permissions. For example, in the above example, if the Inner class of the member is modified in private mode, only internal access to the outer class is allowed. If the Inner class is modified in public mode, access is allowed anywhere. If the Inner class is modified in protected mode, access can only be performed under the same package or inherit from external classes. If it is the default access permission, access can only be performed under the same package. This is a little different from the external class. The external class can only be modified by the public and package access permissions.
2. Local internal class
A local internal class is a class defined in a method or a scope. The difference between a local internal class and a member's internal class is that access to the local internal class is limited to the method or the scope.
Note:
A local internal class is like a local variable in a method. It cannot have public, protected, private, or static modifiers.
3. Anonymous internal class
Anonymous classes cannot be referenced because they cannot have names. They must be declared as part of the new statement at creation. This requires another form of new statement, as shown below: new <class or interface> <class subject> in this form of new statement declares a new anonymous class, it extends a given class or implements a given interface. It also creates a new instance of that class and returns it as the result of the statement.
For example, assume that you have a Map. The key is an item, and the value is the corresponding price. The unit is RMB. Now there is a need to convert the prices in it into US dollars. The traditional approach is to traverse the entire Map, then update each value, and convert the price to US dollars. This method is more troublesome. Function can be used to solve this problem.
New Function () {...} Is to create an internal class.
An anonymous internal class is the only class without a constructor (the constructor cannot be defined ). Because there is no constructor, the use of anonymous internal classes is very limited. Most anonymous internal classes are used for interface callback. During compilation, the anonymous internal class is automatically named Outter $1. class by the system. Generally, an anonymous internal class is used to inherit other classes or implement interfaces. No additional methods are required, but the inherited methods are overwritten or the interface methods are implemented.
An anonymous internal class cannot be an abstract class, because the system immediately creates an internal class object when creating an anonymous internal class. Therefore, anonymous internal classes cannot be defined as abstract classes.
4. static internal class
Static internal classes are also defined in another class, except that a keyword static is added before the class. Static internal classes do not need to depend on external classes. This is similar to static member attributes of the class, and they cannot use non-static member variables or methods of external classes, because you can create static internal class objects without external class objects, conflicts may occur if you allow access to non-static members of the external class, because non-static members of external classes must be attached to specific objects.
public class Outer2 {
-
private static String sname = "Outer2";
private String name;
-
public Outer2(String name) {
this.name = name;
}
-
// Member internal class
public static class Inner {
public void print() {
// The static internal class cannot access non-static member variables or methods of the external class
// System.out.println("Outer---->" + name);
// You can use static variables or methods.
System.out.println("Outer---->" + sname);
}
}
}
Call:
Outer2.Inner inner2 = new Outer2.Inner();
inner2.print();
-
public void drawCircle() {
System.out.println("Circle---->" + Circle.this.radius); // 12.5
}
public class Circle {
private double radius;
-
public Circle(double radius) {
this.radius = radius;
// Create an object for the internal class of the member
new Draw().drawCircle();
}
-
// Member internal class
class Draw {
public void drawCircle() {
System.out.println("Circle---->" + radius); // 12.5
}
}
-
public static void main(String[] args) {
Circle circle = new Circle(12.5); // Circle---->12.5
}
}
package com.qunar.fresh;
-
/**
* Created by xiaosi on 16-3-29.
*/
public class Outer {
private String name;
-
public Outer(String name) {
this.name = name;
}
-
// Member internal class
class Inner {
public void print() {
System.out.println("Outer---->" + name); // Outer---->OuterClass
}
}
-
public static void main(String[] args) {
Outer outer = new Outer("OuterClass");
Outer.Inner inner = outer.new Inner();
inner.print();
}
}
package com.qunar.fresh;
-
/**
* Created by xiaosi on 16-3-29.
*/
public class Outer {
private String name;
private Inner inner = null;
-
public Outer(String name) {
this.name = name;
}
-
public Inner getInstance() {
if (inner == null) {
return new Inner();
}
return inner;
}
-
// Member internal class
class Inner {
public void print() {
System.out.println("Outer---->" + name); // Outer---->OuterClass
}
}
-
public static void main(String[] args) {
Outer outer = new Outer("OuterClass");
Outer.Inner inner = outer.getInstance();
inner.print();
}
}
@Test
public void test1(){
Map map = Maps.newHashMap();
map.put("apple",4.5);
map.put("bear",6.3);
// {bear=6.3, apple=4.5}
System.out.println(map.toString());
-
Map newMap = Maps.transformValues(map, new Function () {
double rate = 0.1544;
@Override
// Convert to USD
public Double apply(Double input) {
return rate * input;
}
});
-
// {bear=0.97272, apple=0.6948000000000001}
System.out.println(newMap.toString());
}