1、例 2-1Example2_1.javapublic class Example2_1public static void main(String args)Rect rectangle1,rectangle2; /声明 2 个对象rectangle1 = new Rect(); /创建对象rectangle2 = new Rect();rectangle1.width=128;rectangle1.height=69;rectangle2.width=18.9;rectangle2.height=59.8;double area=rectangle1.getArea();System.out
2、.println(“rectangle1 的面积:“+area);area=rectangle2.getArea();System.out.println(“rectangle2 的面积:“+area);例 2-2Circle.javapublic class Circle double radius; /圆的半径double getArea() return 3.1415926*radius;Lader.javapublic class Laderdouble above; /梯形的上底 double bottom; /梯形的下底double height; /梯形的高double getA
3、rea() return (above+bottom)*height/2;MainClass.javapublic class MainClass public static void main(String args) Circle circle=new Circle();circle.radius=100;double area=circle.getArea();System.out.println(“圆的面积:“+area); Lader lader=new Lader();lader.above=10;lader.bottom=56;lader.height=8.9;area=lade
4、r.getArea();System.out.println(“梯形的面积:“+area); 例 2-3Rectangle.javapublic class Rectangle /Rectangle 类double width; double height; double getArea() return width*height;class Example2_3 /主类public static void main(String args) Rectangle r;r=new Rectangle();r.width=1.819;r.height=1.5;double area=r.getAr
5、ea();System.out.println(“矩形的面积:“+area);例 2-4Example2_4.javapublic class Example2_4 public static void main (String args )char ch1=国,ch2=庆;int p1=969,p2=12353;System.out.println(ch1+“在 Unicode 表中的位置:“+(int)ch1);System.out.println(ch2+“在 Unicode 表中的位置:“+(int)ch2); System.out.println(“第“+p1+“ 个位置上的字符是:
6、“+(char)p1);System.out.println(“第“+p2+“ 个位置上的字符是:“+(char)p2); 例 2-5People.javapublic class People float weight;int height;void speak() System.out.println(“我的身高是:“+height+“cm“);System.out.println(“我的体重是:“+weight+“kg“);Machine.javapublic class Machine public void estimate(int height,double weight) dou
7、ble number=(height-100)/weight;if(number=1.1)System.out.println(“偏瘦“);else if(number=0.96)System.out.println(“正常“);else if(number=10) System.out.println(“请给出 5 位数的彩票号码“);else int d1 = number%10; / 尾号int d2 = number%100 ; / 后 2 位数int d3 = number%1000 ;/ 后 3 位数switch(d1) case 9 :case 3 :case 1 : Syste
8、m.out.println(“彩票是三等奖“);图 3.5 判断中奖break;default: System.out.println(“彩票不是三等奖“);switch(d2) case 29 :case 46 :case 21 : System.out.println(“彩票是二等奖“);break; default: System.out.println(“彩票不是二等奖“);switch(d3) case 875 :case 326 :case 596 : System.out.println(“彩票是一等奖“);break;default: System.out.println(“彩
9、票不是一等奖“); Example3_2.javaimport java.util.Scanner;public class Example3_2 public static void main(String args) Scanner reader = new Scanner(System.in);System.out.println(“输入彩票号回车确认“);int number = reader.nextInt();Administrator person = new Administrator();person.giveMess(number);例 3-3ComputerSum.jav
10、apublic class ComputerSum void giveSum(int number,int length) if(number=1) long sum=0,a=number,item=a,n=length,i=1;for(i=1;imax) N=i-1;break;i+; System.out.println(“满足 1+2+.+n0)radius=r; double getRadius()return radius;double getArea()area=3.14*radius*radius;return area;Example4_4.javapublic class E
11、xample4_4 public static void main(String args) Circle circle=new Circle();double w=12.76;circle.setRadius(w);System.out.println(“圆的半径:“+circle.getRadius();System.out.println(“圆的面积:“+circle.getArea();System.out.println(“更改向方法参数 r 传递值的 w 的值为 100“);w=100;System.out.println(“w=“+w);System.out.println(“圆
12、的半径:“+circle.getRadius();例 4-5TV.javapublic class TV int channel; /电视频道void setChannel(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(“文艺频道“);bre
13、ak;case 4 : System.out.println(“国际频道“);break; case 5 : System.out.println(“体育频道“);break;default : System.out.println(“不能收看“+channel+“频道“); Chineses.javapublic class Chineses TV homeTV;void buyTV(TV tv) homeTV=tv;void remoteControl(int m) homeTV.setChannel(m); void seeTV() homeTV.showProgram();System
14、.out.println(“用户买回的电视是在“+homeTV.getChannel()+“频道“);Example4_5.javapublic class Example4_5 public static void main(String args) TV haierTV = new TV();haierTV.setChannel(5);System.out.println(“卖给用户的 haierTV 目前的频道是“+haierTV.getChannel();Chineses zhangsan = new Chineses();zhangsan.buyTV(haierTV);System.
15、out.println(“zhangsan 开始看电视节目“);zhangsan.seeTV();int m=2; System.out.println(“zhangsan 用遥控器将买回的电视更改到 “+m+“频道“);zhangsan.remoteControl(m);System.out.println(“现在卖给用户的 haierTV 目前的频道是“+haierTV.getChannel();System.out.println(“zhangsan 再看电视节目“);zhangsan.seeTV(); 例 4-6Computer.javapublic class Computer pu
16、blic int getSum(int. x) /x 是可变参数的参数代表int sum=0;for(int i=0;ic else isTriange=false;public void 计算面积() if(isTriange) double p=(sideA+sideB+sideC)/2.0;double area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC) ;System.out.println(“是一个三角形,面积是:“+area);else System.out.println(“不是一个三角形,不能计算面积“); public void 修改
17、三边(double a,double b,double c) sideA=a;sideB=b;sideC=c;if(a+bc else isTriange=false;例 4-15Example4_15.javaimport tom.jiafei.Triangle;public class Example4_14 public static void main(String args) Triangle tri=new Triangle(67,10);tri.计算面积 ();tri.修改三边 (3,4,5);tri.计算面积 ();例 4-16A.javapublic class A publ
18、ic void hello() System.out.println(“Hello“); Example4_16.javapublic class Example4_16 public static void main(String args) A a=new A();a.hello();例 4-17Yuan.javapublic class Yuan private double radius;public void setRadius(double r) if(r=0) radius=r;public double getRadius() return radius;double getA
19、rea() return 3.14*radius*radius;Example4_17.javapublic class Example4_17 public static void main(String args) Yuan circle=new Yuan(); circle.setRadius(123); System.out.println(“circle 的半径:“+circle.getRadius(); /circle.radius=-523;是非法的,因为 circle 不在 Student 类中 circle.setRadius(-523);System.out.println
20、(“circle 的半径:“+circle.getRadius(); 例 5-1People.javapublic class People double height=170,weight=67.9;protected void tellHeightAndWeight() System.out.printf(“我的体重和身高:%2.2fkg,%2.2fcmn“,weight,height); Student.javapublic class Student extends People int number;void tellNumber() System.out.println(“我的学号
21、是:“+number); int add(int x,int y) return x+y; int sub(int x,int y) return x-y; UniverStudent.javapublic class UniverStudent extends Student int multi(int x,int y) return x*y; double div(double x,double y) return x/y; Example5_1.javapublic class Example5_1 public static void main(String args) int x=1
22、2,y=18;Student zhang = new Student();zhang.weight=73.8;zhang.height=177;zhang.number=100101;zhang.tellHeightAndWeight();zhang.tellNumber(); System.out.print(“zhang 会做加减: “);int result=zhang.add(x,y);System.out.printf(“%d+%d=%dt“,x,y,result);result=zhang.sub(x,y);System.out.printf(“%d-%d=%dn“,x,y,res
23、ult); UniverStudent geng = new UniverStudent();geng.number=6609;geng.tellHeightAndWeight();geng.tellNumber(); System.out.print(“geng 会做加减乘除: “);result=geng.add(x,y);System.out.printf(“%d+%d=%dt“,x,y,result);result=geng.sub(x,y);System.out.printf(“%d-%d=%dt“,x,y,result); result=geng.multi(x,y);System
24、.out.printf(“%d%d=%dt“,x,y,result); double re=geng.div(x,y);System.out.printf(“%d%d=%fn“,x,y,re); 例 5-2A.javapublic class A private int x;public void setX(int x) this.x=x; public int getX() return x;B.javapublic class B extends A double y=12;public void setY(int y) /this.y=y+x; 非法,子类没有继承 xpublic dou
25、ble getY() return y;Example5_2.javapublic class Example5_2 public static void main(String args) B b=new B();b.setX(888);System.out.println(“子类对象未继承的 x 的值是:“+b.getX();b.y=12.678;System.out.println(“子类对象的实例变量 y 的值是:“+b.getY(); 例 5-3Goods.javapublic class Goods public double weight;public void oldSetWe
26、ight(double w) weight=w;System.out.println(“double 型的 weight=“+weight);public double oldGetPrice() double price = weight*10;return price;CheapGoods.javapublic class CheapGoods extends Goods public int weight;public void newSetWeight(int w) weight=w;System.out.println(“int 型的 weight=“+weight);public
27、double newGetPrice() double price = weight*10;return price;Example5_3.javapublic class Example5_3 public static void main(String args) CheapGoods cheapGoods=new CheapGoods();/cheapGoods.weight=198.98; 是非法的,因为子类对象的 weight 已经不是 int 型cheapGoods.newSetWeight(198);System.out.println(“对象 cheapGoods 的 weig
28、ht 的值是:“+cheapGoods.weight);System.out.println(“cheapGoods 用子类新增的优惠方法计算价格:“+cheapGoods.newGetPrice();cheapGoods.oldSetWeight(198.987); /子类对象调用继承的方法操作隐藏的 double 型变量 weightSystem.out.println(“cheapGoods 使用继承的方法(无优惠)计算价格:“+cheapGoods.oldGetPrice(); 例 5-4University.javapublic class University void enter
29、Rule(double math,double english,double chinese) double total=math+english+chinese;if(total=200) System.out.println(“考分“+total+“ 达到大学最低录取线“);elseSystem.out.println(“考分“+total+“ 未达到大学最低录取线“);ImportantUniversity.javapublic class ImportantUniversity extends Universityvoid enterRule(double math,double en
30、glish,double chinese) double total=math+english+chinese;if(total=245) System.out.println(“考分“+total+“ 达到重点大学最低录取线“);elseSystem.out.println(“考分“+total+“ 未达到重点大学最低录取线“);Example5_4.javapublic class Example5_4 public static void main(String args) double math=64,english=76.5,chinese=66;ImportantUniversit
31、y univer = new ImportantUniversity();univer.enterRule(math,english,chinese); /调用重写的方法math=89;english=80;chinese=86;univer = new ImportantUniversity();univer.enterRule(math,english,chinese); /调用重写的方法例 5-5Example5_5.javaclass A float computer(float x,float y) return x+y;public int g(int x,int y) retur
32、n x+y;class B extends A float computer(float x,float y) return x*y; public class Example5_5 public static void main(String args) B b=new B();double result=puter(8,9); /b 调用重写的方法System.out.println(“调用重写方法得到的结果:“+result); int m=b.g(12,8); /b 调用继承的方法System.out.println(“调用继承方法得到的结果:“+m); 例 5-6HomeEletri
33、city.javapublic class HomeEletricity int weight;public void setWeight(int w) weight=w; public void showMess()System.out.println(“我是家用电器“);Television.javapublic class Television extends HomeEletricity public void showMess()System.out.println(“我是电视机,重量是“+weight+“kg“);Icebox.javapublic class Icebox ext
34、ends HomeEletricity public void showMess()System.out.println(“我是冰箱,重量是“+weight+“kg“);Example5_6.javaclass Factory public HomeEletricity make() /方法的类型是 HomeEletricity 类HomeEletricity machine=new HomeEletricity();return machine; class TVFactory extends Factory public Television make() /重写方法的类型是 HomeEl
35、etricity 类的子类:TelevisionTelevision tv=new Television();tv.setWeight(21); return tv; class IceboxFactory extends Factory public Icebox make() /重写方法的类型是 HomeEletricity 类的子类:IceboxIcebox icebox=new Icebox();icebox.setWeight(67);return icebox; public class Example5_6 public static void main(String args)
36、 TVFactory factory1=new TVFactory();Television tv=factory1.make(); /factory1 调用重写的方法tv.showMess(); IceboxFactory factory2=new IceboxFactory();Icebox icebox=factory2.make(); /factory2 调用重写的方法icebox.showMess(); 例 5-7Bank.javapublic class Bank int savedMoney;int year;double interest;public double compu
37、terInterest() interest=year*0.035*savedMoney;System.out.printf(“%d 元存在银行 %d 年的利息:%f 元n“,savedMoney,year,interest);return interest;ConstructionBank.javapublic class ConstructionBank extends Bank double year;public double computerInterest() super.year=(int)year;double remainNumber=year-(int)year;int d
38、ay=(int)(remainNumber*1000);interest=puterInterest()+day*0.0001*savedMoney;System.out.printf(“%d 元存在建设银行 %d 年零%d 天的利息:%f 元n“,savedMoney,super.year,day,interest);return interest;BankOfDalian.javapublic class BankOfDalian extends Bank double year;public double computerInterest() super.year=(int)year;d
39、ouble remainNumber=year-(int)year;int day=(int)(remainNumber*1000);interest=puterInterest()+day*0.00012*savedMoney;System.out.printf(“%d 元存在大连银行 %d 年零%d 天的利息:%f 元n“,savedMoney,super.year,day,interest);return interest;Example5_7.javapublic class Example5_7 public static void main(String args) int amo
40、unt=5000;ConstructionBank bank1=new ConstructionBank();bank1.savedMoney=amount;bank1.year=5.216;double interest1=puterInterest();BankOfDalian bank2=new BankOfDalian();bank2.savedMoney=amount;bank2.year=5.216;double interest2=puterInterest();System.out.printf(“两个银行利息相差%f 元n“,interest2-interest1);例 5-
41、8Card.javapublic class Card String title;Card() title = “新年快乐 !“;Card(String title) this.title = title;public String getTitle() return title;ChristmasCard.javapublic class ChristmasCard extends Card String content; /子类新增的 contentChristmasCard(String title,String content) super(title); /调用父类的构造方法,即执行
42、 Card(title)this.content = content;public void showCard() System.out.println(“*“+getTitle()+“*“);System.out.printf(“%s“,content); Example5_8.javapublic class Example5_8 public static void main(String args) String title = “Happy New Year To You“;String content = “t 牛年耕耘 nt 谷穗满仓nt 出生牛犊nt 喜迎虎年n“;Christ
43、masCard card=new ChristmasCard(title,content);card.showCard(); 例 5-9Example5_9.javaclass A final double PI=3.1415926;/ PI 是常量public double getArea(final double r) return PI*r*r;public final void speak() System.out.println(“您好,Hows everything here ?“); public class Example5_9 public static void main(
44、String args) A a=new A();System.out.println(“面积:“+a.getArea(100);a.speak(); 例 5-10People.javapublic class People int height;double weight;void showBodyMess() System.out.printf(“*n“); void mustDoingThing() System.out.println(“吃饭、睡觉. .饮水“); American.javapublic class American extends People void showBo
45、dyMess() System.out.println(“bodyHeight:“+height+“cm“+“ bodyWeight:“+weight+“kg“); void speakEnglish() System.out.println(“I am Amerian“);Chinese.javapublic class Chinese extends People void showBodyMess() System.out.printf(“身高:%5dcmt 体重:%3.2fkgn“,height,weight); void speakChinese() System.out.print
46、ln(“我是中国人“);Example5_10.javapublic class Example5_10 public static void main(String args) People people=null;American Johnson = new American();people = Johnson ; /people 是 Johnson 对象的上转型对象。people.height = 187; /同于 Johnson.height=187people.weight = 78.67;people.showBodyMess(); /等同于 Johnson 调用重写的 show
47、BodyMess()方法/people.speakEnglish(); /非法,因为 speakEnglish()是子类新增的方法people.mustDoingThing(); Chinese zhang = new Chinese();people = zhang ; /people 是 zhang 对象的上转型对象。people.height = 177; /同于 zhang.height=187people.weight = 68.59;people.showBodyMess(); /等同于 Johnson 调用重写的 showBodyMess()方法/people.speakChin
48、ese(); /非法,因为 speakChinese()是子类新增的方法people.mustDoingThing(); zhang = (Chinese)people;zhang.speakChinese();例 5-11Example5_11.javaclass EspecialCar void cautionSound() class PoliceCar extends EspecialCar void cautionSound() System.out.println(“zhuazhuazhua.“); class AmbulanceCar extends EspecialCar void cautionS