package com.hanqi; //import java.util.*; public class JuXing { //属性 private double chang; //长 private double kuan; //宽 public double getChang() //读 { return chang; } //写 public void setChang(double chang) { //加验证 if(chang > 0) { this.chang = chang; } else { System.out.println("长度值无效"); } } public double getKuan() { return kuan; } public void setKuan(double kuan) { if(kuan > 0) { this.kuan = kuan; } else { System.out.println("宽度值无效"); } } //无参的默认构造方法 public JuXing() { } //给属性赋初始值 public JuXing(double chang,double kuan) { setChang(chang); setKuan(kuan); } //方法 //面向过程的写法(错误示范) public double getMianJi(double chang,double kuan) { return chang * kuan; } //面向对象的写法(正确示范) public double getMianJi() { return this.chang * this.kuan; } }
package com.hanqi; public class cx { private double chang; private double kuan; public double getChang() { return chang; } public void setChang(double chang) { if(chang > 0) { this.chang = chang; } else { System.out.println("骚年,你逗我??"); } } public double getKuan() { return kuan; } public void setKuan(double kuan) { if(kuan <= 0) { System.out.println("数学是体育老师教的??"); } else { this.kuan = kuan; } } cx() { } cx(double chang,double kuan) { setChang(chang); setKuan(kuan); } public double Mianji() { return this.chang * kuan; } }
package com.hanqi; public class TestPC { public static void main(String[] args) { // TODO 自动生成的方法存根 JuXing jx = new JuXing(); jx.setChang(10); jx.setKuan(5); System.out.println("面积 = "+jx.getMianJi()); cx cfx = new cx(); cfx.setChang(1); cfx.setKuan(2); System.out.println("面积 = "+ cfx.Mianji()); for(int i = 0;i < 10; i++) { TestStatic ts = new TestStatic(); ts.add(); } TestStatic.add2(1, 2); } }