"Go" JAVA interface

Source: Internet
Author: User

1. Define the interface
Use interface to define an interface. Interface definition similar to the definition of the same, is also divided into the interface of the Declaration and interface body, where the interface body consists of a constant definition and method definition two parts. The basic format for defining an interface is as follows:

[Modifier] Interface Interface Name [extends parent interface List]{

[public] [static ] [final] constant;

modifier: optional, to specify access permissions for the interface, the optional value is public. If omitted, the default access rights are used.
interface name: A required parameter that specifies the name of the interface, and the interface name must be a valid Java identifier. In general, the first letter is required to capitalize.
extends parent interface names: Optional parameter that specifies 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 methods in the interface are only defined and not implemented.

For example, define an interface for calculations that defines a constant pi and two methods in that interface, with the following code:

[Java]View Plaincopyprint?
    1. public interface  calinterface   
    2. {  
    3.      final float pi=3.14159f;//defines the constant pi    used to represent pi;
    4.     float getarea (float r);
    5.     float getcircumference (float r);
    6. }  
public interface Calinterface {Final float pi=3.14159f;//defines a constant pi float Getarea for pi (float R);//define a method for calculating the area getarea () float getcircumference (float R);//define a method for calculating the perimeter getcircumference ()} 

< Span style= "color: #333333;" > Note:
    As with Java class files, the file name of the interface files must be the same as the interface name. After the
Implement Interface
interface is defined, you can implement the interface in the class. Implementing an interface in a class can use the keyword implements, which has the following basic format:
[modifier] class < class name > [extends parent class name] [Implements interface list]{
}
modifier: strong> Optional parameters that specify access permissions for the class, with the optional values public, abstract, and final.
class Name: required parameter, which specifies the name of the class, and the class name must be a valid Java identifier. In general, the first letter is required to capitalize.
extends the parent class name : 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.
Implements interface list: 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.
    When implementing an interface in a class, the name of the method, the type of return value, the number of parameters and the type must be exactly the same as in the interface, and all methods in the interface must be implemented. For example, write a class called Cire, which implements the interface calculate defined in section 5.7.1, with the following code:

[Java]View Plaincopyprint?
  1. Public class Cire implements Calinterface
  2. {
  3. Public float Getarea (float R)
  4. {
  5. float area=pi*r*r; //Calculate the area of the circle and assign the value to the variable areas
  6. return area; //Returns the calculated Circle area
  7. }
  8. Public float getcircumference (float R)
  9. {
  10. float circumference=2*pi*r; //Calculate the perimeter of the circle and assign a value to the variable circumference
  11. return circumference; //Returns the circumference length after calculation
  12. }
  13. public static void Main (string[] args)
  14. {
  15. Cire C = new Cire ();
  16. float f = c.getarea (2.0f);
  17. System.out.println (Float.tostring (f));
  18. }
  19. }
public class Cire implements Calinterface {public float Getarea (float r) {FL        Oat area=pi*r*r;//calculates the circle area and assigns a value to the variable areas return area;//returns the calculated Circle area. public float getcircumference (float r) {      float Circumference=2*pi*r;           Calculates the circumference of a circle and assigns a value to the variable circumference return circumference;        Returns the computed circumference long} public static void Main (string[] args) {Cire c = new Cire ();        float f = c.getarea (2.0f);    System.out.println (Float.tostring (f)); }}

In the inheritance of the class, only single-inheritance can be done, but when implementing the interface, it is possible to implement multiple interfaces at a time, separating each interface with a comma ",". In this case, there may be a constant or method name conflict, when resolving the problem, if the constant conflict, you need to explicitly specify the interface of the constant, which can be achieved through the "interface name. Constant". If a method conflict occurs, only one method can be implemented. Here is a concrete example of how to solve the above problems in detail.
Transferred from: http://blog.csdn.net/yjkwf/article/details/7238847

"Go" JAVA interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.