It is a study memo of the internal class and lambda style that I am not good at. ..
//External class
class Outer{
//Inner class
class Inner{
//
}
}
The inner class defined in the method can be created only in this method.
class Outer{
void test() {
class Inner{
}
Inner in = new Inner();
}
}
The inner class can access the private instance variables of the outer class.
class Outer{
private String msg = "hoge";
void test() {
class Inner{
void test() {
System.out.println(msg);
}
}
Inner in = new Inner();
in.test();
}
}
public class MultiThreadEx {
public static void main(String[] args) {
Outer o = new Outer();
o.test();
}
}
An internal class companion. A class without a class name is called an anonymous class.
Preparation:
interface Hello{
public void hello();
}
class Greeting {
static void greet(Hello s) {
s.hello();
}
}
General example:
class Person implements Hello{
@Override
public void hello() {
//TODO auto-generated method stub
System.out.println("Hayo");
}
}
public class SampleProgram{
public static void main(String[] args) {
Person p = new Person();
Greeting.greet(p);
}
}
Inner class example:
//How to write an inner class
public class SampleProgram{
public static void main(String[] args) {
class Person implements Hello{
@Override
public void hello() {
//TODO auto-generated method stub
System.out.println("Hani");
}
}
Person p = new Person();
Greeting.greet(p);
//How to write the above two lines in one line
Greeting.greet(new Person());
}
}
Anonymous class example: If you declare a class at the place where you specify the argument of the method, you do not need to give the class name. Hmm ... I'm not used to it. .. ..
public class SampleProgram{
public static void main(String[] args) {
Greeting.greet(new Hello() {
@Override
public void hello() {
//TODO auto-generated method stub
System.out.println("Sleepy");
}
});
}
}
A lambda expression rule for declaring a class that implements a functional interface? According to How to write without code (?)
(Argument string) -> {Processing content}
An interface that has only one abstract method. It seems to be useless if there are two or more methods.
As a test, after adding the method
The target type of this expression must be a functional interface
//The target type of this expression must be a function interface
Method of type Greeting greet(Hello)Is an argument(() -> {})Not applicable to
And a compile error was displayed.
public class SampleProgram{
public static void main(String[] args) {
Greeting.greet( () -> { System.out.println("I am hungry"); } );
}
}
--The argument column type can be omitted --If there is only one argument, the () surrounding the argument can be omitted. --If there is only one statement in the process, you can omit the {}, returen keyword, and semicolon (;) that surround the process.
//Before correction:
Greeting.greet( () -> { System.out.println("I am hungry"); } );
//Revised:
Greeting.greet( () -> System.out.println("I am hungry"));
import java.util.ArrayList;
import java.util.List;
class Animal{
private String type ;
private String name;
Animal(String t, String n){
this.type = t;
this.name = n;
}
public void showAnimal() {
System.out.println(this.type+":"+this.name);;
}
}
public class SampleProgram{
public static void main(String[] args) {
List<Animal> list = new ArrayList<Animal>();
list.add(new Animal("Cat","Marble"));
list.add(new Animal("Parakeet", "Peeco"));
list.add(new Animal("Dog","Pochi"));
//Lambda expression
list.forEach((Animal a) ->{ a.showAnimal();});
//Lambda abbreviation
list.forEach(a -> a.showAnimal());
}
}
Recommended Posts