Java(day10) interface, internal class

interface

Interface is not a class

All interfaces are static constants without member variables. The properties of the interface are actually modified by public static final. Because of the static modification, they can be called directly with the interface name

package cn.tedu.Inter2;
//This class is used to further test the use of interfaces
public class TestUserInter {
    public static void main(String[] args) {
        System.out.println(UserInter.age);
    }
}
interface UserInter{
    int age=10;
}
Operation results:
10

All the methods in the interface are abstract methods, without ordinary methods or construction methods; And the method in the interface can omit public abstract

public abstract void eat();
void eat();

Interfaces can generate objects using polymorphic methods

package cn.tedu.inter;
//This class is used for interface testing
public interface Inter {
    public abstract void eat();
    public abstract void play();
}

package cn.tedu.inter;
//This class is the implementation class of the inter interface
public class InterImpl implements Inter{
    @Override
    public void eat() {
        System.out.println("Eat hot pot");
    }

    @Override
    public void play() {
        System.out.println("Type code");
    }
}

public class InterTest {
    public static void main(String[] args) {
        Inter i=new InterImpl();
        i.eat();
        i.play();

    }
}
Operation results:
Eat hot pot
 Type code

Interface can use interface implementation classes to generate objects

package cn.tedu.inter;
//This class is used for interface testing
public interface Inter {
    public abstract void eat();
    public abstract void play();
}

package cn.tedu.inter;
//This class is the implementation class of the inter interface
public class InterImpl implements Inter{
    @Override
    public void eat() {
        System.out.println("Eat hot pot");
    }

    @Override
    public void play() {
        System.out.println("Type code");
    }
}

public class InterTest {
    public static void main(String[] args) {
        InterImpl i1=new InterImpl();
        i1.eat();
        i1.play();
    }
}
Operation results:
Eat hot pot
 Type code

Interface implementation classes

There is a constructor in the interface implementation class, and the parent class is the top-level parent class object

After the relationship between the interface and the implementation class is established, there are two schemes:

1. Do not implement the abstract method in the interface and turn yourself into an abstract class

abstract public class InterImpl implements Inter{}

2. Implement all abstract methods in the interface and turn themselves into ordinary subclasses

public class InterImpl implements Inter{
    @Override
    public void eat() {  
    }
    @Override
    public void play() {
    }
}

Classes and interfaces

For Java classes, a class can only have one parent class and single inheritance; However, a class can implement multiple interfaces and multiple implementations\

Comparison between interface and abstract class:

Interface is the result of innate design, and abstraction is the result of acquired reconstruction

Abstract classes have construction methods, interfaces have no construction methods, and cannot be instantiated

Inner class

Creation of internal class objects

package cn.tedu.innerclass;

import cn.tedu.inter.Inter;

//Getting started with testing internal classes
public class TestInner1 {
    public static void main(String[] args) {
        //Creation of class objects within members
        Outer.Inner oi = new Outer().new Inner();
        System.out.println(oi.sum);
        oi.delete();
    }
}
class Outer{
    String name;
    private int age;
    public void find(){
        System.out.println("External class...find()method");
        
    }
    //Member inner class
    class Inner{
        int sum=10;
        public void delete(){
            System.out.println("Inner class delete()method");
            
        }
    }
}
Operation results:
10
 Inner class delete()method

Internal classes can directly use the resources of external classes, including private resources

package cn.tedu.innerclass;

import cn.tedu.inter.Inter;

//Getting started with testing internal classes
public class TestInner1 {
    public static void main(String[] args) {
        //Creation of class objects within members
        Outer.Inner oi = new Outer().new Inner();
        System.out.println(oi.sum);
        oi.delete();
    }
}
class Outer{
    String name;
    private int age;
    public void find(){
        System.out.println("External class...find()method");
        
    }
    //Member inner class
    class Inner{
        int sum=10;
        public void delete(){
            System.out.println("Inner class delete()method");
            System.out.println(name);
            System.out.println(age);
            find();
        }
    }
}
Operation results:
10
 Inner class delete()method
null
0
 External class...find()method

External classes can call internal resources by creating objects

package cn.tedu.innerclass;

import cn.tedu.inter.Inter;

//Getting started with testing internal classes
public class TestInner1 {
    public static void main(String[] args) {
        //Creation of class objects within members
        Outer.Inner oi = new Outer().new Inner();
        System.out.println(oi.sum);
        oi.delete();
    }
}
class Outer{
    String name;
    private int age;
    public void find(){
        System.out.println("External class...find()method");
        //Calling internal resources through objects
        Inner in=new Inner();
        System.out.println(in.sum);
        in.delete();
    }
    //Member inner class
    class Inner{
        int sum=10;
        public void delete(){
            System.out.println("Inner class delete()method");
        }
    }
}
Operation results:
10
 Inner class delete()method

The inner class of a member is decorated with private. You can create objects of the outer class and indirectly access the resources of the inner class

package cn.tedu.innerclass;
//The inner class of the test member of this class is decorated with private
public class TestInner2 {
    public static void main(String[] args) {
        new Outer2().getInner2Eat();
    }
}
class Outer2{
    public void getInner2Eat(){
        Inner2 in=new Inner2();
        in.eat();
    }
    private class Inner2{
        public void eat(){
            System.out.println("Inner class eat()method");
        }
    }
}
Operation results:
Inner class eat()method

Call when the inner class of a member is modified by static

package cn.tedu.innerclass;
//The test inner class is decorated with static
public class TestInner3 {
    public static void main(String[] args) {
        Outer3.Inner3 oi = new Outer3.Inner3();
        oi.show();
    }
}
class Outer3{
    static class Inner3{
        public void show(){
            System.out.println("Inner class show()method");
        }
    }
}
Operation results:
Inner class show()method

Member inner classes and methods in inner classes are modified by static

package cn.tedu.innerclass;
//The test inner class is decorated with static
public class TestInner3 {
    public static void main(String[] args) {
      
        Outer3.Inner3.show2();
    }
}  
class Outer3{
    static class Inner3{
        public void show(){
            System.out.println("Inner class show()method");
        }
        public static void show2(){
            System.out.println("I am a static method of a static inner class");
        }

    }
}

The calling of the local inner class needs to be under the local inner class

package cn.tedu.innerclass;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

//Used to test local inner classes
public class TestInner4 {
    public static void main(String[] args) {
        new Outer4().show();
    }
}
class Outer4{
    public void show(){
        System.out.println("I'm an external class show()method");
        class inner4{
            String name;
            int age;
            public void eat(){
                System.out.println("I'm a local inner class eat()");
            }
        }
        inner4 in = new inner4();
        System.out.println(in.age);
        System.out.println(in.name);
        in.eat();
    }
}
Operation results:
I'm an external class show()method
0
null
 I'm a local inner class eat()

Anonymous inner classes have no names and are usually used in conjunction with anonymous objects. Anonymous objects can only be used once, and only one function can be called at a time

package cn.tedu.innerclass;
//Used to test anonymous inner classes
public class TestInner5 {
    public static void main(String[] args) {
        new Inter1() {
            @Override
            public void save() {
                System.out.println("Implementation saving method");
            }
            @Override
            public void get() {
            }
        }.save();
        new Inter2() {
            @Override
            public void eat() {
                System.out.println("eat()method");
            }
        }.eat();
        new Inter3().study();
    }
}
interface Inter1{
    void save();
    void get();
}
abstract  class Inter2{
    public void play(){
        System.out.println("Play code");
    }
    public abstract void eat();
}
class Inter3{
    public void drink(){
        System.out.println("What is a happy planet");
    }
    public void study(){
        System.out.println("Learn to make money");
    }
}
Operation results:
Implementation saving method
eat()method
 Learn to make money

Keywords: Java Back-end

Added by PinkFloyd007 on Mon, 13 Dec 2021 13:07:45 +0200