package chapter9;//listing 9-2publicclassAlternativeCircle{/** Main method */publicstaticvoidmain(String[] args){// Create a circle with radius 1
AlternativeCircle circle1 =newAlternativeCircle();
System.out.println("The area of the circle of radius "+ circle1.radius +" is "+ circle1.getArea());// Create a circle with radius 25
AlternativeCircle circle2 =newAlternativeCircle(25);
System.out.println("The area of the circle of radius "+ circle2.radius +" is "+ circle2.getArea());// Create a circle with radius 125
AlternativeCircle circle3 =newAlternativeCircle(125);
System.out.println("The area of the circle of radius "+ circle3.radius +" is "+ circle3.getArea());// Modify circle radius
circle2.radius =100;
System.out.println("The area of the circle of radius "+ circle2.radius +" is "+ circle2.getArea());}double radius;/** Construct a circle with radius 1 */AlternativeCircle(){
radius =1;}/** Construct a circle with a specified radius */AlternativeCircle(double newRadius){
radius = newRadius;}/** Return the area of this circle */doublegetArea(){return radius * radius * Math.PI;}/** Return the perimeter of this circle */doublegetPerimeter(){return2* radius * Math.PI;}/** Set a new radius for this circle */voidsetRadius(double newRadius){
radius = newRadius;}}
定义TV类的程序:
package chapter9;//listing 9-3publicclassTV{int channel =1;// Default channel is 1int volumeLevel =1;// Default volume level is 1boolean on =false;// By default TV is offpublicTV(){}publicvoidturnOn(){
on =true;}publicvoidturnOff(){
on =false;}publicvoidsetChannel(int newChannel){if(on && newChannel >=1&& newChannel <=120)
channel = newChannel;}publicvoidsetVolume(int newVolumeLevel){if(on && newVolumeLevel >=1&& newVolumeLevel <=7)
volumeLevel = newVolumeLevel;}publicvoidchannelUp(){if(on && channel <120)
channel++;}publicvoidchannelDown(){if(on && channel >1)
channel--;}publicvoidvolumeUp(){if(on && volumeLevel <7)
volumeLevel++;}publicvoidvolumeDown(){if(on && volumeLevel >1)
volumeLevel--;}}
使用TV类创建两个对象的程序
package chapter9;//listing 9-4publicclassTestTV{publicstaticvoidmain(String[] args){
TV tv1 =newTV();
tv1.turnOn();
tv1.setChannel(30);
tv1.setVolume(3);
TV tv2 =newTV();
tv2.turnOn();
tv2.channelUp();
tv2.channelUp();
tv2.volumeUp();
System.out.println("tv1's channel is "+ tv1.channel
+" and volume level is "+ tv1.volumeLevel);
System.out.println("tv2's channel is "+ tv2.channel
+" and volume level is "+ tv2.volumeLevel);}}
{/** The radius of the circle */privatedouble radius =1;/** The number of the objects created */privatestaticint numberOfObjects =0;/** Construct a circle with radius 1 */publicCircleWithPrivateDataFields(){
numberOfObjects++;}/** Construct a circle with a specified radius */publicCircleWithPrivateDataFields(double newRadius){
radius = newRadius;
numberOfObjects++;}/** Return radius */publicdoublegetRadius(){return radius;}/** Set a new radius */publicvoidsetRadius(double newRadius){
radius =(newRadius >=0)? newRadius :0;}/** Return numberOfObjects */publicstaticintgetNumberOfObjects(){return numberOfObjects;}/** Return the area of this circle */publicdoublegetArea(){return radius * radius * Math.PI;}}
package chapter9;//listing 9-9publicclassTestCircleWithPrivateDataFields{/** Main method */publicstaticvoidmain(String[] args){// Create a Circle with radius 5.0
CircleWithPrivateDataFields myCircle =newCircleWithPrivateDataFields(5.0);
System.out.println("The area of the circle of radius "+ myCircle.getRadius()+" is "+ myCircle.getArea());// Increase myCircle's radius by 10%
myCircle.setRadius(myCircle.getRadius()*1.1);
System.out.println("The area of the circle of radius "+ myCircle.getRadius()+" is "+ myCircle.getArea());}}