Java Tutorial/Class Definition/Interface

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

A Partial Interface Implementation

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);
}





Encapsulating Constants in a Program

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;
}



25.4


Extending Interfaces

interface ConversionFactors {
  double INCH_TO_MM = 25.4;
}
interface Conversions extends ConversionFactors {
  double inchesToMillimeters(double inches);
}





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



interface Conversions{
  double inchesToMillimeters(double inches);
}





Initializing interface fields with non-constant initializers

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;
}



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:



accessModifier interface interfaceName {
}
public interface Printable {
         void print (Object o);
}



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


Interfaces and Multiple Inheritance

interface HisInterface {
}
interface HerInterface {
}
public interface MyInterface extends HisInterface, HerInterface {
}





Interfaces and Polymorphism: Using Multiple Interfaces

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();
  }
}



this
that


Multiple interfaces

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);
  }
}





Nesting Classes in an Interface Definition

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



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();
  }
}





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

public class CanonDriver implements Printable {
         public void print (Object obj) {
             // code that does the printing
         }
}



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