Final and anonymous inner classes in java learning

Final and anonymous inner classes for java learning

0x00 Preface

In the last few articles, I learned that the subclass can be rewritten on the basis of the parent class after inheriting the parent class. Then some things in the program may not be easy to change. Then Java provides the final keyword.

0x01 final use

Decoration class format:

final class Final {

}

Decoration method format:

    public final void abc(){
        System.out.println("一个不可重写的的方法");
    }

Modifier variable:

final int a =1;

Classes decorated by this keyword cannot be inherited

Methods modified by this keyword cannot be overridden

A variable modified by this variable cannot be re assigned.

0x02 permission modifier

Pubilc and private are probably the most used in the past. Four access permissions are provided in Java. When modified with different access permissions, the modified content has different access permissions.

public 公共的
prorected 受保护的
default  默认的
private  私有的

You can see that public is the maximum permission and private is the minimum.

If no modifier is added, the default is default.

0x03 internal class

Format:

public class Final{
    public class abc{
        
        
    }
}

Features of internal class access: internal classes can directly access members of external classes, including private methods. To access the members of the inner class, an object of the inner class must be created.

Format for creating internal class objects:

The definition method is similar to that of instantiated objects.

The inner class is still an independent class. After compilation, the inner class will be compiled into an independent class Class file, but preceded by the class name of the external class and the $symbol.

Anonymous Inner Class

Anonymous inner classes are simplified versions of inner classes. Its essence is an anonymous child object with a concrete implementation of the parent class or parent class interface.

Anonymous inner classes may be commonly used in code. Several steps to do when using an interface,

If it is called only once, you can use the anonymous inner class.

The precondition is that the anonymous inner class must inherit a parent class or parent interface.

new 父类或者接口名(){
    //重写方法
    pubilc void method(){
        ...
    }
}

Anonymous inner class code implementation:

这里来定义一个父类:

public abstract class Fu {
    public abstract void fly();
    
}



main方法代码:

public class ZiMain {
    public static void main(String[] args) {

        Fu f = new Fu() {
            @Override
            public void fly() {
                System.out.println("会飞");
            }
        };
        f.fly();
    }
}

0x04 end

After an article is finished, anonymous internal classes may be used more in it. For example, when multithreading is created, because threads cannot be called multiple times, anonymous internal classes can be directly used for creation.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>