[JAVA] Internal class and lambda study notes

It is a study memo of the internal class and lambda style that I am not good at. ..

Inner class

You can create a class in the class

//External class
class Outer{
    //Inner class
    class Inner{
        //
    }
}

You can create a class in the method of the class

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();
    }
}

Anonymous Class

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");

            }
        });
    }
}

Lambda expression

A lambda expression rule for declaring a class that implements a functional interface? According to How to write without code (?)

(Argument string) -> {Processing content}

What is a functional interface?

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.

If you rewrite the process created by the anonymous class to a lambda expression

public class SampleProgram{
    public static void main(String[] args) {
        Greeting.greet( () -> { System.out.println("I am hungry"); } );
    }
}

rule

--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"));

Let's make a loop statement using a lambda expression

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

Internal class and lambda study notes
Jigsaw study notes
Class and model
JavaFX study notes
Docker study notes
[Java] Study notes
abstract (abstract class) and interface (interface)
credentials.yml.enc and master.key <Notes>
Ruby study notes (puts)
Maven basic study notes
Class and instant part2
Modules and Mixins notes
Implement Thread in Java and try using anonymous class, lambda
Study Java # 2 (\ mark and operator)
java.util.Optional class and null and not null
[Implementation] Java Process class notes
Difference between class and instance
Relationship between package and class
Ruby study notes (variables & functions)