Interface: Characteristics of common behaviors (methods) of different things in the real world
is a special class, interface
modifier interface interface name {
//property--constant
//method--abstract method
}
All methods in the interface are abstract methods.
Attribute: public static final
Method: public abstract
1. Cannot be instantiated;
2. Subclass implements the interface. The subclass must implement all methods in the interface before it can create an object
3. Subclasses can implement multiple interfaces, and subclasses need interface method implementations to create objects
Application methods:
1. Abstract class --implements---》Interface;
Concrete class---extends --Abstract class;
2. Concrete class --extends--Abstract class---implements---》(Multiple) interfaces;
Class--Single inheritance--Class;
Class--Multiple implementations --Interface;
Interface--Inheritance--Interface;
interface a(); Interface is a special class
interface b();
interface c extends b,a{}
public class testShirt {
public static void main(String[] args){
Frock s = new Shirt();
s.toString();
Clothing c = new Shirt();
c.calcArea();
}
}
interface Clothing{ //Interface
public static final int size = 1;
public abstract void calcArea();
public abstract void getColor();
public abstract void getDate();
}
abstract class Frock{ //Abstract class inherits abstract method
public static final String color = "red";
public String toString(){
return color;
}
}
class Shirt extends Frock implements Clothing {//The concrete class inherits the abstract class and implements the interface
/*public abstract void calcArea();
public abstract void getColor();
public abstract void getDate();
public static final int size = 1;
public static final String color = "red";*/
int age;
public String toString(){
return super. toString()+age;
}
public void calcArea(){
System.out.println("....eat1");
}
public void getColor(){
System.out .println("....eat2");
}
public void getDate(){
System.out.println("....eat3");
}
}
Enumeration:
An enumeration is a special j class that defines a limited number of exhaustible data sets
Format:
modifier enum enumeration class name {
//data
}
get Data: class name.data
class test2 {
public static void main(String[] args){
Signal[] s = Signal.value();
Signal.red ;
} Signal.Green;
}
enum Signal{
red,Green,bule
}
Package (package): Tell the compiler which package the current class is in
package..Class name*; // Multiple paths can be written;
The names of each part of the package name are preferably lowercase letters;
The name of the class is also called the qualified name of the class;
Organization type. Company name. Project name. Function module
com.oracle.ucloud .view
import: Tell the compiler which package other classes used in the current class are located in;
import package name. Sub-package name;
Compile with package:
java -d .Location source file.java
jar Packaging
1. Create temporary file Main-Class: com.bbb.TestAbstractInterface /
2. Create jar file jar cmf temp.txt Person.jar ./
3. Execute file java -jar myprogram.jar