第四章代码

输出圆的面积 java应用程序

publicclass ComputerCircleArea {

    publicstaticvoid main(String ags[]){

       double radius;

       double area;

       radius=163.16;

       area=3.14*radius*radius;

       System.out.println("半径是"+radius+"的圆的面积是:"+area);

    }

}

使用Circle类创建circle对象,只需要让这个对象分别计算面积即可

class Circle{

    doubleradius;

    double getArea(){

       double area=3.14*radius*radius;

       return area;

    }

}

publicclass Example {

    publicstaticvoid main(String ags[]){

    Circle circle;

    circle=new Circle();

    circle.radius=163.16;

    double area=circle.getArea();

    System.out.println("半径是"+circle.radius+"的圆的面积是:"+area);

    }

}

在主类Example的main方法中使用Lader类创建两个对象laderOne和laderTwo

class Lader{

    doubleabove,bottom,height;

    Lader(){}

    Lader(double a,double b,double c){

       above=a;

       bottom=b;

       height=c;

    }

    publicvoid setAbove(double a){

       above=a;

    }

    publicvoid setBottom(double b){

       bottom=b;

    }

    publicvoid setHeight(double h){

       height=h;

    }

    double computerArea(){

       return (above+bottom)/2*height;

    }

}

publicclass Example {

    publicstaticvoid main(String ags[]){

       Lader laderOne;

       Lader laderTwo;

       double area1=0,area2=0;

       laderOne=new Lader();

       area1=laderOne.computerArea();

       System.out.println("laderOne的面积:"+area1);

       laderTwo=new Lader(10,88,20);

       area2=laderTwo.computerArea();

       System.out.println("laderTow的面积:"+area2);

       laderOne.setAbove(16);

       laderOne.setBottom(26);

       laderOne.setHeight(100);

       laderTwo.setAbove(300);

       laderTwo.setBottom(500);

       System.out.println("laderOneabove,bottom,height:"+laderOne.above+","+laderOne.bottom+","+laderOne.height);

       System.out.println("laderOne的面积是:"+laderOne.computerArea());

       System.out.println("laderTowabove,bottom,height:"+laderTwo.above+","+laderTwo.bottom+","+laderTwo.height);

       System.out.println("laderTow的面积是:"+laderTwo.computerArea());

    }

}

两个Lader共享bottom

class Lader{

    doubleabove,height;

    static doublebottom;

    void setAbove(double a){

       above=a;

    }

    void setBottom(double b){

       bottom=b;

    }

    double getAbove(){

        returnabove;

    }

    double getBottom(){

       returnbottom;

    }

}

publicclass Example {

    publicstaticvoid main(String ags[]){

       Lader.bottom=60;

       Lader laderOne,laderTwo;

       System.out.println("现在所有Lader对象bottom都是"+Lader.bottom);

       laderOne=new Lader();

       laderTwo=new Lader();

       System.out.println("laderOnebottom"+laderOne.getBottom());

       System.out.println("laderTwobottom"+laderTwo.getBottom());

       laderOne.setAbove(11);

       laderTwo.setAbove(22);

       laderOne.setBottom(100);

       System.out.println("现在所有Lader对象的bottom都是"+Lader.bottom);

       System.out.println("laderOneabove"+laderOne.getAbove());

       System.out.println("laderTwoabove"+laderTwo.getAbove());

       System.out.println("laderOnebottom"+laderOne.getBottom());

       System.out.println("laderTwobottom"+laderTwo.getBottom());

    }

}

常量的用法

class Tom{

    finalintMAX=100;

    staticfinalintMIN=20;

}

publicclass Example {

    publicstaticvoid main(String ags[]){

       System.out.println(Tom.MIN);

       Tom cat=new Tom();

       int x=0;

       x=Tom.MIN+cat.MAX;

       System.out.println(x);

    }

}

通过类名调用类方法

class Computer{

    doublex,y;

    staticdouble max(double a,double b){

       return a>b?a:b;

    }

}

publicclass Example {

    publicstaticvoid main(String ags[]){

       double max=Computer.max(12,45);

       System.out.println(max);

    }

}

向一个方法的基本数据类型参数传值

class Tom{

    void f(int x,double y){

       x=x+1;

       y=y+1;

       System.out.println("参数xy的值分别是:"+x+","+y);

    }

}

publicclass Example {

    publicstaticvoid main(String ags[]){

       int x=10;

       double y=12.58;

       Tom cat=new Tom();

       cat.f(x, y);

       System.out.println("main方法中xy的值仍然分别是:"+x+","+y);

    }

}

 

Tom类中的方法f的参数mouse是Jerry类声明的对象,数组属于引用类型参数

class Jerry{

    intleg;

    Jerry(int n){

       leg=n;

    }

    void setLeg(int n){

       leg=n;

    }

    int getLeg(){

       returnleg;

    }

}

class Tom{

    void f(Jerry mouse){

       mouse.setLeg(12);

       System.out.println("在执行方法f时,参数mouse修改了自己的leg的值");

       System.out.println("当前参数mouse的成员leg的值:"+mouse.getLeg());

       mouse=null;

       //mouse.setLeg(12);

    }

}

publicclass Example {

    publicstaticvoid main(String ags[]){

       Tom cat=new Tom();

       Jerry jerry=new Jerry(2);

       System.out.println("在调用方法f之前,jerry的成员leg的值:"+jerry.getLeg());

       cat.f(jerry);

       System.out.println("在调用方法f之后,jerry的成员leg的值:"+jerry.getLeg());

    }

}

类Cone在创建对象时,将一个Circle对象的引用“传值”给Cone对象的bottom

class Circle{

    doubleradius;

    Circle(double r){

       radius=r;

    }

    double computerArea(){

       return 3.14*radius*radius;

    }

    void setRadius(double newRadius){

       radius=newRadius;

    }

    double getRadius(){

       returnradius;

    }

}

class Cone{

    Circle bottom;

    doubleheight;

    Cone(Circle c,double h){

       bottom=c;

       height=h;

    }

    double computerVolume(){

       double volume;

       volume=bottom.computerArea()*height/3.0;

       return volume;

    }

    void setBottomRadius(double r){

       bottom.setRadius(r);

    }

    double getBottomRadius(){

       returnbottom.getRadius();

    }

}

publicclass Example {

    publicstaticvoid main(String ags[]){

       Circle circle=new Circle(8);

       Cone circular=new Cone(circle,18);

       System.out.println("circularbottom半径:"+circular.getBottomRadius());

       System.out.println("circular的体积"+circular.computerVolume());

       circular.setBottomRadius(88);

       System.out.println("circularbottom半径:"+circular.getBottomRadius());

       System.out.println("circular的体积:"+circular.computerVolume());

    }

}

类Area的getArea方法时重载方法

class People{

    double getArea(double x,int y){

       return x*y;

    }

    int getArea(int x,double y){

       return (int)(x*y);

    }

    double getArea(float x,float y,float z){

       return (x*x+y*y+z*z)*2.0;

    }

}

publicclass Example {

    publicstaticvoid main(String ags[]){

       People zhang=new People();

       System.out.println("面积:"+zhang.getArea(10, 3.88));

       System.out.println("面积:"+zhang.getArea(10.0, 8));

    }

}

使用java.util包中的Date类,用来显示本机的当前时间

import java.util.Date;

 

publicclass Example {

    publicstaticvoid main(String ags[]){

       Date date=new Date();

       System.out.println("本地机器的时间:"+date);

    }

}

publicclass Example{

    privateintmoney;

    Example(){

       money=200;

    }

    privateint getMoney(){

       returnmoney;

    }

    publicstaticvoid main(String ags[]){

       Example exa=new Example();

       exa.money=3000;

       int m=exa.getMoney();

       System.out.println("money="+m);

    }

}

用某个类在另外一个类中创建对象后,如果不希望对改对象直接访问自己的变量,即通过“.”运算符来操作自己的成员变量,就应当将该成员变量访问权限设置为private。面向对象编程提倡对象应当调用方法来改变自己的属性,我们应当提供操作数据的方法,这些方法可以经过精心的设计,使得对数据的操作更加合理。

class Employee{

    privatedoublesalary=1800;

    publicvoid setSalary(double salary){

       if(salary>1800&&salary<=6000){

           this.salary=salary;

       }

    }

    publicdouble getSalary(){

       returnsalary;

    }

}

publicclass Example{

    publicstaticvoid main(String ags[]){

       Employee zhang=new Employee();

       Employee wang=new Employee();

       zhang.setSalary(100);

       System.out.println("zhang的薪水:"+zhang.getSalary());

       wang.setSalary(3888);

       System.out.println("wang的薪水:"+wang.getSalary());

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值