标签:
(可从日常生活中抽取)
测试。
属性、方法的功能与含义等)。
1 /* 类所封装的内容:对播放器的频道及开关状态进行调整 2 * 属性:调高、调低频道;开、关播放器 3 */ 4 package Homework; //储存在我的电脑上的 Homework 包中 5 6 public class No01Homework { //公共方法No01Homework 7 public static void main(String[] args) { //主方法 8 Browser browser1 = new Browser(); 9 System.out.println("The browser1‘s channel is " + browser1.channel); 10 Browser browser2 = new Browser(100); 11 System.out.println("The browser2‘s channel is " + browser2.channel); 12 13 browser1.turnOn(); 14 browser1.setChannel(25); 15 System.out.println("The browser1‘s channel now is " + browser1.channel); 16 17 browser1.channelUp(); 18 System.out.println("The browser1‘s channel now is " + browser1.channel); 19 20 browser1.channelDown(); 21 System.out.println("The browser1‘s channel now is " + browser1.channel); 22 23 browser1.turnOff(); 24 } 25 } 26 27 28 class Browser { //创建Browser方法 29 int channel; //定义int变量channel 30 boolean on = false; //定义boolean变量false 31 32 Browser() { //默认constructor 33 channel = 1; 34 } 35 36 Browser(int newChannel) { //constructor 37 channel = newChannel; 38 } 39 40 public void turnOn() { //功能:打开播放器 41 on = true; 42 } 43 44 public void turnOff() { //功能:关闭播放器 45 on = false; 46 } 47 48 public void setChannel(int newChannel) { //功能:设置新频道 49 if(on && newChannel >= 1 && newChannel <= 100) 50 channel = newChannel; 51 } 52 53 public void channelUp() { //功能:上调一个频道 54 if (on && channel < 100) 55 channel++; 56 } 57 58 public void channelDown() { //功能:下调一个频道 59 if (on && channel > 1) 60 channel--; 61 } 62 } 63 64
标签:
原文地址:http://www.cnblogs.com/QingHuan/p/4982190.html