java之给方法传递对象参数

java中给方法传递对象参数,和传递数组一样,传递对象实际上是传递对象的引用。
包括package的用法。
javac Jpro\Circes3.java
javac Jpro\Demo032902.java
java Jpro.Demo032902
Circles3.java的代码如下:

package Jpro;
public class Circles3{
    private double radius = 1;
    private static int numberOfObjects = 0;
    public Circles3(){
        numberOfObjects++;
    }
    public Circles3( double newRadius ){
        radius = newRadius;
        numberOfObjects++;
    }
    public double getRadius(){
        return radius;
    }
    public void setRadius( double newRadius ){
        radius = ( newRadius >= 0 )?newRadius:0;
    }
    public static int getNumberOfObject(){
        return numberOfObjects;
    }
    public double getArea(){
        return radius * radius * Math.PI;
    }
}

Demo032902.java代码如下:

package Jpro;
public class Demo032902{
    public static void main( String [] args ){
        Circles3 myCircle = new Circles3( 1 );
        int n = 5;
        printAreas( myCircle, n );

        System.out.println( "\n" + "radius is " + myCircle.getRadius() );
        System.out.println( "n = " +n );
    }
    public static void printAreas( Circles3 c, int times ){
        System.out.println( "Radius \t \t area" );
        while ( times >= 1 ){
            System.out.println( c.getRadius() + "\t\t" + c.getArea() );
            c.setRadius( ( c.getRadius() + 1 ) );
            times--;
        }
    }
}

你可能感兴趣的:(java)