java learning notes week 3

catalogue

1, Abstract classes and interfaces

1. Abstract methods and abstract classes

2. Interface (key)

2.1 function of interface

2.2 how to define and use interfaces (jdk8 before)

2.3 define static methods and default methods in the interface (jdk8 later)

2.4 multi inheritance of interfaces

2, Detailed explanation of String class

1. String basis

2. String judgment equality

3. Common methods of String class (to master)

2, Inner class

1. Inner class

1.1 non static internal class

1.2 static internal class

1.3 anonymous inner class

 

1, Abstract classes and interfaces

1. Abstract methods and abstract classes

Abstract method: the method modified with abstract has no method body, only declaration (public void s()); No {}). The definition is a "specification", which tells subclasses that they must provide concrete implementations for abstract methods.

Abstract class: a class containing abstract methods is an abstract class. The specification is defined through the abstract method, and then the subclass must define the specific implementation. By abstracting classes, we can strictly limit the design of subclasses and make subclasses more common.

package studyweek3;
//abstract class
abstract class Animal{
    abstract public void shout();
}
class Dog extends Animal{
    //The subclass must implement the abstract method of the parent class, otherwise the compilation error
    public void shout(){
        System.out.println("Woof");
    }
    public void seeDoor(){
        System.out.println("Watch the door");
    }
}
//Test abstract class
public class study1 {
    public static void main(String[] args) {
        //Animal a=new Animal();// Abstract classes cannot be instantiated, so new objects cannot be.
        /*Animal b=new Dog();
        b.shout(); Correct, you can use up transformation new to create an object*/
        Dog a=new Dog();
        /*Animal c=new Dog();
        //c.seeDoor();// Error: the Animal class is used when compiling, but there is no seedoor method in the Animal class, so the compilation cannot pass
        a.shout();
        a.seeDoor();
    }
}

Key points for using abstract classes:

2. Interface (key)

2.1 function of interface

Why do I need an interface? What is the difference between interfaces and abstract classes?

difference:

General class: concrete implementation

Abstract class: concrete implementation, specification (abstract method)

Interfaces: Specifications

2.2 how to define and use interfaces (jdk8 before)

Declaration format:

[access modifier] interface interface name []{

Constant definition// It is always decorated with public static final, even if it is not written.

Method definition// Always public abstract

}

Interface example:

package studyweek3;
//Interface (usually with a lot of comments)

//This is a flying interface
public interface interface2 {
    /*Flight distance*/
    public static final int a=100;
    /*Flight method*/
    void fly();
    /*Stop method*/
    void stop();
}



package studyweek3;

public class study2 implements interface2{
    @Override
    public void fly(){
        System.out.println("fly");
    }
    @Override
    public void stop(){
        System.out.println("stop");
    }

    public static void main(String[] args) {
        study2 a1=new study2();
        a1.fly();
        a1.stop();
    }

}

When transferring parameters, it is called directly through the interface to facilitate management.

2.3 define static methods and default methods in the interface (jdk8 later)

Default method:

interface A{
    default void moren(){
        System.out.println("Default method in interface")            
    }
}

Static method:

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

interface A{
public static void st(){
        System.out.println("a Static method of");
    }
}
class test_a implements A{
    public static void st(){
        System.out.println("test_a Static method of");
    }
}

2.4 multi inheritance of interfaces

Interfaces can support multiple inheritance.

Syntax:

interface A extends b,c;

Extensions can only inherit single, while interfaces can be used to implement multiple inheritance

2, Detailed explanation of String class

1. String basis

2. String judgment equality

package studyweek3;
//Detailed explanation of String class
public class study3 {
    public static void main(String[] args) {
        String str1=new String("abcde");
        String str2="abcde";
        System.out.println(str1==str2);//"abcde" has a constant area. str1 points to the address of the new object, and does the address of the object point to the address of the constant area? str2 directly points to the address of "abcde" in the constant area
        System.out.println(str1.equals(str2));
    }
}

Therefore, when it comes to string comparison, the equals method is used.

str1.equals(str2);

3. Common methods of String class (to master)

 

package studyweek3;

import java.util.Locale;

//Detailed explanation of String class
public class study3 {
    public static void main(String[] args) {
        String str1=new String("abcde");
        String str2="abcde";
        String str3="abcdE";
        String str4="abc";

        System.out.println(str1==str2);//"abcde" has a constant area. str1 points to the address of the new object, and does the address of the object point to the address of the constant area? str2 directly points to the address of "abcde" in the constant area
        System.out.println(str1.equals(str2));

        //String exercise
        System.out.println(str1.length());//Return string length
        System.out.println(str2.charAt(0));//Returns the 0th character
        System.out.println(str2.charAt(4));//Returns the sixth character. str2.length()-1;

        System.out.println(str2.equals(str3));//false
        System.out.println(str2.equalsIgnoreCase(str3));//true ignore uppercase

        System.out.println(str2.indexOf(str4));//Find the substring from scratch and return the subscript 0 of the first substring's initial letter
        System.out.println("Aabcdefabca".indexOf("abc"));//1
        System.out.println("abc".indexOf("A"));//- 1 if not found

        System.out.println("Aabcdefabca".lastIndexOf("abc"));//Search for a string from the end and return the first found string's initial subscript 7
        System.out.println("abc".lastIndexOf("A"));//- 1 if not found

        //String substitution
        String str5="abcdbcd";
        String str6=str5.replace('a','A');//Replace all d with D
        System.out.println(str6);
        String str7="abcdeee".replace("eee","EEE");
        System.out.println(str7);

        System.out.println("javaccc".startsWith("java"));//Determine whether to start with a string true
        System.out.println("javaccc".endsWith("java"));//Determine whether to end with string false

        //Intercept substring (header but not footer)
        String str8="abcdefghi".substring(5);//Returns a new string from the intercept position to the end of the string. fhgi
        System.out.println(str8);
        String str9="abcdefghijk".substring(5,8);//Returns a new string, ending at - 1 after the intercept position. FGH (subscripts 5 to 7)
        System.out.println(str9);

        System.out.println("abcDe".toUpperCase());//String uppercase
        System.out.println("ABCDe".toLowerCase());//String becomes lowercase

        //Remove the leading and trailing spaces, and generally do not go in the middle
        String str10="  a b  ";//Length 7
        String str11=str10.trim();
        System.out.println(str11.length());
        System.out.println(str11);
        //If you want to go to the middle space:
        System.out.println(str10.replace(" ",""));
        System.out.println(str10);//unchanged

    }
}

Note: String is an immutable character sequence. All replacement, substring interception, space removal, case conversion, etc. generate a new String

Quote other people's articles to subdivide this parthttps://blog.csdn.net/qq_35046158/article/details/80273010

2, Inner class

1. Inner class

package studyweek3;
//External class
public class study4 {
    private int age=10;
    private void show(){
        System.out.println("Exhibition");
    }
    //Inner class
    public class Inner{
        private String name="tony";
        private int age=20;

        public void showInner(){
            System.out.println("Inner Exhibition");
            System.out.println(age);
            System.out.println(study4.this.age);//When the external class attribute and the internal class attribute have the same name, you can use the external class name this. Member name call
            show();//Inner classes can use members of outer classes directly
        }
    }

    public static void main(String[] args) {
        study4.Inner inner01=new study4().new Inner();
        inner01.showInner();

        study4 out02=new study4();

1.1 non static internal class

1.2 static internal class

Definition method: static class ClassName{

Class body

Key points of use:

1. The static inner class can access the static properties of the external class, but cannot access the ordinary members of the external class.

2. The static inner class is regarded as a static member of the outer class.

1.3 anonymous inner class

The type that is suitable for use only once. For example: keyboard monitoring operation, etc. It is common in Android development, awt and swing development.

Syntax: new parent class constructor (argument class table) \ implementation interface (){

Anonymous inner class body

}

package studyweek3;
public class study5 {
    public void run(jiekou a){
        a.show();
    }
    public static void main(String[] args) {
        study5 b = new study5();
       /* AA c=new AA();
        b.run(c);*///Pass parameter and class name
        b.run(new jiekou(){//Anonymous inner class, which calls a new class every time it is used. Note the format: here is the parameter transmission interface,
            @Override
            public void show() {
                System.out.println("run");
            }
        }
        );
    }
}

//Classes with names can be used repeatedly.
class AA implements jiekou{
    @Override
    public void show(){
        System.out.println("run");
    }
}
interface jiekou{
    void show();
}

Keywords: Java Back-end

Added by phertzog on Wed, 02 Feb 2022 18:03:48 +0200