`
dasheng
  • 浏览: 146334 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

《java编程思想》学习笔记7 第7章 复用类

阅读更多

1,组合(composition):

 

新的类由现有的类的对象组成,这种方法称为组合;

 

2,继承:按照现有类的类型来创建新类,无需改变现有类的形式。

 

3,带参数的构造器

 

class Game {
  Game(int i) {
    print("Game constructor");
  }
}

class BoardGame extends Game {
  BoardGame(int i) {
    super(i);//调用父类构造器
    print("BoardGame constructor");
  }

public class Chess extends BoardGame {
  Chess() {
    super(11);//调用父类构造器
    print("Chess constructor");
  }
  public static void main(String[] args) {
    Chess x = new Chess();
  }
} /* Output:

 

4,名字屏蔽

 

如果java的基类拥有某个已被多次重载的方法名称,那么在导出类中重新定义该方法名称,不会屏蔽其在基类中的任何版

 

本,这与c++不同。因此,无论在该层或它的基类中对方法进行定义,重载机制都可以正常工作。

 

class Homer {
  char doh(char c) {
    print("doh(char)");
    return 'd';
  }
  float doh(float f) {
    print("doh(float)");
    return 1.0f;
  }
}

class Milhouse {}

class Bart extends Homer {
  void doh(Milhouse m) {
    print("doh(Milhouse)");
  }
}

public class Hide {
  public static void main(String[] args) {
    Bart b = new Bart();
    b.doh(1);
    b.doh('x');
    b.doh(1.0f);
    b.doh(new Milhouse());
  }
} /* Output:

 

输出结果是:

doh(float)
doh(char)
doh(float)
doh(Milhouse)

 

jdk5中新增加了@Override注解,它不是关键字,但是可以当做关键字使用。当你想要覆盖某个方法时,可以

 

选择添加这个注解,在你不小心重载而并非覆盖了该方法时,编译器会报错。

 

class Lisa extends Homer {
  @Override char doh(char c) {
    System.out.println("doh(Milhouse)");
    return 'd';
  }
} ///:~

 

5,final关键字

 

1 final 数据

 

  1.   一个永不改变的编译时常量;
  2. 一个在运行时被初始化的值,你不希望它被改变。

一个既是static又是final的域只占据一段不能改变的存储空间。

 

对于基本类型,final使得数据恒定不变,而对于对象引用,final使引用恒定不变,一旦引用被初始化指向一个对象,就无

 

法再把它改为指向另一个对象。然而,对象其自身却是可以被修改的。

 

class Value {
  int i; // Package access
  public Value(int i) { this.i = i; }
}

public class FinalData {
  private static Random rand = new Random(47);
  private String id;
  public FinalData(String id) { this.id = id; }
  // Can be compile-time constants:
  private final int valueOne = 9;
  private static final int VALUE_TWO = 99;
  // Typical public constant:
  public static final int VALUE_THREE = 39;
  // Cannot be compile-time constants:
  private final int i4 = rand.nextInt(20);
  static final int INT_5 = rand.nextInt(20);
  private Value v1 = new Value(11);
  private final Value v2 = new Value(22);
  private static final Value VAL_3 = new Value(33);
  // Arrays:
  private final int[] a = { 1, 2, 3, 4, 5, 6 };
  public String toString() {
    return id + ": " + "i4 = " + i4 + ", INT_5 = " + INT_5;
  }
  public static void main(String[] args) {
    FinalData fd1 = new FinalData("fd1");
    // fd1.valueOne++; // Error: can't change value
    fd1.v2.i++; // Object isn't constant!
    fd1.v1 = new Value(9); // OK -- not final
    for(int i = 0; i < fd1.a.length; i++)
      fd1.a[i]++; // Object isn't constant!
    // fd1.v2 = new Value(0); // Error: Can't
    // fd1.VAL_3 = new Value(1); // change reference
   // fd1.a = new int[3];
    print(fd1);
    print("Creating new FinalData");
    FinalData fd2 = new FinalData("fd2");
    print(fd1);
    print(fd2);
  }
} /* Output:

 

6,空白final

 

指被声明为final但又未定初始值的域

 

class Poppet {
  private int i;
  Poppet(int ii) { i = ii; }
}

public class BlankFinal {
  private final int i = 0; // Initialized final
  private final int j; // Blank final
  private final  Poppet p; // Blank final reference 
  // Blank finals MUST be initialized in the constructor:
  public BlankFinal() {
    j = 1; // Initialize blank final
    p = new Poppet(1); // Initialize blank final reference
  }
  public BlankFinal(int x) {
    j = x; // Initialize blank final
    p = new Poppet(x); // Initialize blank final reference
  }
  public static void main(String[] args) {
    new BlankFinal();
    new BlankFinal(47);
  }
} ///:~

 

7,final参数

 

class Gizmo {
  public void spin() {}
}

public class FinalArguments {
  void with(final Gizmo g) {
    // g = new Gizmo(); // 非法,不能修改
  }
  void without(Gizmo g) {
    g = new Gizmo(); // OK -- g not final
    g.spin();
  }
  // void f(final int i) { i++; } // 不能改变
  // You can only read from a final primitive:
  int g(final int i) { return i + 1; }
  public static void main(String[] args) {
    FinalArguments bf = new FinalArguments();
    bf.without(null);
    bf.with(null);
  }
} ///:~

 

8,final方法

 

1,把方法锁定,防止任何继承类修改它的含义;

2,确保在继承中该方法保持不变,并且不会被覆盖。

 

类中所有的private方法都隐式的制定为final的。

 

class WithFinals {
  // Identical to "private" alone:
  private final void f() { print("WithFinals.f()"); }
  // Also automatically "final":
  private void g() { print("WithFinals.g()"); }
}

class OverridingPrivate extends WithFinals {
  private final void f() {
    print("OverridingPrivate.f()");
  }
  private void g() {
    print("OverridingPrivate.g()");
  }
}

class OverridingPrivate2 extends OverridingPrivate {
  public final void f() {
    print("OverridingPrivate2.f()");
  }
  public void g() {
    print("OverridingPrivate2.g()");
  }
}

public class FinalOverridingIllusion {
  public static void main(String[] args) {
    OverridingPrivate2 op2 = new OverridingPrivate2();
    op2.f();
    op2.g();
    // You can upcast:
    OverridingPrivate op = op2;
    // But you can't call the methods:
    // op.f();
    //! op.g();
    // Same here:
    WithFinals wf = op2;
    //! wf.f();
    //! wf.g();
  }
} /* Output:

 

9,final类

 

由于final类禁止继承,所以final类中所有的方法都隐式的指定为fina的,因为无法覆盖它们。

 

10,类的代码在初次使用时才会加载,这通常指加载发生在创建类的第一个对象之时,但是当访问static域或static方法

 

时,也会发生加载。

 

class Insect {
  private int i = 9;
  protected int j;
  Insect() {
    print("i = " + i + ", j = " + j);
    j = 39;
  }
  private static int x1 =
    printInit("static Insect.x1 initialized");
  static int printInit(String s) {
    print(s);
    return 47;
  }
}

public class Beetle extends Insect {
  private int k = printInit("Beetle.k initialized");
  public Beetle() {
    print("k = " + k);
    print("j = " + j);
  }
  private static int x2 =
    printInit("static Beetle.x2 initialized");
  public static void main(String[] args) {
    print("Beetle constructor");
    Beetle b = new Beetle();
  }
} /* Output:

 

运行结果:

 

static Insect.x1 initialized
static Beetle.x2 initialized
Beetle constructor
i = 9, j = 0
Beetle.k initialized
k = 47
j = 39

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics