A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 逸风 中级黑马   /  2015-4-29 23:23  /  1092 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 逸风 于 2015-4-30 11:08 编辑

1. 请设计一个类Demo,这个类包含如下操作:
        A:求两个数的和。
        B:判断两个数是否相等。
        C:输出九九乘法表。
最后在测试类Test中进行测试。
class Title1 {
        int x;
        int y;
        public int sum() {
                return x+y;
        }
        
        public boolean compare() {
                return x==y;
        }
        
        public void multiplicationTable() {
                for(int n=1;n<=x;n++) {
                        for(int m=1;m<=n;m++) {
                                System.out.print(n+"*"+m+"="+n*m+"\t");
                        }
                        System.out.println();
                }
        }
}

class Text {
        public static void main(String[] args) {
                Title1 t = new Title1();
                t.x = 9;
                t.y = 9;
                System.out.println(t.sum());
                System.out.println(t.decide());
                t.multiplicationTable();
        }
}

2. 把今天视频中的装大象案例的伪代码,转换成可以运行的java代码,并以此了解面向对象的思想
class Icebox {
        String door;
        public void open() {
                System.out.println("打开"+door);
        }
        public void close() {
                System.out.println("关闭"+door);
        }
}

class Elephant {
        public void in() {
                System.out.println("大象进冰箱");
        }

}

class Title2 {
        public static void main(String[] args) {
                Icebox i = new Icebox();
                Elephant e = new Elephant();
                i.door = "冰箱门";
                i.open();
                e.in();
                i.close();
        }
}
第二题优化如下:
class Icebox {
String door = "冰箱门";
public void open() {
  System.out.println("打开"+door);
}
public void close() {
  System.out.println("关闭"+door);
}
}
class Elephant {
public void in() {
  System.out.println("大象进冰箱");
}
}
class DoSomer {
public void DoElephant(Elephant eh) {
  eh.in();
}
public void DoIcebox(Icebox ib) {
  ib.open();
}
public void DoIcebox1(Icebox ib) {
  ib.close();
}
}
class Title2 {
public static void main(String[] args) {
  DoSomer ds = new DoSomer();
  Icebox ib = new Icebox();
  ds.DoIcebox(ib);
  ds.DoElephant(new Elephant());
  ds.DoIcebox1(ib);
}
}






4 个回复

倒序浏览
厉害厉害。。。。。。。

点评

刚刚又进行优化了!  发表于 2015-4-30 11:12
回复 使用道具 举报
楼主程序运行上面没有错,我只想说在类名书写规范还有定义上要注意!类名首字母要大写。sun没看懂你是想写sum吧,还有第二个用compare要好点吧!

点评

嗯嗯,谢谢了!下午还会有改进。我下午再发!  发表于 2015-4-30 10:58

评分

参与人数 1黑马币 +5 收起 理由
逸风 + 5 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马