Java/Language Basics/Static Import

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

Java static import enum

   <source lang="java">

/* License for Java 1.5 "Tiger": A Developer"s Notebook

    (O"Reilly) example package

Java 1.5 "Tiger": A Developer"s Notebook (O"Reilly) by Brett McLaughlin and David Flanagan. ISBN: 0-596-00738-8 You can use the examples and the source code any way you want, but please include a reference to where it comes from if you use it in your own products or services. Also note that this software is provided by the author "as is", with no expressed or implied warranties. In no event shall the author be liable for any direct or indirect damages arising in any way out of the use of this software.

  • /

import static java.lang.System.out; import static Grade.*; import java.io.IOException; import java.io.PrintStream;

public class EnumImporter {

 private Student[] students = new Student[4];
 public EnumImporter() {
   students[0] = new Student("Brett", "McLaughlin");
   students[0].assignGrade(A);
   students[1] = new Student("Leigh", "McLaughlin");
   students[0].assignGrade(B);
   students[2] = new Student("Dean", "McLaughlin");
   students[0].assignGrade(C);
   students[3] = new Student("Robbie", "McLaughlin");
   students[0].assignGrade(INCOMPLETE);
 }
 public void printGrades(PrintStream out) throws IOException {
   for (Student student : students) {
     if ((student.getGrade() == INCOMPLETE) || 
         (student.getGrade() == D)) {
       // Make this student retake this class
     }
   }
 }
 public static void main(String[] args) {
   try {
     EnumImporter importer = new EnumImporter();
     importer.printGrades(out);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }

}

enum Grade { A, B, C, D, F, INCOMPLETE };

class Student {

 private String firstName;
 private String lastName;
 private Grade grade;
 public Student(String firstName, String lastName) {
   this.firstName = firstName;
   this.lastName = lastName;
 }
 public void setFirstName(String firstName) {
   this.firstName = firstName;
 }
 public String getFirstName() {
   return firstName;
 }
 public void setLastName(String lastName) {
   this.lastName = lastName;
 }
 public String getLastName() {
   return lastName;
 }
 public String getFullName() {
   return new StringBuffer(firstName)
          .append(" ")
          .append(lastName)
          .toString();
 }
 public void assignGrade(Grade grade) {
   this.grade = grade;
 }
 public Grade getGrade() {
   return grade;
 }

}


      </source>
   
  
 
  



Java static import: old way

   <source lang="java">

public class Hypotenuse {

 public static void main(String args[]) {
   double side1, side2;
   double hypot;
   side1 = 3.0;
   side2 = 4.0;
   hypot = Math.sqrt(Math.pow(side1, 2) + Math.pow(side2, 2));
   System.out.println("Given sides of lengths " + side1 + " and " + side2 + " the hypotenuse is "
       + hypot);
 }

}


      </source>
   
  
 
  



Java static import: System.out and err

   <source lang="java">

/* License for Java 1.5 "Tiger": A Developer"s Notebook

    (O"Reilly) example package

Java 1.5 "Tiger": A Developer"s Notebook (O"Reilly) by Brett McLaughlin and David Flanagan. ISBN: 0-596-00738-8 You can use the examples and the source code any way you want, but please include a reference to where it comes from if you use it in your own products or services. Also note that this software is provided by the author "as is", with no expressed or implied warranties. In no event shall the author be liable for any direct or indirect damages arising in any way out of the use of this software.

  • /

import static java.lang.System.err; import static java.lang.System.out; import java.io.IOException; import java.io.PrintStream; public class StaticImporter {

 public static void writeError(PrintStream err, String msg) 
   throws IOException {
  
   // Note that err in the parameter list overshadows the imported err
   err.println(msg); 
 }
 public static void main(String[] args) {
   out.println("Good morning, " + "jexp");
   out.println("Have a day!");
   try {
     writeError(System.out, "Error occurred.");
   } catch (IOException e) {
     e.printStackTrace();
   }
 }

}

      </source>
   
  
 
  



Static Import

   <source lang="java">

////////Fruits.java package MyConstants; public class Fruits {

   public static int apple = 500;
   public static int pear = 501;
   public static int orange = 502;
   public static int banana = 503;
   public static int strawberry = 504;

}

////////////Colors.java package MyConstants; public class Colors {

   public static int white = 0;
   public static int black = 1;
   public static int red = 2;
   public static int blue = 3;
   public static int green = 4;
   public static int orange = 5;
   public static int grey = 6;

}

///////////////////////StaticTest.java

import static MyConstants.Colors.*; import static MyConstants.Fruits.*; public class StaticTest {

   public static void main(String args[])
   {
       System.out.println("orange = " + orange);
       System.out.println("color orange = " + Colors.orange);
       System.out.println("Fruity orange = " + Fruits.orange);
   }

}


      </source>
   
  
 
  



Static import: sort

   <source lang="java">

/* License for Java 1.5 "Tiger": A Developer"s Notebook

    (O"Reilly) example package

Java 1.5 "Tiger": A Developer"s Notebook (O"Reilly) by Brett McLaughlin and David Flanagan. ISBN: 0-596-00738-8 You can use the examples and the source code any way you want, but please include a reference to where it comes from if you use it in your own products or services. Also note that this software is provided by the author "as is", with no expressed or implied warranties. In no event shall the author be liable for any direct or indirect damages arising in any way out of the use of this software.

  • /

import java.util.Arrays; import static java.util.Arrays.sort; import static java.util.Collections.sort; public class SortImporter {

 public static void main(String[] args) {
   float[] f = new float[] {5, 4, 6, 3, 2, 1};
   sort(f);
 }

}

      </source>
   
  
 
  



Static import user defined static fields.

   <source lang="java">

/* Java 2, v5.0 (Tiger) New Features by Herbert Schildt ISBN: 0072258543 Publisher: McGraw-Hill/Osborne, 2004

  • /

package MyMsg;

public class Msg {

 public static final int UPPER = 1; 
 public static final int LOWER = 2; 
 public static final int MIXED = 3; 

 private String msg; 

 // Display a message in the specified case. 
 public void showMsg(int how) { 
   String str; 

   switch(how) { 
     case UPPER: 
       str = msg.toUpperCase(); 
       break; 
     case LOWER: 
       str = msg.toLowerCase(); 
       break; 
     case MIXED: 
       str = msg; 
       break; 
     default: 
       System.out.println("Invalid command."); 
       return; 
   } 

   System.out.println(str); 
 } 

 public Msg(String s) { msg = s; } 

}

// Static import user-defined static fields. import MyMsg.*;

import static MyMsg.Msg.*;

class Test {

 public static void main(String args[]) {  
   Msg m = new Msg("Testing static import."); 

   m.showMsg(MIXED); 
   m.showMsg(LOWER); 
   m.showMsg(UPPER); 
 }  

}

      </source>
   
  
 
  



Use static import to bring sqrt() and pow() into view.

   <source lang="java">

import static java.lang.Math.pow; import static java.lang.Math.sqrt; public class Hypot {

 public static void main(String args[]) {
   double side1, side2;
   double hypot;
   side1 = 3.0;
   side2 = 4.0;
   hypot = sqrt(pow(side1, 2) + pow(side2, 2));
   System.out.println("Given sides of lengths " + side1 + " and " + side2 + " the hypotenuse is "
       + hypot);
 }

}


      </source>