用户工具

站点工具


zh:courses:java2025:ch04

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录 前一修订版
后一修订版
前一修订版
zh:courses:java2025:ch04 [2025/03/09 19:21]
pzczxs [上机作业]
zh:courses:java2025:ch04 [2025/04/04 11:26] (当前版本)
pzczxs 讨论状态变化了
行 1: 行 1:
 ====== 第四章:封装、继承与多态 ====== ====== 第四章:封装、继承与多态 ======
 ===== 课件 ===== ===== 课件 =====
-下载:封装、继承与多态+下载:{{ :​zh:​courses:​java2025:​ch04.pptx |封装、继承与多态}}
  
 ===== 面向对象的三大特性:封装 ===== ===== 面向对象的三大特性:封装 =====
行 654: 行 654:
 ===== 上机作业 ===== ===== 上机作业 =====
   *专利文档中不仅有IPC分类号,还有CPC分类号,CPC的格式与IPC类似,但多了两个字段position和value,这两个字段取值为单个字符,请重写Ipc类和Cpc类,并增加一个父类Classification;   *专利文档中不仅有IPC分类号,还有CPC分类号,CPC的格式与IPC类似,但多了两个字段position和value,这两个字段取值为单个字符,请重写Ipc类和Cpc类,并增加一个父类Classification;
 +【参考答案】
 +<file java Classification.java>​
 +package cn.edu.bjut.chapter4;​
 +
 +public class Classification {
 + protected char section;
 + protected int mainClass;
 + protected char subclass;
 + protected int mainGroup;
 + protected int subgroup;
 +
 + public final static char SEPARATOR = '/';​
 +
 + public Classification(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 Classification(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();​
 + }
 +}
 +</​file>​
 +
 +<file java Ipc.java>​
 +package cn.edu.bjut.chapter4;​
 +
 +public class Ipc extends Classification {
 +
 + public Ipc(char section, int mainClass, char subclass, int mainGroup, int subgroup) {
 + super(section,​ mainClass, subclass, mainGroup, subgroup); ​
 + }
 +
 + public Ipc(String symbol) {
 + super(symbol); ​
 + }
 +
 + public static void main(String[] args) {
 + char section = '​A'; ​
 + int mainClass = 4; 
 + char subclass = '​B'; ​
 + int mainGroup = 55; 
 + int subgroup = 56; 
 +
 + Classification ipc = new Ipc(section,​ mainClass, subclass, mainGroup, subgroup); ​
 + System.out.println(ipc); ​
 +
 + String symbol = "G09F 17/​30"; ​
 + System.out.println(new Ipc(symbol)); ​
 + }
 +}
 +</​file>​
 +
 +<file java Cpc.java>​
 +package cn.edu.bjut.chapter4;​
 +
 +public class Cpc extends Classification {
 + private char position; ​
 + private char value; ​
 +
 + public Cpc(char section, int mainClass, char subclass, int mainGroup, int subgroup, char position, char value) {
 + super(section,​ mainClass, subclass, mainGroup, subgroup); ​
 +
 + this.position = position; ​
 + this.value = value; ​
 + }
 +
 + public Cpc(String symbol, char position, char value) {
 + super(symbol); ​
 +
 + this.position = position; ​
 + this.value = value; ​
 + }
 +
 + public char getPosition() {
 + return position;
 + }
 +
 + public void setPosition(char position) {
 + this.position = position;
 + }
 +
 + public char getValue() {
 + return value;
 + }
 +
 + public void setValue(char value) {
 + this.value = value;
 + }
 +}
 +</​file>​
   *仔细观察身边的事物,对其进行抽象化处理,但需包括父类和父接口等。   *仔细观察身边的事物,对其进行抽象化处理,但需包括父类和父接口等。
  
 [[zh:​courses:​java2025:​index|返回Java课程页]] [[zh:​courses:​java2025:​index|返回Java课程页]]
  
-~~DISCUSSION~~+~~DISCUSSION:closed~~
zh/courses/java2025/ch04.1741519280.txt.gz · 最后更改: 2025/03/09 19:21 由 pzczxs