JAVA 书中第四章的例题

这篇博客展示了JAVA书中第四章的多个例题,包括类的使用、包的导入、对象创建、变量和方法的实践,如Tom类、Example4_11类的日期显示、Example4_12类的二次方程求根等。此外,还讨论了类变量、构造方法、final关键字以及方法参数的作用域等概念。
摘要由CSDN通过智能技术生成
还有学校没有讲到的,也多少写了点:以备后用了:摘出了其中一部分,限于篇幅就不全贴出来了,这毕竟是技术论坛,不该发这些菜鸟级的代码的
package tom.jiafei;


class Tom{


    void speak(){


       System.out.println("Tom类在tom.jiafei包中");  


    }


}


public class Example4_10{


    public static void main(String args[]){


       Tom cat=new Tom();


       cat.speak();


       System.out.println("Example4_10类也在tom.jiafei包中");  


    }


}
import java.util.Date;
public class  Example4_11{
    public static void main(String args[]){
       Date date=new Date();
       System.out.printf("本地机器的时间:\n%s",date);  
    }
}


import tom.jiafei.*;
public class Example4_12{
    public static void main(String args[ ]){
       SquareEquation equation=new SquareEquation(4,5,1);
       equation.getRoots();
       equation.setCoefficient(-3,4,5);
       equation.getRoots();
    }
}


public class Example4_13{
    private int money;
    Example4_13(){
       money=2000;
    } 
    private int getMoney(){
       return money;
    }
    public static void main(String args[ ]){
       Example4_13 exa=new Example4_13(); //对象exa在Example4_13类中 
       exa.money=3000;
       int m=exa.getMoney();
       System.out.println("money="+m);
    }
}
贴图费劲,就不贴图了
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,l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值