赞
踩
- public class Text {
-
- /**
- * 程序入口
- */
- public static void main(String[] args) {
- Vehicle car1,car2;
- //使用new运算符和默认的构造方法创建对象car1
- car1=new Vehicle();
- //使用new运算符和默认的构造方法创建对象car2
- car2=new Vehicle();
- car1.setPower(128);
- car2.setPower(76);
- System.out.println("car1的功率是:"+car1.getPower());
- System.out.println("car2的功率是:"+car2.getPower());
- //car1调用speedUp()方法将自己的speed的值增加80
- car1.speedUp(80);
- //car2调用speedUp()方法将自己的speed的值增加80
- car2.speedUp(80);
- System.out.println("car1目前的速度:"+car1.getSpeed());
- System.out.println("car1目前的速度:"+car2.getSpeed());
- car1.speedDown(10);
- car2.speedDown(20);
- System.out.println("car1目前的速度:"+car1.getSpeed());
- System.out.println("car1目前的速度:"+car2.getSpeed());
- //家中的电视
- Tv haierTv=new Tv();
- //haierTv调用setChannel(int m),并向参数m传递5
- haierTv.setChannal(5);
- System.out.println("haierTv的频道是"+haierTv.getChannel());
- Family zhangsanFamily=new Family();
- //zhangsanFamily调用void buyTv(Tv tv)方法,并将haierTv传递给参数Tv
- zhangsanFamily.buyTv(haierTv);
- System.out.println("zhangsanFamily开始看电视节目");
- zhangsanFamily.seeTv();
- int m=2;
- System.out.println("zhangsanFamily将电视更换到"+m+"频道");
- zhangsanFamily.remoteComtrol(m);
- System.out.println("haierTv的频道是"+haierTv.getChannel());
- System.out.println("zhangsanFamily再看电视节目");
- zhangsanFamily.seeTv();
- //共饮同井水
- //用类名调用setWaterAmount(int m),并向参数传值200
- Village.setWaterAmount(200);
- int leftWater=Village.waterAmount; //用Village类的类名访问waterAmount
- System.out.println("水井中有"+leftWater+"升水");
- Village zhaoZhuang,maJiaHeZi;
- zhaoZhuang=new Village("赵庄");
- maJiaHeZi=new Village("马家河子");
- zhaoZhuang.setPeopleNumber(80);
- maJiaHeZi.setPeopleNumber(120);
- //zhaoZhuang调用drinkWater(int n),并向参数传值50
- zhaoZhuang.drinkWater(50);
- leftWater=maJiaHeZi.lookWaterAmount(); //maJiaHeZi调用lookWaterAmount()
- String name=maJiaHeZi.name;
- System.out.println(name+"发现水井中有"+leftWater+"升水");
- maJiaHeZi.drinkWater(100);
- leftWater=zhaoZhuang.lookWaterAmount();//zhaozhuang调用lookWaterAmount()方法
- name=zhaoZhuang.name;
- System.out.println(name+"发现水井中有"+leftWater+"升水");
- int peopleNumber=zhaoZhuang.getPeopleNumber();
- System.out.println("赵庄的人口:"+peopleNumber);
- peopleNumber=maJiaHeZi.getPeopleNumber();
- System.out.println("马家河子的人口:"+peopleNumber);
-
-
- // TODO Auto-generated method stub
-
- }
-
- }
- public class Vehicle {
- //声明double型变量speed,刻画速度
- double speed;
- //声明int型变量power,刻画功率
- int power;
- void speedUp(int s){
- //将参数s的值与成员变量speed的和赋值给成员变量speed
- speed=speed+s;
- }
- void speedDown(int d){
- //将成员变量speed与参数d的差赋值给成员变量speed
- speed=speed-d;
- }
- void setPower(int p){
- //将参数p的值赋值给成员变量power
- power=p;
- }
- int getPower(){
- //返回成员变量power的值
- return power;
- }
- double getSpeed(){
- return speed;
- }
-
- }
-
- public class Tv {
- int channel;//电视频道
- void setChannal(int m){
- if(m>=1){
- channel=m;
- }
- }
- int getChannel(){
- return channel;
- }
- void showProgram(){
- switch(channel){
- case 1:System.out.println("综合频道");
- break;
- case 2:System.out.println("经济频道");
- break;
- case 3:System.out.println("文艺频道");
- break;
- case 4:System.out.println("国际频道");
- break;
- case 5:System.out.println("体育频道");
- break;
- default :System.out.println("不能收看"+channel+"频道");
-
- }
- }
-
- }
-
- public class Family {
- Tv homeTv;
- void buyTv(Tv tv){
- //将参数tv赋值给homeTv
- homeTv=tv;
- }
- void remoteComtrol(int m){
- homeTv.setChannal(m);
- }
- void seeTv(){
- homeTv.showProgram();//homeTv调用showProgram()方法
- }
-
- }
-
- public class Village {
- static int waterAmount;//模拟水井的水量
- int peopleNumber;//村庄的人数
- String name;
- Village(String s){
- name=s;
- }
- static void setWaterAmount(int m){
- if(m>0)
- waterAmount=m;
- }
- void drinkWater(int n){
- if(waterAmount-n>=0){
- waterAmount=waterAmount-n;
- System.out.println(name+"喝了"+n+"升水");
- }
- else
- waterAmount=0;
- }
- static int lookWaterAmount(){
- return waterAmount;
- }
- void setPeopleNumber(int n){
- peopleNumber=n;
- }
- int getPeopleNumber(){
- return peopleNumber;
- }
-
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。