Java Fundamentals 14 Object Oriented ~ Interface and Inner Class

Like you who love programming!
Learn SpringBoot actual combat course https://edu.csdn.net/course/detail/31433
Learn SpringCloud introductory course https://edu.csdn.net/course/detail/31451


Object-oriented series:
class and object
encapsulation
inherit
polymorphic
static keyword and singleton mode
interface and inner class

Preface

Hello, everyone. Interface is an important type in Java. In actual project development, a large number of interfaces will be used, called: interface-oriented programming. This chapter will focus on interfaces and internal classes.

interface

What is the interface

Insert picture description here
The interface in life,
such as the USB interface on the computer, regulates the shape and size of the interface. As long as the device conforms to the standard, it can be connected.

The interface in the program
is a set of specifications, which stipulates the definition of the method, as long as the class that implements the interface must be defined in accordance with the specification of the interface.

Use interface

The syntax for defining the interface:

public interface 接口名{
	静态常量的定义;
	方法的定义;
}

Note when defining the interface:

  1. The method is abstract and cannot be realized
  2. The defined properties will be automatically converted to static constants
  3. The method can only be public, the default is public

Implement interface

public class 类名 implements 接口名{
	具体方法的实现
}

Pay attention to the implementation of the interface:

  1. All methods in the interface must be implemented
  2. The method must be exactly the same as defined in the interface
  3. Method must be public
  4. A class can implement multiple interfaces
  5. A class can inherit the class while implementing the interface
class 类名 extends 父类 implements 接口{
}
  1. Interface can inherit interface, the implementation class must implement all methods
interface A{
	void testA();
}
//接口之间的继承
interface B extends A{
	void testB();
}
class C implements B{
	public void testA() {
	}
	public void testB() {
	}
}

The significance of the interface in development

  1. Develop a set of specifications for the realization of the class
  2. Separate design and implementation

default keyword

A new feature of Java 1.8, methods defined by default can have default implementations.

public interface Person {
	//给接口方法默认的实现
	default void eat(){
		System.out.println("在吃饭!!");
	}
	void walk();
}

The implementation class is not mandatory to implement methods with default.

Similarities and differences between interfaces and abstract classes

The difference between interfaces and abstract classes are common interview questions. Similarities
:

  1. There may be unimplemented methods
  2. Can not be instantiated

difference:

  1. Abstract class is a single inheritance, the class can implement multiple interfaces
  2. Interface cannot define a constructor
  3. The methods in the interface can only be public, and abstract classes can have various access types.
  4. Only static constants can be defined in interfaces, and ordinary member variables can be defined in abstract classes.
  5. Abstract methods in the interface do not need to be added, abstract classes must be added.

Inner class

The inner class is the class defined in the class.
Java's internal classes include:

  • Member inner class
  • Static inner class
  • Local inner class
  • Anonymous inner class

Member inner class

Same level as member variables (attributes) and member methods are owned by a certain object.
grammar:

class 外部类{
	class 内部类{
	...
	}
}

Features:

  • Generally only used in external classes
  • The attributes and methods of the inner class and the class are the same level.
  • Objects belonging to the class are loaded into memory when each object is created

Static inner class

Adding static to the member inner class is the static inner class
Syntax:

public class 外部类{
	static class 内部类{
	}
}

The difference with the member inner class:

  1. Member inner class belongs to object, static inner class belongs to outer class
  2. Member inner class is loaded every time an object is created, static inner class and outer class are loaded together
  3. The member inner class can access all members of the outer class, and the static inner class can only access the static members of the outer class

Local inner class

The class defined in the method can only be used in the current method, and the class is loaded into memory when the method is called.
grammar:

class 外部类{
	public void test(){
		class 内部类{
		
		}
		内部类 对象 = new 内部类();
	}
}

Anonymous inner class

An inner class without a name, an object is created when the class is created.
Application: The implementation class of the interface or abstract class only needs to be used once, and the code is one-time.
grammar:

new 接口/抽象类()
{
	实现方法
};

Implement USB case

//Usb接口
interface Usb{
	void connect(); //连接
	void charge(); //充电
}
//匿名内部类实现接口
Usb usb = new Usb(){
	public void connect(){
		System.out.println("Test2测试连接");
	}
	public void charge(){
		System.out.println("Test2测试充电");
	}
};
usb .connect();
usb .charge();

The difference from the general class:

  1. no name
  2. Can only be used once
  3. No construction method
  4. Cannot define static members

Interview question: in? Fill in what can be output 30, 20, 10 in the console.

class Outer {
 	public int num = 10;
     class Inner {
         public int num = 20;
         public void show() {
             int num = 30;
             System.out.println(?);   	num
             System.out.println(?);   	this.num
             System.out.println(?); 	Outer.this.num
         }
     }
 }

 class InnerClassTest {
     public static void main(String[] args) {
         Outer.Inner oi = new Outer().new Inner();
         oi.show();
     }    
 }   

End

Okay, has everyone mastered this article? Welcome to leave a message for discussion.
Leave two assignments:

  1. Define the player interface and play music method; define the game console interface and play the game method;
    define the phone class, which has brand and price attributes, and call methods.
    Define the mobile phone class to inherit the phone and implement the player and game console interfaces
  2. Define the arithmetic interface and define four methods of addition, subtraction, multiplication, and division. Each method has two double parameters and the return value is double.
    Use regular classes and anonymous inner classes to implement interfaces.

If you need to learn other Java knowledge, poke here ultra-detailed knowledge of Java Summary

Guess you like

Origin blog.csdn.net/u013343114/article/details/112523663