长方形类的案例
- class Test3 {
- public static void main(String[] args) {
- Rectangle r = new Rectangle();
- r.setLength(14);
- r.setWide(20);
- System.out.println(r.getZhouChang());
- System.out.println(r.getMianJi());
- }
- }
- class Rectangle {
- private int length;
- private int wide;
- public Rectangle() {}
- public Rectangle(int length,int wide) {
- this.length = length;
- this.wide = wide;
- }
- public void setLength(int length) {
- this.length = length;
- }
- public int getLength() {
- return length;
- }
- public void setWide(int wide) {
- this.wide = wide;
- }
- public int getWide() {
- return wide;
- }
- public int getZhouChang(){
- return (wide+length)*2;
- }
- public int getMianJi(){
- return wide*length;
- }
- }
复制代码 |
|