public class Power 1 public static void mainString


![唤起方法 public class Power 1 { public static void main(String[] args) { double a= 唤起方法 public class Power 1 { public static void main(String[] args) { double a=](http://slidetodoc.com/presentation_image_h2/00baea1dbe2c2615444662106717e3fb/image-3.jpg)
唤起方法 public class Power 1 { public static void main(String[] args) { double a= 10. 0, b; int x= 3; System. out. println("Main: a= " + a + ", x= " + x); b= power(a, x); System. out. println("Main: Result b= " + b); } public static double power(double d, int i){ System. out. println("Power: d= " + d + ", i= " +i); double x= d; // 这个 x 是不同于 main方法中的 x for (int j= 1; j < i; j++) x *= d; // x = x* d; return x; } }



![传值呼叫的范例 public class Call. By. Value { public static void main(String[] args) int i= 传值呼叫的范例 public class Call. By. Value { public static void main(String[] args) int i=](http://slidetodoc.com/presentation_image_h2/00baea1dbe2c2615444662106717e3fb/image-7.jpg)
传值呼叫的范例 public class Call. By. Value { public static void main(String[] args) int i= 3; double d= 77. 0; System. out. println("Main 1: i= " + triple(i, d); // 没有回传值 System. out. println("Main 2: i= " + // 范例的第二部份: 改变自变量 triple(i, i); // 好 - Java System. out. println("Main 3: i= " + } { i + ", d= " + d); 转换整数为双精度 i); public static void triple(int ii, double dd) { System. out. println(“Triple 1: ii= " +ii+ ", dd= " +dd); ii *= 3; // ii= ii*3; dd *= 3. 0; System. out. println(“Triple 2: ii= " +ii+ ", dd= " +dd); } }
![跟随对象的传值呼叫 public class Call. Obj. Example { public static void main(String[] args) { Demo 跟随对象的传值呼叫 public class Call. Obj. Example { public static void main(String[] args) { Demo](http://slidetodoc.com/presentation_image_h2/00baea1dbe2c2615444662106717e3fb/image-8.jpg)
跟随对象的传值呼叫 public class Call. Obj. Example { public static void main(String[] args) { Demo d= new Demo(3); int i= 3; System. out. println("Main 1: i= " + i + ", d. a= " + d. a); triple(i, d); // 无回传值 System. out. println("Main 2: i= " + i + ", d. a= " + d. a); } public static void triple(int ii, Demo dd){ System. out. println("T 1: ii= "+ ii + ", dd. a= " + dd. a); ii *= 3; // ii= ii*3; dd. a *= 3; System. out. println("T 2: ii= "+ ii + ", dd. a= " + dd. a); } }

跟随对象的传值呼叫 class Demo { // 设定为public只为表示传值呼叫 public int a; public Demo(int aa) { a= aa; } }





![字符串 public class String. Example { public static void main(String[] args) { String first= 字符串 public class String. Example { public static void main(String[] args) { String first=](http://slidetodoc.com/presentation_image_h2/00baea1dbe2c2615444662106717e3fb/image-15.jpg)
字符串 public class String. Example { public static void main(String[] args) { String first= "George "; String middle= "H. W. "; String last= "Bush"; String full= first + middle + last; System. out. println(full); // 测试字符串的相等 (一般的对象) String full 2= "George H. W. Bush"; if (full. equals(full 2)) // 正确的表示 System. out. println("Strings equal"); if (full == full 2) // 错误的表示 System. out. println("A miracle!"); if (first == “George ”) //错误的表示, 但是可以作用 System. out. println("Not a miracle!");

字符串 // 修改字符串一定要用间接的方式才能完成 // 字符串是常数 middle = middle. substring(0, 2) + " "; full= first + middle + last; System. out. println(full); } }



建构子中的 this public class Simple. Point { private double x, y; // 资料成员 public Simple. Point() { // 建构子 x= 0. 0; y= 0. 0; } public Simple. Point(int x, int y) { this. x = x; this. y = y; } // 方法 public double get. X() { return x; } public double get. Y() { return y; } public void set. X(double xval) { x= xval; } public void set. Y(double yval) { y= yval; } public void move(double delta. X, double delta. Y) { x += delta. X; y += delta. Y; } } // 类别 Simple. Point 结束


类别弹簧的建构子 class Spring { private String material= “steel”; // 初始化 private double length; // 只考虑压缩 private double max. Deflect; private double k; public Spring(String m, double len, double md, double k) { material= m; length= len; max. Deflect= md; this. k= k; // “this” } public Spring(double len, double k) { this("steel", len, 0. 25*len, k); } public Spring(double len) { this(len, 0. 5*len); } public Spring() { this(10. 0); } // “this”

类别弹簧的方法 public public public String get. Material() { return material; } double get. Length() { return length; } double get. Max. Def() { return max. Deflect; } double get. K() { return k; } // 不需要 ‘this’ void set. Material(String m) {material= m; } void set. Length(double len) {length= Math. abs(len); } void set. Max. Def(double m) {max. Deflect= Math. abs(m); } void set. K(double k) {this. k= k; } // this double get. Force(double deflection) { if (deflect > max. Deflect) deflect= max. Deflect; return k*deflect; } public double get. Force() { return k*max. Deflect; } } // 覆载方法
![弹簧的 main() 方法 public class Spring. Example { public static void main(String[] args) { 弹簧的 main() 方法 public class Spring. Example { public static void main(String[] args) {](http://slidetodoc.com/presentation_image_h2/00baea1dbe2c2615444662106717e3fb/image-23.jpg)
弹簧的 main() 方法 public class Spring. Example { public static void main(String[] args) { Spring one= new Spring("aluminum", 2. 0, 1. 0, 5. 0); Spring two= new Spring(5. 0, 3. 0); Spring three= new Spring(); // 3 个不同的建构子 double f 1= one. get. Force(0. 5); double f 2= two. get. Force(1. 5); double f 3= three. get. Force(0. 1); System. out. println("f 1: " + f 1 + "nf 2: " + f 2 + "nf 3: " + f 3); double f 4= one. get. Force(); // 超载的方法 double f 5= two. get. Force(); double f 6= three. get. Force(); System. out. println("f 4: " + f 4 + "nf 5: " + f 5 + "nf 6: " + f 6); System. exit(0); } }







梁的类别 class Beam { private double L; private double E= 30000. 0; // 初始程序 private double I= 1000. 0; public Beam(double L, double E, double I) { this. L= L; // this 来存取资料 this. E= E; this. I= I; } public Beam(double L) { this. L= L; // 其它的靠起始程序 } // 可以使用 this(L, 30000. , 1000. ); public double get. Deflection(double P) { return -P*L*L*L/(3. 0*E*I); } public double get. Deflection(double P, String u) { if (u. equals("ft")) // not == return -3. 3*P*L*L*L/(3. 0*E*I); else return -P*L*L*L/(3. 0*E*I); } }

- Slides: 31