程序清单9-1 TestSimpleCircle.java
/*
* 需求:定义Circle类,并使用该类创建对象。三个圆对象,其半径分别为1、25和125,然后显示着三个圆的半径和面积。
* 然后将第二个对象的半径改为100,并显示它的新半径和面积。
*/
package com.echo.test9;
public class TestSimpleCircle {
public static void main(String[] args) {
// 创建半径为1的圆的对象
SimpleCircle circle1 = new SimpleCircle();
System.out.println("半径为" + circle1.radius + "的圆的面积是" + circle1.getArea());
// 创建半径为25的圆的对象
SimpleCircle circle2 = new SimpleCircle(25);
System.out.println("半径为" + circle2.radius + "的圆的面积是" + circle2.getArea());
// 创建半径为125的圆的对象
SimpleCircle circle3 = new SimpleCircle(125);
System.out.println("半径为" + circle3.radius + "的圆的面积是" + circle3.getArea());
//将第二个对象circle2的半径改为100
circle2.radius=100;
System.out.println("半径为" + circle2.radius + "的圆的面积是" + circle2.getArea());
}
}
class SimpleCircle {
double radius;
SimpleCircle() {
radius = 1;
}
SimpleCircle(double newRadius) {
radius = newRadius;
}
// 返回圆的面积
double getArea() {
return radius * radius * Math.PI;
}
// 返回圆的周长
double getPerimeter() {
return 2 * radius * Math.PI;
}
// 设置圆的半径
void setRadius(double newRadius) {
radius = newRadius;
}
}
程序清单9-2 SimpleCircle.java
/*
* 需求:定义Circle类,并使用该类创建对象。三个圆对象,其半径分别为1、25和125,然后显示着三个圆的半径和面积。
* 然后将第二个对象的半径改为100,并显示它的新半径和面积。
*/
package com.echo.test9;
public class SimpleCircle {
public static void main(String[] args) {
// 创建半径为1的圆的对象
SimpleCircle circle1 = new SimpleCircle();
System.out.println("半径为" + circle1.radius + "的圆的面积是" + circle1.getArea());
// 创建半径为25的圆的对象
SimpleCircle circle2 = new SimpleCircle(25);
System.out.println("半径为" + circle2.radius + "的圆的面积是" + circle2.getArea());
// 创建半径为125的圆的对象
SimpleCircle circle3 = new SimpleCircle(125);
System.out.println("半径为" + circle3.radius + "的圆的面积是" + circle3.getArea());
// 将第二个对象circle2的半径改为100
circle2.radius = 100;
System.out.println("半径为" + circle2.radius + "的圆的面积是" + circle2.getArea());
}
double radius;
SimpleCircle() {
radius = 1;
}
SimpleCircle(double newRadius) {
radius = newRadius;
}
// 返回圆的面积
double getArea() {
return radius * radius * Math.PI;
}
// 返回圆的周长
double getPerimeter() {
return 2 * radius * Math.PI;
}
// 设置圆的半径
void setRadius(double newRadius) {
radius = newRadius;
}
}
程序清单9-3 TV.java
package com.echo.test9;
public class TV {
int channel = 1;
int volumeLevel = 1;
boolean on = false;
public TV() {
}
public void turnOn() {
on = true;
}
public void turnOff() {
on = false;
}
public void setChannel(int newChannel) {
if (on && newChannel >= 1 && newChannel <= 120) {
channel = newChannel;
}
}
public void setVolume(int newVolumeLevel) {
if (on && newVolumeLevel >= 1 && newVolumeLevel <= 7) {
volumeLevel = newVolumeLevel;
}
}
public void channelUp() {
if (on && channel <= 120) {
channel++;
}
}
public void channelDown() {
if (on && channel >= 1) {
channel--;
}
}
public void volumeUp() {
if (on && volumeLevel <= 7) {
volumeLevel++;
}
}
public void volumeDown() {
if (on && volumeLevel >= 1) {
volumeLevel--;
}
}
}
程序清单9-4 TestTV.java
package com.echo.test9;
public class TestTV {
public static void main(String[] args) {
TV tv1 = new TV();
tv1.turnOn();
tv1.setChannel(30);
tv1.setVolume(3);
TV tv2 = new TV();
tv2.turnOn();
tv2.channelUp();
tv2.channelUp();
tv2.volumeUp();
System.out.println("tv1的频道是" + tv1.channel + "tv1的音量是" + tv1.volumeLevel);
System.out.println("tv2的频道是" + tv2.channel + "tv2的音量是" + tv2.volumeLevel);
}
}
程序清单9-6 CircleWithStaticMembers.java
package com.echo.test9;
public class CircleWithStaticMembers {
double radius;
static int numberOfObjects = 0;
CircleWithStaticMembers() {
radius = 1.0;
numberOfObjects++;
}
CircleWithStaticMembers(double newRadius) {
radius = newRadius;
numberOfObjects++;
}
static int getNumberOfObjects() {
return numberOfObjects;
}
double getArea() {
return radius * radius * Math.PI;
}
}
程序清单9-7 TestCircleWithStaticMembers.java
package com.echo.test9;
public class TestCircleWithStaticMembers {
public static void main(String[] args) {
System.out.println("圆对象创建之前");
System.out.println("圆对象的数量是:" + CircleWithStaticMembers.numberOfObjects);
// 创建c1
CircleWithStaticMembers c1 = new CircleWithStaticMembers();
System.out.println("创建c1之后");
System.out.println("c1半径是:" + c1.radius + "圆对象的数量是:" + c1.numberOfObjects);
// 创建c2
CircleWithStaticMembers c2 = new CircleWithStaticMembers(5);
// 修改c1
c1.radius = 9;
System.out.println("创建c1和修改c1之后");
System.out.println("c1半径是:" + c1.radius + "圆对象的数量是:" + CircleWithStaticMembers.numberOfObjects);
System.out.println("c2半径是:" + c2.radius + "圆对象的数量是:" + CircleWithStaticMembers.numberOfObjects);
}
}