当前位置:   article > 正文

java上机任务(四)———CSDN我又回来了_haiertv调用setchannel方法,并传参数,5

haiertv调用setchannel方法,并传参数,5
  1. public class Text {
  2. /**
  3. * 程序入口
  4. */
  5. public static void main(String[] args) {
  6. Vehicle car1,car2;
  7. //使用new运算符和默认的构造方法创建对象car1
  8. car1=new Vehicle();
  9. //使用new运算符和默认的构造方法创建对象car2
  10. car2=new Vehicle();
  11. car1.setPower(128);
  12. car2.setPower(76);
  13. System.out.println("car1的功率是:"+car1.getPower());
  14. System.out.println("car2的功率是:"+car2.getPower());
  15. //car1调用speedUp()方法将自己的speed的值增加80
  16. car1.speedUp(80);
  17. //car2调用speedUp()方法将自己的speed的值增加80
  18. car2.speedUp(80);
  19. System.out.println("car1目前的速度:"+car1.getSpeed());
  20. System.out.println("car1目前的速度:"+car2.getSpeed());
  21. car1.speedDown(10);
  22. car2.speedDown(20);
  23. System.out.println("car1目前的速度:"+car1.getSpeed());
  24. System.out.println("car1目前的速度:"+car2.getSpeed());
  25. //家中的电视
  26. Tv haierTv=new Tv();
  27. //haierTv调用setChannel(int m),并向参数m传递5
  28. haierTv.setChannal(5);
  29. System.out.println("haierTv的频道是"+haierTv.getChannel());
  30. Family zhangsanFamily=new Family();
  31. //zhangsanFamily调用void buyTv(Tv tv)方法,并将haierTv传递给参数Tv
  32. zhangsanFamily.buyTv(haierTv);
  33. System.out.println("zhangsanFamily开始看电视节目");
  34. zhangsanFamily.seeTv();
  35. int m=2;
  36. System.out.println("zhangsanFamily将电视更换到"+m+"频道");
  37. zhangsanFamily.remoteComtrol(m);
  38. System.out.println("haierTv的频道是"+haierTv.getChannel());
  39. System.out.println("zhangsanFamily再看电视节目");
  40. zhangsanFamily.seeTv();
  41. //共饮同井水
  42. //用类名调用setWaterAmount(int m),并向参数传值200
  43. Village.setWaterAmount(200);
  44. int leftWater=Village.waterAmount; //用Village类的类名访问waterAmount
  45. System.out.println("水井中有"+leftWater+"升水");
  46. Village zhaoZhuang,maJiaHeZi;
  47. zhaoZhuang=new Village("赵庄");
  48. maJiaHeZi=new Village("马家河子");
  49. zhaoZhuang.setPeopleNumber(80);
  50. maJiaHeZi.setPeopleNumber(120);
  51. //zhaoZhuang调用drinkWater(int n),并向参数传值50
  52. zhaoZhuang.drinkWater(50);
  53. leftWater=maJiaHeZi.lookWaterAmount(); //maJiaHeZi调用lookWaterAmount()
  54. String name=maJiaHeZi.name;
  55. System.out.println(name+"发现水井中有"+leftWater+"升水");
  56. maJiaHeZi.drinkWater(100);
  57. leftWater=zhaoZhuang.lookWaterAmount();//zhaozhuang调用lookWaterAmount()方法
  58. name=zhaoZhuang.name;
  59. System.out.println(name+"发现水井中有"+leftWater+"升水");
  60. int peopleNumber=zhaoZhuang.getPeopleNumber();
  61. System.out.println("赵庄的人口:"+peopleNumber);
  62. peopleNumber=maJiaHeZi.getPeopleNumber();
  63. System.out.println("马家河子的人口:"+peopleNumber);
  64. // TODO Auto-generated method stub
  65. }
  66. }
  67. public class Vehicle {
  68. //声明double型变量speed,刻画速度
  69. double speed;
  70. //声明int型变量power,刻画功率
  71. int power;
  72. void speedUp(int s){
  73. //将参数s的值与成员变量speed的和赋值给成员变量speed
  74. speed=speed+s;
  75. }
  76. void speedDown(int d){
  77. //将成员变量speed与参数d的差赋值给成员变量speed
  78. speed=speed-d;
  79. }
  80. void setPower(int p){
  81. //将参数p的值赋值给成员变量power
  82. power=p;
  83. }
  84. int getPower(){
  85. //返回成员变量power的值
  86. return power;
  87. }
  88. double getSpeed(){
  89. return speed;
  90. }
  91. }
  92. public class Tv {
  93. int channel;//电视频道
  94. void setChannal(int m){
  95. if(m>=1){
  96. channel=m;
  97. }
  98. }
  99. int getChannel(){
  100. return channel;
  101. }
  102. void showProgram(){
  103. switch(channel){
  104. case 1:System.out.println("综合频道");
  105. break;
  106. case 2:System.out.println("经济频道");
  107. break;
  108. case 3:System.out.println("文艺频道");
  109. break;
  110. case 4:System.out.println("国际频道");
  111. break;
  112. case 5:System.out.println("体育频道");
  113. break;
  114. default :System.out.println("不能收看"+channel+"频道");
  115. }
  116. }
  117. }
  118. public class Family {
  119. Tv homeTv;
  120. void buyTv(Tv tv){
  121. //将参数tv赋值给homeTv
  122. homeTv=tv;
  123. }
  124. void remoteComtrol(int m){
  125. homeTv.setChannal(m);
  126. }
  127. void seeTv(){
  128. homeTv.showProgram();//homeTv调用showProgram()方法
  129. }
  130. }
  131. public class Village {
  132. static int waterAmount;//模拟水井的水量
  133. int peopleNumber;//村庄的人数
  134. String name;
  135. Village(String s){
  136. name=s;
  137. }
  138. static void setWaterAmount(int m){
  139. if(m>0)
  140. waterAmount=m;
  141. }
  142. void drinkWater(int n){
  143. if(waterAmount-n>=0){
  144. waterAmount=waterAmount-n;
  145. System.out.println(name+"喝了"+n+"升水");
  146. }
  147. else
  148. waterAmount=0;
  149. }
  150. static int lookWaterAmount(){
  151. return waterAmount;
  152. }
  153. void setPeopleNumber(int n){
  154. peopleNumber=n;
  155. }
  156. int getPeopleNumber(){
  157. return peopleNumber;
  158. }
  159. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/475629
推荐阅读
相关标签
  

闽ICP备14008679号