Java Tutorial/Class Definition/Class Fields

Материал из Java эксперт
Версия от 05:00, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A Class that Demonstrates Shadowing

public class MainClass {
  static int x;
  public static void main(String[] args) {
    x = 5;
    System.out.println("x = " + x);
    int x;
    x = 10;
    System.out.println("x = " + x);
    System.out.println("ShadowApp.x = " + ShadowApp.x);
  }
}





Dynamically changing the behavior of an object via composition (the "State" design pattern)

abstract class Actor {
  public abstract void act();
}
class HappyActor extends Actor {
  public void act() {
    System.out.println("HappyActor");
  }
}
class SadActor extends Actor {
  public void act() {
    System.out.println("SadActor");
  }
}
class Stage {
  private Actor actor = new HappyActor();
  public void change() {
    actor = new SadActor();
  }
  public void performPlay() {
    actor.act();
  }
}
public class MainClass {
  public static void main(String[] args) {
    Stage stage = new Stage();
    stage.performPlay();
    stage.change();
    stage.performPlay();
  }
}



HappyActor
SadActor


Scope class demonstrates field and local variable scopes

public class MainClass
{
   public static void main( String args[] )
   {
      Scope testScope = new Scope();
      testScope.begin();
   }
}
class Scope
{
   private int x = 1; 
   public void begin()
   {
      int x = 5; // method"s local variable x shadows field x
      System.out.printf( "local x in method begin is %d\n", x );
      useLocalVariable();
      useField();
      useLocalVariable();
      useField();
      System.out.printf( "\nlocal x in method begin is %d\n", x );
   }
   public void useLocalVariable()
   {
      int x = 25; // initialized each time useLocalVariable is called
      System.out.printf("\nlocal x on entering method useLocalVariable is %d\n", x );
      ++x; // modifies this method"s local variable x
      System.out.printf( 
         "local x before exiting method useLocalVariable is %d\n", x );
   }
   // modify class Scope"s field x during each call
   public void useField()
   {
      System.out.printf("\nfield x on entering method useField is %d\n", x );
      x *= 10; // modifies class Scope"s field x
      System.out.printf("field x before exiting method useField is %d\n", x );
   }
}



local x in method begin is 5
local x on entering method useLocalVariable is 25
local x before exiting method useLocalVariable is 26
field x on entering method useField is 1
field x before exiting method useField is 10
local x on entering method useLocalVariable is 25
local x before exiting method useLocalVariable is 26
field x on entering method useField is 10
field x before exiting method useField is 100
local x in method begin is 5


Scope for Class and Local Variables

public class MainClass {
  static int x;
  public static void main(String[] args) {
    x = 5;
    System.out.println("main: x = " + x);
    myMethod();
  }
  public static void myMethod() {
    int y;
    y = 10;
    if (y == x + 5) {
      int z;
      z = 15;
      System.out.println("myMethod: z = " + z);
    }
    System.out.println("myMethod: x = " + x);
    System.out.println("myMethod: y = " + y);
  }
}