Java Tutorial/Class Definition/Interface

Материал из Java эксперт
Перейти к: навигация, поиск

A Partial Interface Implementation

   <source lang="java">

interface Conversions {

 double INCH_TO_MM = 25.4;
 double OUNCE_TO_GRAM = 28.349523125;
 double POUND_TO_GRAM = 453.5924;
 double HP_TO_WATT = 745.7;
 double WATT_TO_HP = 1.0 / HP_TO_WATT;
 public double inchesToMillimeters(double inches);
 public double ouncesToGrams(double ounces);

}</source>





Encapsulating Constants in a Program

   <source lang="java">

interface ConversionFactors {

 double INCH_TO_MM = 25.4;
 double OUNCE_TO_GRAM = 28.349523125;
 double POUND_TO_GRAM = 453.5924;
 double HP_TO_WATT = 745.7;
 double WATT_TO_HP = 1.0 / HP_TO_WATT;

}</source>



25.4


Extending Interfaces

   <source lang="java">

interface ConversionFactors {

 double INCH_TO_MM = 25.4;

} interface Conversions extends ConversionFactors {

 double inchesToMillimeters(double inches);

}</source>





Fields and Methods in an Interface

  1. Fields in an interface must be initialized and are implicitly public, static, and final.
  2. You declare methods in an interface just as you would in a class.
  3. Methods in an interface do not have a body.
  4. All methods are implicitly public and abstract



   <source lang="java">

interface Conversions{

 double inchesToMillimeters(double inches);

}</source>





Initializing interface fields with non-constant initializers

   <source lang="java">

import java.util.Random; public class MainClass {

 public static void main(String[] args) {
   System.out.println(RandVals.randomInt);
   System.out.println(RandVals.randomLong);
   System.out.println(RandVals.randomFloat);
   System.out.println(RandVals.randomDouble);
 }

}

interface RandVals {

 Random rand = new Random();
 int randomInt = rand.nextInt(10);
 long randomLong = rand.nextLong() * 10;
 float randomFloat = rand.nextLong() * 10;
 double randomDouble = rand.nextDouble() * 10;

}</source>



3
-6942612165866507216
-8.1946244E18
5.045338828500432


Interfaces and Abstract Classes

  1. The interface should be regarded as a contract between a service provider and its clients.
  2. An abstract class is a class that cannot be instantiated
  3. An abstract class must be implemented by a subclass.
  4. In Java, the interface is a type.

Follow this format to write an interface:



   <source lang="java">

accessModifier interface interfaceName { } public interface Printable {

        void print (Object o);

}</source>



  1. The Printable interface has a method, print.
  2. print is public even though there is no public keyword.


Interfaces and Multiple Inheritance

   <source lang="java">

interface HisInterface { } interface HerInterface { } public interface MyInterface extends HisInterface, HerInterface { }</source>





Interfaces and Polymorphism: Using Multiple Interfaces

   <source lang="java">

interface ThisInterface {

 public void thisMethod();

} interface ThatInterface {

 public void thatMethod();

} class MyClass implements ThisInterface, ThatInterface {

 // Class definition including methods from both interfaces...
 public void thisMethod() {
   System.out.println("this");
 }
 public void thatMethod() {
   System.out.println("that");
 }

} public class MainClass {

 public static void main(String[] a) {
   MyClass cls = new MyClass();
   cls.thisMethod();
   cls.thatMethod();
 }

}</source>



this
that


Multiple interfaces

   <source lang="java">

interface A {

 void aMethod();

} interface B {

 void bMethod();

} interface C {

 void cMethod();

} class ClassA {

 public void aMethod() {
 }

} class D extends ClassA implements A, B, C {

 public void bMethod() {
 }
 public void cMethod() {
 }

} public class MainClass {

 public static void t(A x) {
   x.aMethod();
 }
 public static void u(B x) {
   x.bMethod();
 }
 public static void v(C x) {
   x.cMethod();
 }
 public static void w(ClassA x) {
   x.aMethod();
 }
 public static void main(String[] args) {
   D h = new D();
   t(h);
   u(h);
   v(h);
   w(h);
 }

}</source>





Nesting Classes in an Interface Definition

An inner class to an interface will be static and public by default.



   <source lang="java">

interface Port {

 // Methods & Constants declared in the interface...
 class Info {
   // Definition of the class...
 }

} public class MainClass {

 public static void main(String[] a) {
   Port.Info info = new Port.Info();
 }

}</source>





To implement an interface: use the implements keyword after the class declaration

   <source lang="java">

public class CanonDriver implements Printable {

        public void print (Object obj) {
            // code that does the printing
        }

}</source>



  1. An implementation class has to override all methods in the interface.
  2. A class can implement multiple interfaces.