Interface
adopt interface Keyword definition interface
Class passing implements Implementation interface
Interface cannot be instantiated
Interfaces are all abstract methods
Abstract methods in interfaces can be omitted public abstract keyword
The implementation class must implement all the abstract methods in the interface
Either it's an abstract class
stay jdk1.8 Allow common methods to appear in the interface , Ask to be static perhaps default modification
// static public void hi() {}
// default public void hi() {}
public class Test1_Interface {
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo demo=new DemoImpl();
demo.eat();
demo.game();
}
}
interface Demo{
void eat();
void game();
}
class DemoImpl implements Demo{
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("eat()...");
}
@Override
public void game() {
// TODO Auto-generated method stub
System.out.println("game()...");
}
}
Usage of interface
There is no constructor in the interface / No variables are constants
Methods are abstract (jdk1.8 There can be special common methods )
The constants in the interface can be abbreviated , It will automatically splice public static final
The method in the interface can be abbreviated , It will automatically splice public abstract
public class Test2_Interface2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Inter in=new InterImpl();
System.out.println(in.age);
System.out.println( Inter.age );// Get value ,age yes static Of
//TODO 7, Test the method in the interface
String desc = in.save();
System.out.println(desc);
in.delete(10);
}
}
interface Inter{
int age=40;
String save();
void delete(int id);
}
class InterImpl implements Inter{
@Override
public String save() {
// TODO Auto-generated method stub
return "success";
}
@Override
public void delete(int id) {
// TODO Auto-generated method stub
System.out.println("delete()..."+id);
}
}
Complex use of interfaces
Interfaces can inherit more ,
Implementation classes and interfaces can be implemented in more than one way
public class Test3_Interface3 {
public static void main(String[] args) {
//4,--2 The function of interface No , There are so many interfaces , Whose function do you want to use , On the left, write who
Inter2 in = new Inter2Impl() ;
in.update();
in.save();
in.delete(5);
in.get();
}
}
interface Inter1{
void save();
}
interface Inter3{
void update();
void get();
}
//5, Implementation classes can inherit at the same time , Multiple implementation
abstract class Impl2 extends Object implements Inter1 , Inter3{
}
//3, Interface and implementation class -- It's about realizing relationships , It can be realized more ( commas )
//--Impl Implementation class , At the same time Inter1 and Inter3 Function of interface ,
//-- Multiple abstract methods need to be rewritten at the same time , Otherwise, it's an abstract class
class Impl implements Inter1 , Inter3{
@Override
public void update() {
}
@Override
public void get() {
}
@Override
public void save() {
}
}
//1, Interface and interface -- It's inheritance , You can inherit more ( commas )
//--2 Interface No. 1 also uses 1 Number and 3 The function of interface No !
interface Inter2 extends Inter1 , Inter3{
void delete(int id) ;
}
//2, Want to use 2 The function of interface No , Find an implementation class to implement the interface
class Inter2Impl implements Inter2{
@Override
public void save() {
System.out.println(1);
}
@Override
public void update() {
System.out.println(2);
}
@Override
public void get() {
System.out.println(3);
}
@Override
public void delete(int id) {
System.out.println(4);
}
}
Four , The difference between interface and abstract class
--1, Classes and relationships between classes : It's inheritance
--java It only supports single root inheritance
--class A extends B
-- among A Subclass ,B Parent class . The subclass can use all the functions of the parent class
-- Method rewrite override: If you want to modify the original function of the parent class , You can do method rewriting
--2, Interface and the relationship between interfaces : It's inheritance
-- Interface is to break through java Limitations of single inheritance
-- Interfaces can be inherited from each other
--interface A extends B , C
-- among A It's a subinterface ,B and C It's the parent interface
--A You have B and C All functions of ,A Is the most comprehensive
--class Impl implements A
--Impl The implementation class needs to be rewritten at the same time A B C All the abstract methods in the interface , Otherwise, it's an abstract class
--3, The relationship between classes and interfaces : It's about realizing relationships
-- Implementation classes can implement interfaces , And it can be realized more
--class A implements B,C
--A It's the implementation class ,B and C It's the interface .
--A It needs to be rewritten at the same time B and C All the abstract methods in the interface , Otherwise, it's an abstract class
-- Class can implement more than one implementation while inheriting
--class A extends B implements C , D
--A You can call it a subclass , It can also be called an implementation class
--A At the same time, it has a parent class B The function of , It also has an interface C and D The function of
--A It needs to be rewritten at the same time C and D All the abstract methods in the interface , Otherwise, it's an abstract class
-- For the parent class B The method in ? See the demand ,B Class is the parent class , If it's all ordinary methods , Rewriting occurs only if it needs to be modified .
--4, The difference between abstract classes and interfaces
-- The same thing
-- Abstract classes and interfaces are abstract layers , It is generally used to extract commonness
-- Can't be instantiated
-- Difference
-- Construction method : In the abstract class there is , Not in the interface !!
-- Member variables : In the abstract class there is , Not in the interface , Interfaces are constants !!
-- Member method : Abstract classes can have ordinary methods and abstract methods , But the interfaces are all abstract methods (1.8 There can be )
-- There are shorthand forms in the interface , There is no abstract class .
-- Constant in interface ,int age = 10; Will automatically splice constants public static final
-- Methods in the interface ,void save(); Will automatically splice the method public abstract
-- How to design your abstraction layer , Is it an abstract class or an interface ?
-- The key depends on whether you need to provide a method body , If no method in a class needs to provide a method body , Can be designed as an interface . If a method in a class needs to provide a method , Designed as an abstract class .
-- Is the abstract layer designed as an abstract class or an interface , Who's good ?
-- If the implementation class wants to have multiple functions at the same time , It's better to choose the interface , Because interfaces can inherit and implement more
-- If it is designed as an abstract class , A subclass can only inherit one parent class , This is the only right to inherit
5、 ... and , Programming :
-- demand :
Concrete things : Teacher of Peiyou class , Master class teacher
Generality : Prepare lessons / lecture
test : Creating polymorphic object tests
-- Using the idea of interface oriented programming :
public class Test4_Design {
public static void main(String[] args) {
Teacher cgb=new cgbteacher();
cgb.ready();
cgb.teach();
}
}
interface Teacher{
void ready();
void teach();
}
class cgbteacher implements Teacher{
@Override
public void ready() {
// TODO Auto-generated method stub
System.out.println(" The teacher of Peiyou class is Prepare lessons ");
}
@Override
public void teach() {
// TODO Auto-generated method stub
System.out.println(" The teacher of Peiyou class is lecture ");
}
}
abstract class actteacher implements Teacher{
}