《JAVA中的修饰符使用说明.docx》由会员分享,可在线阅读,更多相关《JAVA中的修饰符使用说明.docx(18页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、学生学号试验课 成绩武汉理工大学学生试验报告书试验课程名称Java修饰符的使用说明开课学院计算机科学与技术学院学生姓名谷鹏学生专业班级软件所以SY001班20222022学年第一学期public String puVar;public function(String puVar) super; this.puVar = puVar;publicvoid printSystem.out.println(aI am a public function+n); 2)package pkgl;publicclassClassA /* paramargs*/publicstaticvoid main(S
2、tring args) / TODO Auto-generated method stub functionfu=new function(tongpack);System.out.println(fu.puVar);fu.print;3)package pkg2;import pkgl.*;publicclassClassB publicClassB / TODO Auto-generated constructor stub/* paramargs*/publicstaticvoid main(String args) / TODO Auto-generated method stub f
3、unction fu2=new function(another pack); System.out.print(fu2.puVar);)4)package pkg2;import pkgl.function;publicclassClassCextends function publicClassC(String puVar) super(puVar);/ TODO Auto-generated constructor stub/* paramargs*/publicstaticvoid main(String args) / TODO Auto-generated method stub
4、ClassCcs=newClassC(rfzileirr); System.out.printIn(cs.puVar); cs.print;)1.2 :packageonlyone;publicfinalclassWithoutcld /* paramargs*/private String ver;public String getVer returnver;publicvoidsetVer(String ver) this.ver = ver;)publicWithoutcld(String ver) super; this.ver =ver;)publicvoidmypowerSyste
5、m.out.println(MI have a private variable +a M+ver);)publicstaticvoid main(String args) / TODO Auto-generated method stubWithoutcldwd=newWithoutcld(ralone);System.out.println(wd.ver);wd.mypower;)1.3 packagestract;publicabstractclassXuClass publicinta,b;publicXuClass(inta,int b) this.a=a;this.b=b;publ
6、icvoidadd System.out.printIn(Sum=+(a+b);publicabstractvoidmul;packageyingyong;importstract.XuClass;publicclass Work extendsXuClass public Work(inta,int b) super(a,b);/ TODO Auto-generated constructor stub Overridepublicvoidmul / TODO Auto-generated method stubSystem.out.println(product=+(a*b);)/* pa
7、ramargs*/publicstaticvoid main(String args) / TODO Auto-generated method stub Work wk=newWork(10,20);wk.add; wk.mul;2.1) packagefangctl;publicclassSuperCls private String prVar: protected String ptVar; public String puVar;String Van;publicSuperCls(String prVar, String ptVar, String puVar,StringVar)
8、super;this.prVar = prVar;this.ptVar = ptVar;this.puVar = puVar; this.Van =Var;publicvoid print System, out. print In (f,I am Father!);DrivatevoidprintK System.out.pri ntIn(I am Father+but one of my private);)protectedvoid print2 Sy stem. out. print In (frI am Father+rbutonr of my protected);)void pr
9、int3System.out.println(f,I am Father+ “but one of my default);) )packagefangying;importfangctl.SuperCls;publicclassAchildClsextendsSuperCls publicAchildCls(String prVar, String ptVar, String puVar,StringVar) super(prVar, ptVar, puVar,Var);/ TODO Auto-generated constructor stub/* paramargs*/publicsta
10、ticvoid main(String args) / TODO Auto-generated method stubAchildCls ac=newAchildCls(Age,phone,Email,name);ac.print;System.out.println(ac.puVar);ac.print2;System.out.println(ac.ptVar);2.2 packagechangli;publicclassConstCls /* paramargs*/privatefinaldoublepi=3.1415926;publicintN=256;publicConstCls(in
11、t n) superjN=n;publicvoid printl System.out.printIn (pi);publicvoid print2 System.out.printin(N);publicstaticvoid main(String args) / TODO Auto-generated method stubConstClscs=newConstCls(27);cs.printl;System.out.println(cs.pi);cs.print2;System.out.printIn(cs.N);)2.3 packagestapack;publicclassStaCls
12、 /* paramargs*/public String Van;staticintcount=100;publicStaCls super;)publicStaCls(String van) super;Van = van;publicvoid print System.out.printIn(count); publicstaticvoid main(String args) / TODO Auto-generated method stub StaCls stl=newStaCls;stl.print;StaCls.count=lQl;System.out.print(stl.;StaC
13、ls st2=newStaCls;St2 .rniint=t? . rnunt+A ;Sy stem. out. print In ( st2.;System,out.printin(StaCls.count);)3. 1packagefangwen;publicclassConCls public StringpuVar;public String prVar;protected String ptVar;String deVar;publicConCls(String puVar, String prVar, String ptVar, String deVar) super;this.p
14、uVar = puVar;this.prVar = prVar;this.ptVar = ptVar;this.deVar = deVar;publicvoid printlSystem.out.println(This is a public function+puVar); )privatevoidprint2 System.out.printlriChp&ivate functiona+prVar); protectedvoidprint3 System.out.printld5hpsotected function+ptVar); void print4System.out.print
15、ln(KThis is a defult function=0);balance=current;return times-1;)1) packageshixian;importabcont.PhoneCard;publicclass yingl extendsPhoneCard staticlongnextCardNumber ;Static-Final String connectNumber=tf200t;staticdoubleadditoryFee;longcardNumber;intpassword;booleanconnected;staticnextCardNumber=202
16、28QQQ01;additoryFee=0.1;public yingl / TODO Auto-generated constructor stub )yingl(doubleib) cardumber=nextCardNumb er+;balance=ib;)OverridepublicvoidperformDial / TODO Auto-generated method stubbalance-=0.5+additoryFee;)/* paramargs*/publicstaticvoid main(String args) / TODO Auto-generated method s
17、tub)2) packageshixian;importabcont.PhoneCard;publicclass ying2 extendsPhoneCard public ying2 / TODO Auto-generated constructor stub ying2(doubleib)balance=ib;)OverridepublicvoidperformDial / TODO Auto-generated method stub balance-=0.9;)3) packageshixian;publicclassyingzong publicyingzong (/ TODO Au
18、to-generated constructor stub/* paramargs*/publicstaticvoid main(String args) / TODO Auto-generated method stub yingl yl=newyingl(50.0);ying2 y2=newying2(50.0);System. out. println(yingl 可以拨打“+yl. TimeLeft+次 ); System. out. printin (“ying2 可以拨打“+y2. TimeLef t+”次”);)3.3package test;publicclassTestSta
19、tic publicTestStatic / TODO Auto-generated constructor stub/* paramargs*/publicstaticvoid main(String args) JAVA修饰符:1类的修饰符:类的修饰符可分为:可访问掌握符和非访问掌握符两种。可访问掌握符是:公共类修饰符public非访问掌握符有:抽象类修饰符abstract ;最终类修饰符 final1)公共类修饰符public : Java语言中类的可访问 掌握符只有一个:public即公共的。每个Java程序的主类都 必需是public类作为公共工具供其它类和程序使用的应定义 为 pu
20、blic 类。2 )抽象类修饰符abstract :但凡用abstract修饰符 修饰的类,被称为抽象类。所谓抽象类是指这种类没有具 体对象的一种概念类。这样的类就是Java语言的abstract 类。3 )最终类修饰符final :当一个类不行能有子类时可 用修饰符final把它说明为最终类。被定义为final的类通 常是一些有固定作用、用来完成某种标准功能的类。4 )类缺省访问掌握符:假设一个类没有访问掌握符, 说明它具有缺省的访问掌握符特性。此时,这个类只能被 同一个包中的类访问或引用。这一访问特性又称为包访问 性。/ TODO Auto-generated method stubInt
21、eger i=newlnteger(25);System.out.println(i.intValue*2);System.out.printIn(i.floatValue*2);System. out. print In (,ri=,f+i. intValue*2);System. out printin (Integer .parselnt(r,100a) *2);System. out. print In (rf100*2=fr+Integen. toString(100*2);System.out.println(Integer.vatueO/(rf100)intValue*2);Sy
22、stem. out. print In (Integer;Sy stem. out. print In (Integer;)3.4packageficont;publicclassfiCls public String va;publicfiCls(String va) super;this.va = va;/ TODO Auto-generated constructor stubpublicvoid printlSystem.out.println(frI am a public function);)DrivatevoidDrint2(System.out.printin(工 am a
23、private function);)finalvoid print3Sy stem. out. print In (rfI am a defult function);)publicstaticvoid main(Stringargs)fiClsfs=newfiCls(astrlrf);fs.print3;1) packagefying;importficont.fiCls;publicclassFchildextendsfiCls publicFchild(String va) super(va);/ TODO Auto-generated constructor stub/* param
24、args*/publicstaticvoid main(String args) / TODO Auto-generated method stub Fchildfd=newFchild(String);fd.printl;4. 14. 1package pkgl;publicinterfaceFourBA publicvoid add(inta,int b);publicvoidmul(inta,int b);)1) package pkgl;publicclassAppclsimplementsFourBApublicAppcls / TODO Auto-generated constru
25、ctor stubpublicvoidadd(intaintb) System, out. print In (rrsum=+(a+b);publicvoidmul(inta,int b) System.out.pnintln( pkg2,在pkgl里面建一个 public类型的function类,作为功能类,里面含有一个公 共类的成员变量和成员函数,再在同一个包中,建一个 ClassA类,访问function中的方法和变量。在pkg2中建一 个ClassB类,引入pkgl,直接访问里面的成员变量。再建 一个ClassC类,作为function的子类,调用从父类中继承 的变量和方法。运行结果如
26、以下图:tongpackI am a public functionanother packzileiI am a public function程序见附录2 .建立一个用final修饰的Withoutcld类,有一个私有成员变量,一个访问其变量的成员函数。在主函数中通过对 象直接访问,或访问其方法。运行结果如以下图:aloneI have a private variable alone程序见附录1.2.3 .建立两个包fangctl和fangying,在fanngctl中建立父 类 SuperCis里面含有public,protected,private、def ult 四类成员变量,同时
27、在建立相应类型的方法访问对应的变量。在 fangying包中建立子类的对象,分别调用其中的public和 protected变量和方法。运行结果如下:I am Father!EmailI am Fatherbut onr of my protectedphone程序见附录2.1o4 .构建一个StaCls的类,甚至一个静态成员变量count 并初始化,再利用成员函数访问。在主函数中构造类的不同 对象,分别用对象转变类中静态成员变量的值,输出,验证 其结果。程序结果如下:190101102102程序见附录2. 3.5 .建立两个包fangwen和addition,在fangwen中建立 父类Co
28、nCls 的类,分别设置 public、private、protected、 defult,四种类型的方法。在主函数中调用私有的方法。在 addition包中,先建立一个testpupr类,引入ConCls类, 建立起对象,调用其中的public方法。在建立一个ConCls 的子类,调用里面的public和protected方法。运行结果如下:This is a private functionemailThis is a public functionnameThis is a public functionnameThis is a protected functionemail程序见附录
29、3.1.6 .在包ficont中建立一个fiCls的父类,里面存在三个 成员方法,分别为public、private finalo在包fying中 建立父类的子类Fchild,创立对象调用继承的public的方 法。运行结果如下:I am a defult functionI am a public function程序见附录3. 4.7 .建立一个Integer类的对象i,对象i的整数初值为25.分别调用i中的三个方法,实现了取对象中的整数值、将对象 中的整数只取出并转化成浮点数及字符串。然后用静态方法 测试。将方法参数的字符串“100”转化为整数100;将作为方 法参数的整数100 2转换为字符串“200” ;然后,首先调用 类方法valueOf返回一个Integer对象(内含整数100),然后 再调用对象方法intValue返回该对象所封装的整数值,并乘 以2已验证它的数据类型。最终输出Integer类的两个静态域 的值。运行结果如下:5050.0 i=5。200100*2200200 2147483647-2147483648程序见附录3. 3.附录:1.1:1)package pkgl;publicclass function