Java+2+程序设计实用教程

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Java+2+程序设计实用教程(2)例3-1Fish类的定义classFish{//关键字class用来定义类FishStringname;//描述Fish的名字StringtheColor;//描述Fish的颜色voidsetValue(Stringn,Stringc){//设置值:名字,颜色name=n;theColor=c;}voidswimming(){//Fish最大的特性是能游泳System.out.println(icanswimming...);}voidoutput(){System.out.println(mynameis:+name+ilike:+theColor);}publicstaticvoidmain(Stringargs[]){FishclownFish=newFish();//创建Fish的变量clownFishclownFish.setValue(nemo,red);//调用Fish中的方法(函数)为f赋值clownFish.output();//输出f所描述的鱼的信息clownFish.swimming();//Fish会游泳}例3-2定义一个学生类publicclassStudentClssDemo{Stringname,dept,sex;intage,english,maths,computer;voidsetData(Stringn,Strings,Stringd,inta,inte,intm,intc){//成员方法name=n;age=a;//类的成员变量在整个类内有效sex=s;dept=d;english=e;maths=m;computer=c;}inttotal(){//成员方法intsum=english+maths+computer;//sum是方法体中的局部变量returnsum;//局部变量sum有效范围是它所在语句块}voidprint(){//成员方法System.out.println(姓名:+name+年龄:+age+性别:+sex+系别:+dept);System.out.println(英语:+english+数学:+maths+计算机:+computer);}publicstaticvoidmain(Stringargv[]){StudentClssDemos=newStudentClssDemo();//创建对象s.setData(张楠,男,计算机系,19,80,85,90);//调用成员方法s.print();//成员方法的调用System.out.println(总分:+s.total());//成员方法的调用}}例3-3Book类的定义与对象的实例化classBook{Stringname;floatprice;intwidth,height;Book(Stringn,floatp,intw,inth){//构造方法,用于实例化对象name=n;price=p;width=185;height=260;}voidoutput(){System.out.println(name:+name+\t+price:+price);}publicstaticvoidmain(String[]args){Bookb1;//声明对象b1=newBook(Java2Programming,31.0f,185,260);//实例化对象b1Bookb2=newBook(C++Programming,31.0f,185,260);//声明并实例化对象b2b1.output();//通过对象b1调用方法output()b2.output();//通过对象b2调用方法output()}}例3-4定义一个描述书的类classBookDemo{Stringname;floatprice;voidoutput(){System.out.println(name:+name+\t+price:+price);}publicstaticvoidmain(String[]args){BookDemob1;//声明对象b1=newBookDemo();//实例化对象,数据成员被初始化为空值b1.output();b1.name=java2Programming;//通过对象名为成员变量赋值b1.price=32.5f;//通过对象名为成员变量赋值b1.output();}}例3-5带构造方法的类classBookDemo1{Stringname;floatprice;BookDemo1(Stringn,floatp){//构造方法,与类同名name=n;price=p;}voidoutput(){System.out.println(name:+name+\t+price:+price);}publicstaticvoidmain(String[]args){BookDemo1b1;//声明对象b1=newBookDemo1(Java2,32.5f);//自动调用构造方法实例化对象b1.output();}}例3-6带访问控制权限修饰符的类classBook{Stringname;privatefloatprice;//私有成员Book(Stringn,floatp){name=n;price=p;}publicvoidsetPrice(floatp){//公有成员,类的外部接口price=p;}publicvoidoutput(){//公有成员,类的外部接口System.out.println(name:+name+\t+price:+price);}}publicclassuseBookDemo{publicstaticvoidmain(String[]args){Bookb=newBook(Java2,52.5f);b.output();//可以访问类Book的公有成员b.price=32.0f;//非法访问,其他类不能访问类Book的私有成员b.setPrice(32.0f);//正确的访问b.output();}}例3-7带静态成员变量的类classuseBook{Stringname;floatprice;intcount=0;//记录创建对象的个数staticintbookNumber=100;//静态变量,书的起始编号为100useBook(Stringn,floatp){name=n;price=p;count++;//记录创建对象个数,增加1bookNumber++;//书号实现自动增加1}voidoutput(){prt(name:+name+\t+price:+price+\t);prt(bookNumber:+bookNumber+\t+count:+count+\n);}publicstaticvoidprt(Strings){System.out.print(s);}publicstaticvoidmain(String[]args){useBookb1,b2,b3;//声明对象b1=newuseBook(Java2,36.0f);b1.output();b2=newuseBook(C++,40.0f);b2.output();b3=newuseBook(VB,35.0f);b3.output();}}例3-8静态变量的访问/*classBook{Stringname;privatefloatprice;Book(Stringn,floatp){name=n;price=p;}publicvoidsetPrice(floatp){//类的外部接口price=p;}publicvoidoutput(){//类的外部接口System.out.println(name:+name+\t+price:+price);}}publicclassuseBook{publicstaticvoidmain(String[]args){Bookb=newBook(Java2,32.5f);//自动调用构造方法实例化对象b.output();b.price=100.0f;//非法访问b.output();}}*//*classuseBook{Stringname;floatprice;intcount=0;//记录创建对象的个数staticintbookNumber=100;//静态变量,书的起始编号为100useBook(Stringn,floatp){name=n;price=p;count++;//记录创建对象个数,增加1bookNumber++;//书号实现自动增加1}voidoutput(){prt(name:+name+\t+price:+price+\t);prt(bookNumber:+bookNumber+\t+count:+count+\n);}publicstaticvoidprt(Strings){System.out.print(s);}publicstaticvoidmain(String[]args){useBookb1,b2,b3;//声明对象b1=newuseBook(Java2,36.0f);b1.output();b2=newuseBook(C++,40.0f);b2.output();b3=newuseBook(VB,35.0f);b3.output();}}*/publicclassCircle{staticdoublePI=3.1415926;intradius;Circle(intr){radius=r;}doublegetArea(){doublearea=radius*radius*PI;//创建对象后可以直接访问,也可以通过类名访问returnarea;}publicstaticvoidmain(String[]args){System.out.println(圆周率的值:+Circle.PI);//创建实例前,可访问Circlec=newCircle(10);System.out.println(c.getArea());}}例3-9带静态方法的类classBook{Stringname;privatefloatprice;staticintcount;//静态变量,统计对象个数Book(Stringn,floatp){name=n;price=p;count++;//加1}publicstaticvoiddisCount(){//静态方法System.out.print(Count:+count+\t);//只能访问静态成员}publicvoidoutput(){//实例方法disCount();//实例方法可以访问静态成员System.out.println(name:+name+\t+price:+price);}}publicclassuseBookDemo2{publicstaticvoidmain(String[]args){Book.disCount();//通过类名调用静态方法System.out.print(\n);//换行Bookb1=newBook(Java2,32.5f);b1.output();Bookb2=newBook(C++,32.5f);b2.output();Bookb3=newBook(Delphi,32.5f);b3.output();Book.disCount();//通过类名调用静态方法}}例3-10传值—传递基本数据类型参数publicclassPassValue{staticvoidexchange(inta,intb){//静态方法才被main方法调用inttemp;temp=a;a=b;b=temp;}publicstaticvoidmain(String[]args){inti=10;intj=100;System.out.println(beforecall:+i=+i+\tj=+j);//调用前exchange(i,j);System.out.

1 / 20
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功