书中第四章例子代码(一)

class Lader{

    double above,bottom,height;
    Lader(){}
    Lader(double a,double b,double h){
        above=a;
        bottom=b;
        height=h;
    }
    public void setAbove(double a){
        above=a;
    }
    public void setBottom(double b){
        bottom=b;
    }
    public void setHeight(double h){
        height=h;
    }
    double computeArea(){
       return (above+bottom)*height/2.0;
    }
}
public class Example4_1{
  public static void main(String args[]){
       double area1=0,area2=0;
       Lader laderOne,laderTwo;
       laderOne=new Lader();
       laderTwo=new Lader(10,88,20);
       laderOne.setAbove(16);
       laderOne.setBottom(26);
       laderOne.setHeight(100);
       laderTwo.setAbove(300);
       laderTwo.setBottom(500);
       area1=laderOne.computeArea();
       area2=laderTwo.computeArea();
       System.out.println("laderOne的above,bottom和height:"+                                         laderOne.above+","+laderOne.bottom+","+laderOne.height);
       System.out.println("laderOne的面积:"+area1);
       System.out.println("laderTwo的above,bottom和height:"+
                       laderTwo.above+","+laderTwo.bottom+","+laderTwo.height);
       System.out.println("laderTwo的面积:"+area2);
    }

}



class Lader{

  double above,height;    //实例变量
    static double bottom;   //类变量
    void setAbove(double a){
       above=a;
    }
    void setBottom(double b){
       bottom=b;
    }
    double getAbove(){
       return above;
    }
    double getBottom(){
       return bottom;
    }
}
class Example4_2{
    public static void main(String args[]){
       Lader.bottom=60;    //Lader的字节码被加载到内存,通过类名操作类变量
       Lader laderOne,laderTwo;
       System.out.println("现在所有Lader对象的bottom都是"+Lader.bottom);    
       laderOne=new Lader();  
       laderTwo=new Lader();
       System.out.println("laderOne的bottom:"+laderOne.getBottom());
       System.out.println("laderTwo的bottom:"+laderTwo.getBottom());
       laderOne.setAbove(11);
       laderTwo.setAbove(22);
       laderTwo.setBottom(100);
       System.out.println("现在所有Lader对象的bottom都是"+Lader.bottom);
       System.out.println("laderOne的above:"+laderOne.getAbove());
       System.out.println("laderTwo的above:"+laderTwo.getAbove());
    } 
}

class Tom{
    final int MAX=100;
    static final int MIN=20; 
}
public class Example4_3{
    public static void main(String args[ ]){
        System.out.println(Tom.MIN);
        Tom cat=new Tom();
        int x=0;
        x=Tom.MIN+cat.MAX;
        System.out.println(x);
    }
}


class Tom{
    void f(int x,double y){
        x=x+1;
        y=y+1;
        System.out.printf("参数x和y的值分别是:%d,%3.2f\n",x,y);
    } 
}
public class Example4_5{
    public static void main(String args[]){
        int x=10;
        double y=12.58; 
        Tom cat=new Tom();
        cat.f(x,y);
        System.out.printf("main方法中x和y的值仍然分别是:%d,%3.2f\n",x,y);
    }
}


public class Tom{
    int leg;
    Tom(int n){
       this.cry();   //可以省略this,即将this.cry();写成cry();
       leg=n;
       this.cry(); 
    }
    void cry(){
       System.out.println("我是Tom ,我现在有"+leg+"条腿");
    }
    public static void main(String args[]){
       Tom cat=new Tom(4);  //当调用构造方法Tom时,其中的this就是对象cat
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值