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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ZhengJX 中级黑马   /  2015-9-21 21:43  /  563 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

长方形类的案例
  1. class Test3 {
  2.         public static void main(String[] args) {
  3.                 Rectangle r = new Rectangle();
  4.                 r.setLength(14);
  5.                 r.setWide(20);
  6.                 System.out.println(r.getZhouChang());
  7.                 System.out.println(r.getMianJi());
  8.         }
  9. }

  10. class Rectangle {
  11.         private int length;
  12.         private int wide;

  13.         public Rectangle() {}
  14.         public Rectangle(int length,int wide) {
  15.                 this.length = length;
  16.                 this.wide = wide;
  17.         }
  18.         public void setLength(int length) {
  19.                 this.length = length;
  20.         }
  21.         public int getLength() {
  22.                 return length;
  23.         }
  24.         public void setWide(int wide) {
  25.                 this.wide = wide;
  26.         }
  27.         public int getWide() {
  28.                 return wide;
  29.         }
  30.         public int getZhouChang(){
  31.                 return (wide+length)*2;
  32.         }
  33.         public int getMianJi(){
  34.                 return wide*length;
  35.         }
  36. }
复制代码

2 个回复

倒序浏览
这里体现了封装的思想。重载了构造方法,用到了this代表本类对象。
回复 使用道具 举报
我们班的啊{:2_30:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马