目录

第三章:类与对象

课件

下载:类与对象

Point类

Point.java
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 + "]";
	}
}

初始化对象

Rectangle.java
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());
	}
}

构造函数

Rectangle.java
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)
    }
    ...
}

类修辞符:无修辞符

NoQualifier类

NoQualifier.java
package cn.edu.bjut.chapter3;
 
class NoQualifier {
	int a = 45; 
}

NoQualiferTester类

NoQualifierTester.java
package cn.edu.bjut.chapter3;
 
public class NoQualifierTester {
	public static void main(String[ ] args) { 
        NoQualifier nq = new NoQualifier( );
        System.out.println(nq.a);
    }
} 

类修辞符:public

Rectangle2类

Rectangle2.java
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);
	}
}

PublicQualifierTester类

PublicQualifierTester.java
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();
    }
}

类修辞符:abstract

Shape类

Shape.java
package cn.edu.bjut.chapter3;
 
public abstract class Shape {
    abstract double area(); 
}

Rectangle3类

Rectangle3.java
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);
	}
}

Triangle类

Triangle.java
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);
	}
}

类修辞符:final

RectangleFinal.java
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);
    }
}
FinalTester.java
package cn.edu.bjut.chapter3;
 
// 错误
public class FinalTester extends RectangleFinal {
 
}

数据成员修饰符:static

Student.java
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);
	}
}

数据成员修饰符:final

Circle.java
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());
	}
}

方法体内的局部变量

LocalVariableTester.java
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);
	}
}

形式参数与实际参数

ParameterTester.java
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);
	}
}

成员方法的递归引用

Factorial.java
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));
	}
}

上机作业

【参考答案】

Date.java
package cn.edu.bjut.chapter3;
 
public class Date {
	private int year, month, day; 
 
	public Date(int year, int month, int day) {
		this.year = year; 
		this.month = month; 
		this.day = day; 
	}
 
	public int getYear() {
		return year;
	}
 
	public void setYear(int year) {
		this.year = year;
	}
 
	public int getMonth() {
		return month;
	}
 
	public void setMonth(int month) {
		this.month = month;
	}
 
	public int getDay() {
		return day;
	}
 
	public void setDay(int day) {
		this.day = day;
	}
 
	@Override
	public String toString() {
		return year + "-" + month + "-" + day;
	}
 
	public int daysInMonth() {
		switch (month) {
		case 1: 
		case 3: 
		case 5: 
		case 7: 
		case 8:
		case 10: 
		case 12: 
			return 31; //大月
		case 4: 
		case 6: 
		case 9: 
		case 11: 
			return 30; //小月
		default: // 2月
			if (((year % 100 != 0) && (year % 4 == 0)) 
					|| (year % 400 == 0)) {
				return 29; 
			} 
 
			return 28; 
		}
	}
 
	public Date tomorrow() {
		int tomorrowYear = year, tomorrowMonth = month, tomorrowDay = day; 
 
		if (day == daysInMonth()) {
			tomorrowDay = 1; 
			tomorrowMonth++; 
 
			if (month == 12) {
				tomorrowMonth = 1; 
				tomorrowYear++; 
			}
		} else {
			tomorrowDay++; 
		}
 
		return (new Date(tomorrowYear, tomorrowMonth, tomorrowDay)); 
	}
 
	public static void main(String[] args) {
		Date date = new Date(2017, 9, 30); 
		System.out.println("The number of days: " + date.daysInMonth());
		System.out.println(date + "\n" + date.tomorrow());
 
		Date date2 = new Date(2016, 2, 28); 
		System.out.println("The number of days: " + date2.daysInMonth());
		System.out.println(date2 + "\n" + date2.tomorrow());
	}
}
位数 内容 取值
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.java
package cn.edu.bjut.chapter3;
 
public class Ipc {
	private char section; 
	private int mainClass; 
	private char subclass; 
	private int mainGroup; 
	private int subgroup; 
 
	private final static char SEPARATOR = '/'; 
 
	public Ipc(char section, int mainClass, char subclass, int mainGroup, int subgroup) {
		this.section = section; 
		this.mainClass = mainClass; 
		this.subclass = subclass; 
		this.mainGroup = mainGroup; 
		this.subgroup = subgroup; 
	}
 
	public char getSection() {
		return section;
	}
 
	public void setSection(char section) {
		this.section = section;
	}
 
	public int getMainClass() {
		return mainClass;
	}
 
	public void setMainClass(int mainClass) {
		this.mainClass = mainClass;
	}
 
	public char getSubclass() {
		return subclass;
	}
 
	public void setSubclass(char subclass) {
		this.subclass = subclass;
	}
 
	public int getMainGroup() {
		return mainGroup;
	}
 
	public void setMainGroup(int mainGroup) {
		this.mainGroup = mainGroup;
	}
 
	public int getSubgroup() {
		return subgroup;
	}
 
	public void setSubgroup(int subgroup) {
		this.subgroup = subgroup;
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(); 
 
		sb.append(section); 
 
		String strMainClass = String.format("%2s", mainClass).replace(' ', '0'); 
		sb.append(strMainClass);
 
		sb.append(subclass); 
 
		String strMainGroup = String.format("%4s", mainGroup); 
		sb.append(strMainGroup); 
 
		sb.append(SEPARATOR); 
 
		sb.append(subgroup); 
 
		return sb.toString(); 
	}
 
	public static void main(String[] args) {
		char section = 'A'; 
		int mainClass = 4; 
		char subclass = 'B'; 
		int mainGroup = 55; 
		int subgroup = 56; 
 
		Ipc ipc = new Ipc(section, mainClass, subclass, mainGroup, subgroup); 
		System.out.println(ipc); 
	}
}

【参考答案】

Ipc.java
package cn.edu.bjut.chapter3;
 
public class Ipc {
	private char section; 
	private int mainClass; 
	private char subclass; 
	private int mainGroup; 
	private int subgroup; 
 
	private final char SEPARATOR = '/'; 
 
	public Ipc(char section, int mainClass, char subclass, int mainGroup, int subgroup) {
		this.section = section; 
		this.mainClass = mainClass; 
		this.subclass = subclass; 
		this.mainGroup = mainGroup; 
		this.subgroup = subgroup; 
	}
 
	// example: G09F 17/30
	public Ipc(String symbol) {
		this.section = symbol.charAt(0); 
		this.mainClass = Integer.parseInt(symbol.substring(1, 3)); 
		this.subclass = symbol.charAt(3); 
 
		int pos = symbol.indexOf(SEPARATOR); 
		this.mainGroup = Integer.parseInt(symbol.substring(4, pos).trim()); 
		this.subgroup = Integer.parseInt(symbol.substring(pos + 1)); 
	}
 
	public char getSection() {
		return section;
	}
 
	public void setSection(char section) {
		this.section = section;
	}
 
	public int getMainClass() {
		return mainClass;
	}
 
	public void setMainClass(int mainClass) {
		this.mainClass = mainClass;
	}
 
	public char getSubclass() {
		return subclass;
	}
 
	public void setSubclass(char subclass) {
		this.subclass = subclass;
	}
 
	public int getMainGroup() {
		return mainGroup;
	}
 
	public void setMainGroup(int mainGroup) {
		this.mainGroup = mainGroup;
	}
 
	public int getSubgroup() {
		return subgroup;
	}
 
	public void setSubgroup(int subgroup) {
		this.subgroup = subgroup;
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(); 
 
		sb.append(section); 
 
		String strMainClass = String.format("%2s", mainClass).replace(' ', '0'); 
		sb.append(strMainClass);
 
		sb.append(subclass); 
 
		String strMainGroup = String.format("%4s", mainGroup); 
		sb.append(strMainGroup); 
 
		sb.append(SEPARATOR); 
 
		sb.append(subgroup); 
 
		return sb.toString(); 
	}
 
	public static void main(String[] args) {
		char section = 'A'; 
		int mainClass = 4; 
		char subclass = 'B'; 
		int mainGroup = 55; 
		int subgroup = 56; 
 
		Ipc ipc = new Ipc(section, mainClass, subclass, mainGroup, subgroup); 
		System.out.println(ipc); 
 
		String symbol = "G09F 17/30"; 
		System.out.println(new Ipc(symbol)); 
	}
}

返回Java课程页