Interface:
[Modifier] Interface Interface Name [extends parent interface name 1, parent interface Name 2, ...] {
[Public] [Static] [final] type variable;
[Public] [Abstract] method;
}
modifier, an optional parameter that specifies the access rights for the interface, and the optional value is public. If omitted, the default access rights are used.
Interface name, required parameter, to specify the name of the interface, and the interface name must be a valid Java identifier. The general first letter is capitalized.
A list of extends parent interface names, optional parameters that specify which parent interface the interface to define inherits from. When you use the extends keyword, the parent interface name is a required parameter.
Method, the method in the interface is only defined and not implemented.
eg
public interface calculate{
float pi = 3.14;
Float Getarea (float R);
}
Equivalent to:
public interface calculate{
Public fianl float pi = 3.14;
public abstract float Getarea (float R);
}
Implementation of the interface:
[Modifier]class class name [Extends parent class name] [Implements interface list]{
}
modifier, an optional parameter that specifies the access rights for the class, and the optional value is public, abstract, final.
Class name, a required parameter that specifies the name of the class, and the class name must be a valid Java identifier. In general, the first letter is capitalized.
Extends the parent class name, an optional parameter that specifies which parent class the class to define inherits from. When you use the extends keyword, the parent class name is a required parameter.
The Implements interface list, an optional parameter that specifies which interfaces the class implements. When using the Implements keyword, the interface list is a required parameter. When multiple interface names exist in the interface list, each interface name is separated by commas.
eg
public interface calculate{
float pi = 3.14;
Float Getarea (float R);
}
public class Circle implements calculate{
public float Getarea (float r) {
return pi * R * r;
}
}
Implementation of Inheritance:
[modifier] class subclass name extends parent class name {
}
modifier, an optional parameter that specifies the access rights for the class, and the optional value is public, abstract, final.
Subclass name, required parameter, to specify the name of the subclass, and the class name must be a valid Java identifier. In general, the first letter is capitalized.
Extends the parent class name, a required parameter that specifies which parent class the subclass to define inherits from.
eg
public class bird{
protected String color;
protected String skin;
}
public class extends bird{
public void Fly () {
}
}
Abstract class:
Abstract class class Name {
}
eg
Public abstract class fruit{
protected String color;
Public Fruit () {
color = "green";
}
}
Abstract method (cannot use private or static keyword decoration, because to be overridden in a subclass, the method cannot be called directly with the class name)
Abstact < method return value type > method name (parameter list);
eg
Public abstract class fruit{
protected String color;
Public Fruit () {
color = "green";
}
public abstract void Harvest ();
}
member Inner class (an inner class can only be used in an external class, generally as a member variable of an outer class, an instantiation of an inner class must be in a non-static method in an outer class or external class; An inner class can access an external class member)
public class outerclass{
Private Class innerclass{
}
}
eg
public class outerclass{
private int i = 0;
private void G () {
}
Private innerclass{
void F () {
g ();
i++;
}
}
}
The local inner class (the class defined in the method of the class, and his scope is also within the method body)
eg
public class selloutclass{
private String name;
Public Selloutclass () {
Name = "Apple";
}
public void sell (double price) {
Class apple{
Double Price;
Public Apple (double price) {
This.price = Price;
}
public void Price () {
System.out.println ("Start selling Now" + name);
SYSTEM.OUT.PRINTLN ("Unit Price:" + prices);
}
}
Apple a = new Apple (price);
A.price ();
}
}
Anonymous inner class (an anonymous inner class, which is an inner class without a name, because there is no name, the anonymous inner class can only be used once, and it is often used to simplify code writing; But there is a precondition for using anonymous inner classes: You must inherit a parent class or implement an interface)
EG1: Do not use anonymous inner classes to implement abstract methods
public abstract class Person {
public abstract void Eat ();
}
public class Child extends person {
public void Eat () {
System.out.println ("Eat something");
}
}
public class Test {
public static void Main (string[] args) {
Person p = new Child ();
P.eat ();
}
}
Running result: eat something
As you can see, we inherit the person class with child, and then we implement an instance of child and transform it upward into a reference to the person class. However, if the child class is used only once, it is wasteful to write it as a separate class. The anonymous inner class is introduced at this time.
EG2: The basic implementation of anonymous inner classes
public abstract class Person {
public abstract void Eat ();
}
public class Test {
public static void Main (string[] args) {
Person p = new person () {
public void Eat () {
System.out.println ("Eat something");
}
};
P.eat ();
}
}
Running result: eat something
As you can see, we directly implement the method in the abstract class person in curly braces so that the writing of a class can be omitted, and the anonymous inner class can also be used on the interface.
EG3: Using anonymous inner classes on an interface
Public interface Person {
public abstract void Eat ();
}
public class Test {
public static void Main (string[] args) {
Person p = new person () {
public void Eat () {
System.out.println ("Eat something");
}
};
P.eat ();
}
}
Running result: eat something
As can be seen from the above example, as long as a class is abstract or an interface, the methods in its subclasses can be implemented using anonymous inner classes, although subclasses of generic classes can also be implemented using anonymous inner classes.
The most common situation is in the implementation of multithreading, because to implement multithreading must inherit the thread class or inherit the Runnable interface.
Anonymous inner class implementation of the Eg4:thread class
public class Test {
public static void Main (string[] args) {
Thread t = new Thread () {
public void Run () {
for (int i = 1; I <= 3; i++) {
System.out.print (i + "");
}
}
};
T.start ();
}
}
Run Result: 1 2 3
Anonymous inner class implementation of the Eg5:runnable interface
public class Test {
public static void Main (string[] args) {
Runnable r = new Runnable () {
public void Run () {
for (int i = 1; I <= 3; i++) {
System.out.print (i + "");
}
}
};
Thread t = new Thread (r);
T.start ();
}
}
Run Result: 1 2 3
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java interfaces, classes