这是本文档旧的修订版!
下载:类与对象
package cn.edu.bjut.chapter3; class Point { private int x, y; public void setPoint(int a, int b) { x = a; y = b; } public int getX() { return x; } public int getY() { return y; } public String toString() { return "Point [x=" + x + ", y=" + y + "]"; } }
package cn.edu.bjut.chapter3; public class Rectangle { double length = 0, width = 0; // 赋值语句 // 初始化代码块 { length = 1; width = 2; } // 构造函数 public Rectangle(double len, double wid) { length = len; width = wid; } public double area() { return (length * width); } public static void main(String[] args) { Rectangle rect = new Rectangle(30, 20); System.out.println(rect.area()); } }
package cn.edu.bjut.chapter3; public class Rectangle { double length, width; // 构造函数 public Rectangle(double length, double width) { this.length = length; this.width = width; } // 构造函数 public Rectangle(double length) { this(length, length) } ... }
package cn.edu.bjut.chapter3; class NoQualifier { int a = 45; }
package cn.edu.bjut.chapter3; public class NoQualifierTester { public static void main(String[ ] args) { NoQualifier nq = new NoQualifier( ); System.out.println(nq.a); } }
package cn.edu.bjut.chapter3; public class Rectangle2 { double length, width; public Rectangle2(double length, double width) { this.length = length; this.width = width; } public double area() { return (length * width); } }
package cn.edu.bjut.chapter3_1; import cn.edu.bjut.chapter3.Rectangle2; public class PublicQualifierTester { public static void main(String[] args) { Rectangle2 rect = new Rectangle2(30, 20); System.out.println(rect.area()); // 错误 NoQualifier nq = new NoQualifier(); } }
package cn.edu.bjut.chapter3; public abstract class Shape { abstract double area(); }
package cn.edu.bjut.chapter3; public class Rectangle3 extends Shape { double length, width; public Rectangle3(double length, double width) { this.length = length; this.width = width; } public double area() { return (length * width); } }
package cn.edu.bjut.chapter3; public class Triangle extends Shape { double height, width; public Triangle(double height, double width) { this.height = height; this.width = width; } @Override double area() { return (height * width / 2); } }
package cn.edu.bjut.chapter3; public final class RectangleFinal { double length = 0, width = 0; // 赋值语句 public Rectangle(double length, double width) { this.length = length; this.width = width; } double area() { return (length*width); } }
package cn.edu.bjut.chapter3; // 错误 public class FinalTester extends RectangleFinal { }
package cn.edu.bjut.chapter3; public class Student { private String name; private static String country = "中国"; public Student(String name) { this.name = name; } public String toString() { return "name = " + name + ", country = " + country; } public static void main(String[] args) { Student s1 = new Student("何道昌"); Student s2 = new Student("孙双双"); System.out.println(s1 + "\n" + s2); s1.country = "日本"; System.out.println(s1 + "\n" + s2); System.out.println(Student.country); } }
package cn.edu.bjut.chapter3; public class Circle { private double r; public static final double PI = 3.14; public Circle(double r) { this.r = r; } public double area() { return (r * r * PI); } public static void main(String[] args) { Circle c1 = new Circle(2); Circle c2 = new Circle(3); System.out.println(c1.area() + "\t" + c2.area()); } }
package cn.edu.bjut.chapter3; public class LocalVariableTester { static int add(int x, int y) { int z, d; z = x + y; // z = x + d; return z; } public static void main(String[] args) { int a = 2, b = 3; int f = add(a, b); System.out.println("f = " + f); // System.out.println("z = " + z); { int z = a + b; System.out.println("z = " + z); } // System.out.println("z = " + z); } }
package cn.edu.bjut.chapter3; public class ParameterTester { static void add(double x, double y) { double z; z = x + y; System.out.println("z = " + z); x = x + 3.2; y = y + 1.2; System.out.println("x = " + x + "\ty = " + y); } static double add2(double y1, double y2) { double z; z = y1 + y2 + 2.9; return z; } public static void main(String[] args) { int a = 2, b = 7; double f1 = 2, f2 = 4, f3; add(a, b);// 按Java的类型转换规则达到形参类型 System.out.println("a = " + a + "\tb = " + b); // f3 = add2(f1, f2, 3.5);错,实参与形参参数个数不一致 f3 = 2 + add2(f1, f2); System.out.println("f1 = " + f1 + "\tf2 = " + f2 + "\tf3 = " + f3); } }
package cn.edu.bjut.chapter3; public class Factorial { static int calculate(int n) { if (n == 0) { return 1; } else { return (n * calculate(n - 1)); } } public static void main(String[] args) { int n = 5; System.out.println(n + "! = " + calculate(n)); } }
| 位数 | 内容 | 取值 |
|---|---|---|
| 1 | section | A..H |
| 2,3 | class | 01…99 |
| 4 | subclass | A..Z |
| 5-8 | main group(右对齐) | 1…9999 |
| 9 | 分割符 | / |
| 10-14 | subgroup(左对齐) | 0…99999 |
* 上题的Ipc类至少要包含两个构造函数,其中一个构造函数有五个参数(分别为部、大类、小类、大组和小组),另一个构造函数只有一个参数(以字符串形式表示的IPC分类号)。
评论