java类的应用.doc_第1页
java类的应用.doc_第2页
java类的应用.doc_第3页
java类的应用.doc_第4页
java类的应用.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

类中变量和方法一、变量1、类变量的定义public class a int aa;/本类中个非静态方法中变量的默认值,不定义为0。 public static void main(String args) a f=new a(); f.b(); public void b() int bb=33;/ must be initialized in the method System.out.print(aa+bb); /display 0 332、类变量作用域public class a int aa=11; int bb=22; public static void main(String args)a d=new a(); d.a();d.b(); /the object is affected in the same classa f=new a();f.b(); public void a() bb=33,cc=21; System.out.print(aa+bb+cc); public void b() cc=12; System.out.print(aa+bb+cc); /显示65 56 45(1)类各方法在“类的同一个对象中”共用同一个类普通变量public class a int aa=11; /随对象走 static int bb=22;/不随对象走 public static void main(String args)a d=new a(); d.a(); a f=new a();f.b(); public void a() bb=33; System.out.print(aa+bb); public void b() System.out.print(aa+bb); /显示1133 1133(2)类各方法在“类不同的对象中”共用同一个类静态变量二、方法1、没有返回值(1)void 表示方法没有返回值/-void a()System.out.print(“a”); /执行/-void a()System.out.print(“a”); /执行return;/ 程序ending by “return” or“ ”2、有返回值方法中通过return将返回值返回。返回的可以是变量也可以是表达式,return 返回变量值不用括号,返回表达式用括号。(1)返回变量public class A public static void main(String args) A aa=new A(); int b=aa.a(); System.out.print(b); int a()return 3; Int a=5;if(a0)return 3;else return 4; (2)返回表达式public class A public static void main(String args) A aa=new A(); int b=aa.a(); System.out.print(b); int a() int a=3, b=3;int c=a+b;return c; /显示6变量和方法返回值3、带参数的方法1、参数不同的方法可取相同的名字,在使用时由参数来确定调用哪一个方法。public class A void a1(int q, long w) System.out.print(a1); void a1(int q, int w) System.out.print(a2); void a1(long q, int w) System.out.print(a3); public static void main(String arge) A s=new A(); s.a1(2,3); 调用时参数没有默认值public class Avoid a1(int q, int w) public static void main(String arge) A s=new A(); s.a1(2,3); 同名同参数方法不可用public class A int a1(long q, int w) System.out.print(a1); void a1(long q, int w) System.out.print(a2); public static void main(String arge) A s=new A(); s.a1(); /编程将显示错误: a1() 已经在 A 中被定义4、数组参数class b void m1(String a) /也可以 String a for(int i=0;ia.length;i+) System.out.print(ai); public class a public static void main(String args) b gg=new b(); String w=dddd, ggggg; gg.m1(w); /显示ddddggggg/-public class A public static void main(String args) for(int i=0;i=1;x-) f=f*x; return f;/if q=3,display 6 (3*2*1=6)自调int p(int x) if(x=0&newage=0&newage=200) age=newage; else age=0; public class g3 public static void main(String arge) f1 q1=new f1(); int dd=q1.aa(); System.out.print(age=+dd); 类的继承和接口一、类的继承1、类的继承class f public void ff() System.out.println(father); class kid extends f public void kk() System.out.println(kid); 称f为父类kid为f的子类。子类自动拥有父类的方法和变量,kid类有两个方法一个是自己的一个是继承的。public class g2 public static void main(String args) kid k1=new kid(); k1.kk();/显示father k1.ff();/显示kid class f public void ff() System.out.println(father); class kid extends f public void kk() System.out.println(kid); 2、this和super当子类中定义的方法和父类方法同名时,如果想分别使用子类和父的同名方法用this和super,没有关键字默认为this。class b public void bb() System.out.print(father); public class a extends b public static void main(String arge) a c=new a(); c.cc(); public void bb() System.out.print(kid); void cc() this.bb();/调用本类方法 super.bb();/调用父类方法/显示kid father3、abstract类和final类abstract抽象类是指不能直接被实例化生成对象的类,抽象类中的方法只能通过继承类实现。public class A public static void main(String args) kid k1=new kid(); k1.ff1(); / f f1=new f();不可以 / f1.ff1(); abstract class f public void ff1() System.out.println(father); class kid extends fFinal类与abstract类相反,不能成为父类被继承,只能直接使用。 public class g4 public static void main(String args) f f1=new f(); f1.ff1(); final class f public void ff1() System.out.println(father); 四、接口接口的作用和类的继承类似,指定变量和方法名,不直定义方法的内容,方法必须为空。接口中的方法和变量应当为public的。关键字implement用来实现方法,在使用时要给出方法的实际内容。利用接口可实现多重继承(可同时实现多个接口) public class a public static void main(String args) kid k1=new kid(); k1.ff1(); int bb=k1.aa;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论