课外天地 李树青学习天地Java程序语言课件 → [推荐]Java授课视频第六课:面向对象——封装之二


  共有9954人关注过本帖树形打印复制链接

主题:[推荐]Java授课视频第六课:面向对象——封装之二

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1843 积分:24846 威望:0 精华:34 注册:2003/12/30 16:34:32
[推荐]Java授课视频第六课:面向对象——封装之二  发帖心情 Post By:2011/3/27 14:29:24 [只看该作者]

媒体文件信息
文件来源:http://www.njcie.com/JavaSE/files/第六课:面向对象封装之二.wmv
您可以点击控件上的播放按钮在线播放。注意,播放此媒体文件存在一些风险。
附加说明:动网论坛系统禁止了该文件的自动播放功能。
由于该用户没有发表自动播放多媒体文件的权限或者该版面被设置成不支持多媒体播放。

 回到顶部
帅哥哟,离线,有人找我吗?
admin
  2楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1843 积分:24846 威望:0 精华:34 注册:2003/12/30 16:34:32
带有增加秒数功能的时钟类  发帖心情 Post By:2011/3/27 14:31:33 [只看该作者]

class Clock {
        private int hour;
        private int minute;
        private int second;

        public Clock() {
        }

        public Clock(int h, int m, int s) {
                setTime(h, m, s);
        }

        public int getHour() {
                return hour;
        }

        public void setHour(int hour) {
                if (hour >= 0 && hour <= 23)
                        this.hour = hour;
                else if (hour > 0)
                        this.hour = hour % 24;
                else
                        this.hour = 0;
        }

        public int getMinute() {
                return minute;
        }

        public void setMinute(int minute) {
                if (minute >= 0 && minute <= 59)
                        this.minute = minute;
                else
                        this.minute = minute % 60;
        }

        public int getSecond() {
                return second;
        }

        public void setSecond(int second) {
                if (second >= 0 && second <= 59)
                        this.second = second;
                else
                        this.second = second % 60;
        }

        public void setTime(int h, int m, int s) {
                setHour(h);
                setMinute(m);
                setSecond(s);
        }

        public String toString() {
                java.text.DecimalFormat df = new java.text.DecimalFormat("00");
                return df.format(hour) + "小时" + df.format(minute) + "分钟"
                                + df.format(second) + "秒";
        }

        public void addSecond(int s) {
                second = second + s;
                if (second >= 60) {
                        minute = minute + second / 60;
                        second = second % 60;
                }
                if (minute >= 60) {
                        hour = hour + minute / 60;
                        minute = minute % 60;
                }
                if (hour >= 23) {
                        hour = hour % 24;
                }
        }
}

public class Exec {
        public static void main(String[] args) {
                Clock beijing = new Clock(12, 23, 45);
                beijing.addSecond(21);
                System.out.println(beijing);
        }
}


 回到顶部
帅哥哟,离线,有人找我吗?
admin
  3楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1843 积分:24846 威望:0 精华:34 注册:2003/12/30 16:34:32
带有相加和约分功能的分数类  发帖心情 Post By:2011/3/27 14:32:32 [只看该作者]

class FS {
        private int fz = 1;
        private int fm = 1;

        public FS() {
        }

        public FS(double d) {
                int decimalCount = 1;
                int dLen = String.valueOf(d).length();
                for (int i = 0; i < dLen; i++) {
                        d = d * 10;
                        decimalCount *= 10;
                }
                fz = (int) d;
                fm = decimalCount;
                yf();
        }

        public FS(int fz, int fm) {
                setFS(fz, fm);
        }

        public int getFz() {
                return fz;
        }

        public void setFz(int fz) {
                this.fz = fz;
        }

        public int getFm() {
                return fm;
        }

        public void setFm(int fm) {
                if (fm != 0)
                        this.fm = fm;
                else
                        fm = 1;
        }

        public void setFS(int fz, int fm) {
                setFz(fz);
                setFm(fm);
        }

        public String toString() {
                return fz + "/" + fm;
        }

        public FS plus(FS fs) {
                FS result = new FS();
                result.setFz(fz * fs.fm + fs.fz * fm);
                result.setFm(fm * fs.fm);
                result.yf();
                return result;
        }

        public void yf() {
                int minValue = Math.min(Math.abs(fz), Math.abs(fm));
                for (int i = minValue; i >= 1; i--) {
                        if (fz % i == 0 && fm % i == 0) {
                                fz = fz / i;
                                fm = fm / i;
                                break;
                        }
                }
        }
}

public class Exec {
        public static void main(String[] args) {
                FS fs1 = new FS(0.5);
                FS fs2 = new FS(3, 5);
                FS fs3 = fs1.plus(fs2);
                System.out.println(fs3);
        }
}


 回到顶部