[Java Basics] Study Notes 3 - Abstract Classes and Internal Classes

final

final is often called the final modifier. After being modified by it, it generally has the following functions:

  1. Prevent classes from being inherited
  2. Prevent parent class methods from being overridden
  3. Prevent properties from being modified
  4. Prevent local variables from being modified

Properties modified by final can only be initialized in two places: when defining; inside a static code block;
do not try to assign a value to a final variable in the constructor! ! !

You do not need to set final decoration for a method in a final class

The combination of final and static works more efficiently!


abstract class

Abstract classes must memorize rules

  1. Abstract classes are not instantiable
  2. Abstract classes can have no abstract methods
  3. As long as there is an abstract method in a class, it must be declared as an abstract class
  4. abstract can only modify methods and classes
  5. An abstract class can have any members owned by a normal class
  6. If a class inherits an abstract class, it must implement all abstract methods in it, unless the class is also an abstract class.
  7. Abstract methods cannot have function bodies
  8. Abstract methods cannot be modified with private, final, static and other modifiers that prevent rewriting.

Simple abstract class and abstract method experimental code constructed based on the above knowledge

package chapter2;

public class AbstractDemo {
    
    
    public static void main(String[] args) {
    
    
        AbstractEntity entity = new AbstractEntity();
        entity.getName();
        entity.sayHi();
    }
}

abstract class Abstract1 {
    
    
    public abstract void getName();

    public void sayHi() {
    
    
        System.out.println("hw");
    }
}

class AbstractEntity extends Abstract1{
    
    
    @Override
    public void getName() {
    
    
        System.out.println("no name");
    }
}

interface

Static methods and default methods in the interface can have their own function bodies, and other functions cannot have function bodies.

interface USBInterface {
    
    
    public void getType();

    // 默认方法
    default public void defTest(){
    
    
        System.out.println("default test");
    }

    // 静态方法
    public static void staticTest() {
    
    
        System.out.println("static test");
    }
}

Interface usage details

  1. Interface cannot be instantiated
  2. Abstract methods in interfaces do not require the abstract modifier
  3. All methods in the interface are public
  4. Ordinary classes must rewrite methods to implement interfaces; abstract classes may implement interfaces without rewriting any methods.
  5. A class can implement multiple interfaces
  6. Interface properties can only be final
  7. To access interface properties, use the format:接口名.属性名
  8. Interfaces can additionally inherit from multiple other interfacesinterface B extends A,C{}

Interfaces can also implement polymorphism. The following shows array polymorphism.

package chapter2;

public class InterfaceDemo {
    
    
    public static void main(String[] args) {
    
    
        Device[] devices = new Device[2];
        devices[0] = new Phone();
        devices[1] = new Computer();

        for (Device device : devices) {
    
    
            if (device instanceof Phone) ((Phone) device).call();
            else device.work();
        }
    }
}

interface Device {
    
    
    void work();
}

class Phone implements Device {
    
    
    @Override
    public void work() {
    
    
        System.out.println("phone work");
    }

    public void call() {
    
    
        System.out.println("打电话了");
    }
}

class Computer implements Device {
    
    
    @Override
    public void work() {
    
    
        System.out.println("computer work");
    }
}

Interface polymorphic transfer phenomenon

For example, below, A2 inherits the interface A1, then our class implements the A2 interface and also implements the A1 interface, showing a transitive relationship.

package chapter2;

public class InterfacePolyLinkDemo {
    
    
    public static void main(String[] args) {
    
    
        A1 aa1 = new A();
        A2 aa2 = new A();
    }
}

interface A1 {
    
    
}

interface A2 extends A1 {
    
    
}

class A implements A2 {
    
    
}

inner class

local inner class

Defined in a method and has a class name

All members of the external class can be accessed, including private ones.
Access modifiers cannot be added, but they can be final modified.
The scope is only within the method.

If the inner class has properties or methods with the same name as the outer class, you can use the following format:外部类名.this.外部类属性或者方法名

package chapter3;

public class LocalInnerClass {
    
    
    public static void main(String[] args) {
    
    
        new LocalClass().getNum();
    }
}

class LocalClass {
    
    
    private int outNum = 1;

    public void getNum() {
    
    

        // 定义局部内部类 InnerClass
        class InnerClass {
    
    
            private int outNum = 2;

            public void say() {
    
    
                // 输出局部内部类的 outNum,即 2
                System.out.println(outNum);
                // 输出外部类的 outNum,即 1
                System.out.println(LocalClass.this.outNum);
            }
        }
        // 创建 InnerClass 的实例并调用 say() 方法
        new InnerClass().say();
    }
}

anonymous inner class

Defined within a method, but anonymous

We need to provide an interface so that anonymous inner classes can have a template to refer to.

package chapter3;

public class AnonymousInnerClass {
    
    
    public static void main(String[] args) {
    
    
        new AnonymousClass().getWhatSay();
    }
}

interface A {
    
    
    void say();
}

class AnonymousClass {
    
    
    public void getWhatSay() {
    
    
        // 匿名内部类
        new A() {
    
    
            @Override
            public void say() {
    
    
                System.out.println("fuck");
            }
        }.say();
    }
}

An important method of anonymous inner class is to pass it as an actual parameter, thereby simplifying the code operation.

package chapter3;

public class AnonymousInnerClass {
    
    
    public static void main(String[] args) {
    
    
        // 调用方法,直接传入一个匿名类实现
        sayHello(new A() {
    
    
            @Override
            public void say() {
    
    
                System.out.println("hello world");
            }
        });
    }

    // 接收一个接口作为形参
    public static void sayHello(A a){
    
    
        // 调用接口方法
        a.say();
    }
}

interface A {
    
    
    void say();
}

member inner class

Appears directly as a member of the external class and is not modified by static

He can add any access modifier

The inner class can access all members of the outer class and vice versa.

package chapter3;

public class MemberInnerClass {
    
    
    public static void main(String[] args) {
    
    
        new MemberClass().sayHello();
    }
}

class MemberClass {
    
    
    private int num = 100;

    public void sayHello() {
    
    
        System.out.println("fuck");
        InnerClass innerClass = new InnerClass();
        System.out.println(innerClass.getNum() + innerClass.res);
    }

    class InnerClass {
    
    
        private int res = 1000;

        public int getNum() {
    
    
            return MemberClass.this.num;
        }
    }
}

static inner class

Appears directly as a member of the outer class, static modified

Like all static-modified members, the inner class can only access static members of the outer class.


Guess you like

Origin blog.csdn.net/delete_you/article/details/132698130