java 板


LINE

※ 引述《withdrawn (翔)》之铭言: : 这学期老师刚交完 : 期末要给他看一个物件导向程式 : 须用到封装.继承.抽象.多型.建构子 : (用法都了解) : 问题是不知道从哪方面下手.. : 不知道要做什麽主题= = : 虽然说只有5% 还是想写看看 毕竟以後还是要写专题 我比较好奇的是... 如果用法都了解的话... 为甚麽还要烦恼做什麽主题? 如果只是讲到这边,大概又会说版主在酸人 秉持着把 Java 版糟糕化的任务(喂喂!) 只好以身作则...... [挺] (建议使用空白键翻页 \囧/) - 首先,你必须要先知道 即使是封装,也不是程式必需品 就算是你打算写出一个作业系统 你还是可以完全没有任何封装的行为 更不用说後头的继承、多型之类的鬼东西 (当然,请不要真的这样尝试... XDrz) 还是那句话 「道不虚明、有为而发」 你必须要先体验到「棍!这样下去好烦」 进阶版是可以感觉到「棍!这样下去以後会有爆炸的危险」 为了解决这些问题,所以找了这些技术来克服 我觉得这样才有意义... 以我的标准来说,这样才算「会」那些东西 (所以,其实我还是不确定多型是啥东西... 只是写出来的程式,人家说:「这就是多型啊 [殴]」) - 如果你真的纯粹只想交作业 那麽,一个刚开始写程式要会的印星星金字塔 也是可以搞出那些东西的 [茶] ========== 不负责任程式码开始了 ========== //Pyramid.java public abstract class Pyramid extends BbsBuilding { Pyramid(char brick, int level) { super(brick); build(level); } @Override public void build(int level) { for (int i = 0; i < level; i++) { fillFloor(i); nextFloor(); } } public static void show(Pyramid pyramid) { pyramid.takePicture(); } public static void main(String[] args) { StarPyramid star = new StarPyramid(5); Pyramid.show(star); MysteriousPyramid mysterious = new MysteriousPyramid(25); Pyramid.show(mysterious); GhostPyramid nothing = new GhostPyramid(10); Pyramid.show(nothing); } } //StarPyramid.java public class StarPyramid extends Pyramid{ public StarPyramid(int level) { super('*', level); } } //MysteriousPyramid.java public class MysteriousPyramid extends Pyramid { MysteriousPyramid(int level) { super('?', level); } } //GhostPyramid.java public class GhostPyramid extends Pyramid { public GhostPyramid(int level) { super(' ', level); } } //BbsBuilding.java import java.util.ArrayList; public abstract class BbsBuilding implements Building { ArrayList<String> building; private String floor = ""; private char brick; BbsBuilding(char brick) { this.brick = brick; building = new ArrayList<String>(); } @Override public void fillFloor(int size) { for (int i = 0; i <= size; i++) { floor += brick; } } @Override public void nextFloor() { building.add(floor); floor = ""; } private final int CONSOLE_WIDTH = 77; private final int CONSOLE_HIGHT = 21; //TODO level > 80 will break pic @Override public void takePicture() { // 相框下缘 System.out.print("+"); drawLineBy(0, CONSOLE_WIDTH - this.getClass().getName().length() - 4, '-'); System.out.println(this.getClass().getName() + "---+"); // 留白 for (int i = building.size(); i < CONSOLE_HIGHT - 2; i++) { System.out.print("|"); drawLineBy(0, CONSOLE_WIDTH - 1, ' '); System.out.println("|"); } // 建筑主体 for (int i = getTopFloor(); i < building.size(); i++) { System.out.print("|"); System.out.print(building.get(i)); drawLineBy(building.get(i).length() + 1, CONSOLE_WIDTH, ' '); System.out.println("|"); } // 相框下缘 System.out.print("+"); drawLineBy(0, CONSOLE_WIDTH - 1, '-'); System.out.println("+"); } private void drawLineBy(int start, int end, char c) { for (int i = start; i < end; i++) { System.out.print(c); } } private int getTopFloor() { if (building.size() > CONSOLE_HIGHT) { return building.size() - CONSOLE_HIGHT + 2; } else { return 0; } } } //Building.java public interface Building { void build(int level); void fillFloor(int size); void nextFloor(); //TODO next version will make Camera interface and cancel this behavior public void takePicture(); } ========== 以下为执行结果,请将此行对齐萤幕顶端,用空白键翻页 ========== +--------------------------------------------------------------StarPyramid---+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | |* | |** | |*** | |**** | |***** | +----------------------------------------------------------------------------+ +--------------------------------------------------------MysteriousPyramid---+ |??????? | |???????? | |????????? | |?????????? | |??????????? | |???????????? | |????????????? | |?????????????? | |??????????????? | |???????????????? | |????????????????? | |?????????????????? | |??????????????????? | |???????????????????? | |????????????????????? | |?????????????????????? | |??????????????????????? | |???????????????????????? | |????????????????????????? | +----------------------------------------------------------------------------+ +-------------------------------------------------------------GhostPyramid---+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +----------------------------------------------------------------------------+ ㄜ... 该不该写信给出作业的老师,以防有人拿这个版本交差呢? [核爆] -- 钱锺书: 说出来的话 http://www.psmonkey.org 比不上不说出来的话 Java 版 cookcomic 版 只影射着说不出来的话 and more...... --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 219.70.214.149
1F:→ rofellosx:我以为GhostPyramid反白有东西 06/10 12:42
2F:→ PsMonkey:ㄜ... 理论上是有... [茶] 06/10 13:10







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Gossiping站内搜寻

TOP