java编程思想易错点三

来源:互联网 发布:软件代理商名录 编辑:程序博客网 时间:2024/06/07 21:48

1、通过改变导入的包,可以控制把自己的代码从调试状态变成最终发行版本。


//: c06:Bath.java// From 'Thinking in Java, 2nd ed.' by Bruce Eckel// www.BruceEckel.com. See copyright notice in CopyRight.txt.// Constructor initialization with composition.class Soap {  private String s;  Soap() {    System.out.println("Soap()");    s = new String("Constructed");  }  public String toString() { return s; }}public class Bath {  private String     // Initializing at point of definition:    s1 = new String("Happy"),     s2 = "Happy",     s3, s4;  Soap castille;  int i;  float toy;  Bath() {    System.out.println("Inside Bath()");    s3 = new String("Joy");    i = 47;    toy = 3.14f;    castille = new Soap();  }  void print() {    // Delayed initialization:    if(s4 == null)      s4 = new String("Joy");    System.out.println("s1 = " + s1);    System.out.println("s2 = " + s2);    System.out.println("s3 = " + s3);    System.out.println("s4 = " + s4);    System.out.println("i = " + i);    System.out.println("toy = " + toy);    System.out.println("castille = " + castille);  }  public static void main(String[] args) {    Bath b = new Bath();    b.print();  }} ///:~
2、上述代码的输出结果:

Inside Bath()
Soap()
s1 = Happy
s2 = Happy
s3 = Joy
s4 = Joy
i = 47
toy = 3.14
castille = Constructed

特别记住Soap castille;并没有做初始化的操作,所以不会调用构造方法,而在Bath类里的构造器对变量都做初始化。

//: c06:Detergent.java// From 'Thinking in Java, 2nd ed.' by Bruce Eckel// www.BruceEckel.com. See copyright notice in CopyRight.txt.// Inheritance syntax & properties.class Cleanser {  private String s = new String("Cleanser");  public void append(String a) { s += a; }  public void dilute() { append(" dilute()"); }  public void apply() { append(" apply()"); }  public void scrub() { append(" scrub()"); }  public void print() { System.out.println(s); }  public static void main(String[] args) {    Cleanser x = new Cleanser();    x.dilute(); x.apply(); x.scrub();    x.print();  }}public class Detergent extends Cleanser {  // Change a method:  public void scrub() {    append(" Detergent.scrub()");    super.scrub(); // Call base-class version  }  // Add methods to the interface:  public void foam() { append(" foam()"); }  // Test the new class:  public static void main(String[] args) {    Detergent x = new Detergent();    x.dilute();    x.apply();    x.scrub();    x.foam();    x.print();    System.out.println("Testing base class:");    Cleanser.main(args);  }} ///:~

3、非常经典的继承关系及子类通过父类的public方法去访问父类的私有属性:要记住子类可以继承父类的私有属性,但是只能通过访问父类的公有方法才能访问属性,所以我们在建类的时候要保留公有的访问方法set和get方法。结果:

Cleanser dilute() apply() Detergent.scrub() scrub() foam()
Testing base class:
Cleanser dilute() apply() scrub()


//: c06:FinalArguments.java// From 'Thinking in Java, 2nd ed.' by Bruce Eckel// www.BruceEckel.com. See copyright notice in CopyRight.txt.// Using "final" with method arguments.class Gizmo {  public void spin() {}}public class FinalArguments {  void with(final Gizmo g) {    //! g = new Gizmo(); // Illegal -- g is final    g.spin();  }  void without(Gizmo g) {    g = new Gizmo(); // OK -- g not final    g.spin();  }  // void f(final int i) { i++; } // Can't change  // 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);  }} ///:~

4、final对象不能被改变,所以报错了:

Exception in thread "main" java.lang.NullPointerException
at FinalArguments.with(FinalArguments.java:13)
at FinalArguments.main(FinalArguments.java:25)


//: c06:FinalData.java// From 'Thinking in Java, 2nd ed.' by Bruce Eckel// www.BruceEckel.com. See copyright notice in CopyRight.txt.// The effect of final on fields.class Value {  int i = 1;}public class FinalData {  // Can be compile-time constants  final int i1 = 9;  static final int VAL_TWO = 99;  // Typical public constant:  public static final int VAL_THREE = 39;  // Cannot be compile-time constants:  final int i4 = (int)(Math.random()*20);  static final int i5 = (int)(Math.random()*20);    Value v1 = new Value();  final Value v2 = new Value();  static final Value v3 = new Value();  // Arrays:  final int[] a = { 1, 2, 3, 4, 5, 6 };  public void print(String id) {    System.out.println(      id + ": " + "i4 = " + i4 +       ", i5 = " + i5);  }  public static void main(String[] args) {    FinalData fd1 = new FinalData();    //! fd1.i1++; // Error: can't change value    fd1.v2.i++; // Object isn't constant!    fd1.v1 = new Value(); // OK -- not final    for(int i = 0; i < fd1.a.length; i++)      fd1.a[i]++; // Object isn't constant!    //! fd1.v2 = new Value(); // Error: Can't     //! fd1.v3 = new Value(); // change reference    //! fd1.a = new int[3];    fd1.print("fd1");    System.out.println("Creating new FinalData");    FinalData fd2 = new FinalData();    fd1.print("fd1");    fd2.print("fd2");  }} ///:~

5、fina数据一创建就不变,若加上static修饰就只是对同一个对象上

fd1: i4 = 4, i5 = 8
Creating new FinalData
fd1: i4 = 4, i5 = 8
fd2: i4 = 10, i5 = 8


0 0
原创粉丝点击