Study notes: java abstract classes and interfaces

final

When declaring classes, properties, and methods in Java, you can use the keyword final to modify, meaning "final".
The final marked class cannot be inherited. Improve safety and improve program readability.
The
final marked methods of String, System, and StringBuffer cannot be overridden by subclasses.
GetClass() in the Object class.
The final marked variables (member variables or local variables) are called constants. The name is capitalized and can only be assigned once.
The member variables marked with final must be explicitly assigned at the same time of declaration or in each constructor or code block before they can be used.

public final class Test{
    
    
    public static int totalNumber = 5 ;
    public final int ID;
    public Test(){
    
    
        ID = ++totalNumber;  //可在构造方法中给final变量赋值
    }
    public static void main(String[] args) {
    
    
        Test t = new Test();
        System.out.println(t.ID);
        final int I = 10;
        final int J;
        J = 20;
        J = 10;  //非法
    }}

Abstract class

When the abstract keyword is used to modify a class, the class is called an abstract class; when the abstract keyword is
used to modify a method, the method is called an abstract method.
Abstract method: only method declaration, no method implementation. End with a semicolon: abstract int abstractMethod( int a );
Classes containing abstract methods must be declared as abstract classes.
Abstract classes cannot be instantiated. An abstract class is used to be inherited as a parent class. The subclass of the abstract class must override the abstract method of the parent class and provide the method body. If all abstract methods are not rewritten, it is still an abstract class.
You cannot use abstract to modify attributes, private methods, constructors, static methods, and final methods.

abstract class A{
    
    
    abstract void m1( );
    public void m2( ){
    
    
        System.out.println("A类中定义的m2方法");
    }
}

class B extends A{
    
    
    void m1( ){
    
    
        System.out.println("B类中定义的m1方法");
    }
}

public class Test{
    
    
    public static void main( String args[ ] ){
    
    
        A a = new B( );
        a.m1( );
        a.m2( );
    }
}

interface

Sometimes you must derive a subclass from several classes and inherit all their properties and methods. However, Java does not support multiple inheritance. With interfaces, you can get the effect of multiple inheritance.
An interface is a collection of abstract methods and constant value definitions.
Essentially speaking, an interface is a special abstract class, which only contains the definition of constants and methods, but not the implementation of variables and methods.
Implement interface classes:
class SubClass implements InterfaceA{}
One class can implement multiple interfaces, and interfaces can also inherit other interfaces.

The characteristics of the
interface : use interface to define.
All member variables in the interface are decorated by public static final by default.
All methods in the interface are decorated by public abstract by default.
There is no constructor for the interface.
The interface uses a multi-layer inheritance mechanism.
Interface definition example

public interface Runner {
    
    
    int ID = 1;
    void start();
    public void run();
    void stop();
}

The class that implements the interface must provide the specific implementation content of all methods in the interface before it can be instantiated. Otherwise, it is still an abstract class.
The main purpose of the interface is to be implemented by the implementation class. (Interface-oriented programming)
Similar to the inheritance relationship, there is polymorphism between the interface and the implementation class.
Define the grammatical format of the Java class: write extends first, then implements
<modifier> class <name> [extends <superclass>]
[implements < interface> [,< interface>]*] { <declarations>* }

Interface method to simulate computer switch

package com.java;

public interface USB {
    
    
    public void On();
    public void Off();
}
package com.java;

public class KeyBoard implements USB {
    
    
    @Override
    public void On() {
    
    
        System.out.println("启动键盘");
    }

    @Override
    public void Off() {
    
    
        System.out.println("关闭键盘");
    }
}
package com.java;

public class Mouse implements USB {
    
    
    @Override
    public void On() {
    
    
        System.out.println("鼠标启动");
    }

    @Override
    public void Off() {
    
    
        System.out.println("鼠标关闭");
    }
}
package com.java;

public class Computer {
    
    
    private USB[] usbArr=new USB[4];
    //添加usb设备
    public void add(USB usb) {
    
    
        for(int i=0;i<usbArr.length;i++)
        {
    
    
            if(usbArr[i]==null)
            {
    
    
                usbArr[i]=usb;
                break;
            }
        }
    }
    //电脑开机
    public void powerOn() {
    
    
        //1.开启usb设备接口
        for(int i=0;i<usbArr.length;i++)
        {
    
    
            if(usbArr[i]!=null) {
    
    
                usbArr[i].On();
            }
        }
        //2.电脑开机
        System.out.println("开机成功");
    }
    //电脑关机
    public void powerOff() {
    
    
        //1.关闭usb设备
        for(int i=0;i<usbArr.length;i++)
        {
    
    
            if(usbArr[i]!=null) {
    
    
                usbArr[i].Off();
            }
        }
        //2.电脑关机
        System.out.println("关机成功");
    }
}
package com.java;

public class Tast {
    
    
    public static void main(String[] args) {
    
    
            // TODO Auto-generated method stub
            //创建电脑对象
            Computer com=new Computer();
            //像电脑中添加usb设备
            com.add(new Mouse());
            com.add(new KeyBoard());
            //电脑开机
            com.powerOn();
            System.out.println();
            //电脑关机
            com.powerOff();
            System.out.println();
        }

    }

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44909275/article/details/105271644