📄 抽象类.txt
字号:
抽象类 List.1 DataHiding/DataHiding.java
抽象类 List.2 ProtectedData/ProtectedData.java
抽象类 List.3 AbstractDemo/TObject.java
抽象类 List.4 AbstractDemo/TContainer.java
抽象类 List.5 AbstractDemo/TMyObject.java
抽象类 List.6 AbstractDemo/AbstractDemo.java
--------------------------------------------------------------------------------
抽象类 List.1 DataHiding/DataHiding.java
Return to top
001: class TDate {
002: private int month, day, year;
003: public TDate(int month, int day, int year) {
004: setDate(month, day, year);
005: }
006: public void setDate(int month, int day, int year) {
007: this.month = month;
008: this.day = day;
009: this.year = year;
010: }
011: public String getDate() {
012: return month + "/" + day + "/" + year;
013: }
014: }
015:
016: class DataHiding {
017: public static void main(String args[]) {
018: TDate birthday = new TDate(8, 15, 1975);
019: String s = birthday.getDate();
020: System.out.println("birthday = " + s);
021: }
022: }
Return to top
--------------------------------------------------------------------------------
抽象类 List.2 ProtectedData/ProtectedData.java
Return to top
001: class TDate {
002: protected int month, day, year;
003: public TDate(int month, int day, int year) {
004: setDate(month, day, year);
005: }
006: public void setDate(int month, int day, int year) {
007: this.month = month;
008: this.day = day;
009: this.year = year;
010: }
011: public String getDate() {
012: return month + "/" + day + "/" + year;
013: }
014: }
015:
016: class TDateTime extends TDate {
017: protected int hour, min;
018: public TDateTime(int month, int day, int year,
019: int hour, int min) {
020: super(month, day, year); // Call superclass constructor
021: this.hour = hour;
022: this.min = min;
023: }
024: public String getDate() { // Override method
025: return month + "/" + day + "/" + year +
026: " : " + hour + ":" + min;
027: }
028: }
029:
030: class ProtectedData {
031: public static void main(String args[]) {
032: TDate now = new TDateTime(3, 15, 2001, 14, 45);
033: String s = now.getDate();
034: System.out.println("now = " + s);
035: }
036: }
Return to top
--------------------------------------------------------------------------------
抽象类 List.3 AbstractDemo/TObject.java
Return to top
001: abstract class TObject implements Comparable {
002: abstract public int compareTo(Object other);
003: abstract public void show();
004: }
Return to top
--------------------------------------------------------------------------------
抽象类 List.4 AbstractDemo/TContainer.java
Return to top
001: import TObject;
002: import java.util.Arrays;
003:
004: class ContainerFullException extends Exception { };
005:
006: class TContainer {
007: // Private instance variables
008: private int size; // Size of objArray
009: private int count; // Count of objects in objArray
010: private TObject objArray[]; // Array of objects
011:
012: // Constructor (n = array size)
013: public TContainer(int n) {
014: if (n <= 0) n = 1; // Minimum allowed size
015: size = n;
016: count = 0;
017: objArray = new TObject[size];
018: }
019:
020: // Insert object into container
021: public void putObject(TObject obj)
022: throws ContainerFullException {
023: if (count >= size)
024: throw new ContainerFullException();
025: objArray[count++] = obj;
026: }
027:
028: // Display all objects in container
029: public void showAllObjects(String label) {
030: System.out.println(label);
031: for (int i = 0; i < count; i++)
032: objArray[i].show();
033: System.out.println();
034: }
035:
036: // Sort the objects in the container
037: public void sort() {
038: if (count > 1)
039: Arrays.sort(objArray, 0, count);
040: }
041: }
Return to top
--------------------------------------------------------------------------------
抽象类 List.5 AbstractDemo/TMyObject.java
Return to top
001: import TObject;
002:
003: class TMyObject extends TObject {
004: private String s;
005:
006: // Constructor
007: TMyObject(String s) {
008: this.s = s;
009: }
010:
011: // Implement Comparable interface method
012: public int compareTo(Object other) {
013: TMyObject otherObject = (TMyObject)other;
014: return s.compareTo(otherObject.s);
015: }
016:
017: // Implement TObject abstract method
018: public void show() {
019: System.out.print(s + " ");
020: }
021: }
Return to top
--------------------------------------------------------------------------------
抽象类 List.6 AbstractDemo/AbstractDemo.java
Return to top
001: import TContainer;
002: import TMyObject;
003:
004: class AbstractDemo {
005: public static void main(String args[]) {
006: TContainer container = new TContainer(100);
007: try {
008: container.putObject(new TMyObject("Peach"));
009: container.putObject(new TMyObject("Mango"));
010: container.putObject(new TMyObject("Lime"));
011: container.putObject(new TMyObject("Banana"));
012: container.putObject(new TMyObject("Kiwi"));
013: container.putObject(new TMyObject("Grapefruit"));
014: container.putObject(new TMyObject("Orange"));
015: container.putObject(new TMyObject("Lemon"));
016: container.putObject(new TMyObject("Apple"));
017: container.showAllObjects("Before sorting");
018: container.sort();
019: container.showAllObjects("After sorting");
020: } catch (ContainerFullException e) {
021: System.out.println("Container overflow error");
022: }
023: }
024: }
Return to top
? 2003 by ChinaITLab.com All rights reserved.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -