/*圆的类*/ public class R { private double radius; // 构造方法,有参构造 public R(double radius) { this.radius = radius; } //方法的重载,参数不同 // 构造方法,无参构造 public R() { this.radius = 1; } // 求圆面积的方法 public double getArea() { return radius * radius * Math.PI; } //求圆的周长 public double getPerimeter() { return 2 * Math.PI * radius; } public void setRadius(double newRadius) { this.radius=newRadius; } }
public class TR { public static void main(String[] args) { R circle1=new R(); double area=circle1.getArea(); System.out.println(area); R circle2=new R(20); System.out.println(circle2.getArea()); System.out.println(circle1.getPerimeter()); System.out.println(circle2.getPerimeter()); double ridius=8; double areaCircle=Math.PI*ridius*ridius; System.out.println(areaCircle); circle2.setRadius(10); System.out.println(circle2.getArea()); } }
/*圆类的和并*/ public class TR { private double radius; public TR() { this.radius=1; } public TR(double radius){ this.radius=radius; } public double getArea() { return Math.PI*radius*radius; } public double getPerimeter() { return 2*Math.PI*radius; } public static void main(String[] args) { TR cir1=new TR(); System.out.println("The area of the circle of radius "+cir1.radius+" is "+cir1.getArea()); TR cir2=new TR(100); System.out.println("The area of the circle of radius "+cir2.radius+" is "+cir2.getArea()); } }

/*造电视*/ public class TV { public int channel=1; public int volumeLevel=1; public boolean on=false; public TV() { } public void turnOn() { on =true; System.out.println("电视已启动"); } public void turnOff() { on=false; System.out.println("电视已关闭"); } public int getChannel() { return channel; } public void setChannel(int channel) { if(on) { System.out.println("电视启动可以换台。"); if(channel>=1&&channel<=120) { this.channel = channel; System.out.println("频道已经调到 "+channel+" 台"); }else { System.out.println("你要调的频道不存在。"); } }else { System.out.println("电视关闭不能换台"); } } public int getVolumeLevel() { return volumeLevel; } public void setVolumeLevel(int volumeLevel) { if(on) { System.out.println("电视启动可调声音"); if(volumeLevel>=1&&volumeLevel<=7) { this.volumeLevel = volumeLevel; System.out.println("声音的大小设置成了 "+volumeLevel+" 大小"); } }else { System.out.println("电视关闭不能调声音"); } } 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--; } } }
public class TextTV { public static void main(String[] args) { TV tv1=new TV(); tv1.turnOff(); tv1.setChannel(30); tv1.setVolumeLevel(3); TV tv2=new TV(); tv2.turnOn(); System.out.println("CCTV1's channel is "+tv2.channel+" and volume is "+tv1.volumeLevel); tv2.channelUp(); System.out.println("CCTV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel); tv2.channelUp(); System.out.println("CCTV3's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel); tv2.channelUp(); System.out.println("CCTV4's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel); } }