ImageVerifierCode 换一换
格式:DOCX , 页数:22 ,大小:62.79KB ,
资源ID:10677140      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-10677140.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(继承多态和接口.docx)为本站会员(b****1)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

继承多态和接口.docx

1、继承多态和接口一、访问控制符(1)类的访问权限: 公共访问权限(public)。 包访问权限。(2)成员的访问权限 public private protected 默认包访问 结论:public包访问protectedprivate相同包中的非子类中被访问YESYESYESNO相同包中的子类中被访问YESYESYESNO不同包中的非子类中被访问YESNONONO不同包中的子类中被访问YESNONONO二、继承(一)Java中继承的实现 extends关键字例题:class Person Stringname; int age; void walk() System.out.println(会

2、走路); class Student extends Person String no; void takeLessons() System.out.println(我喜欢所有的课程); (二)构造方法在继承中的作用 若子类没有定义构造方法,创建对象时将无条件的调用父类的无参构造方法。 对于父类的有参构造方法,子类可以在自己的构造方法中使用关键字super来调用它,但super语句必须是子类构造方法的第一条语句。 子类在自己的构造方法中如果没有使用super明确调用父类的构造方法,则在创建对象时,将自动先执行父类的无参构造方法,然后再执行自己定义的构造方法。例题:class Parent St

3、ring my; public Parent(String x) my=x; public classSubClass extendsParent public static voidmain(String args) (三)子类的继承性 子类和父类在同一包中的继承性:例题: 子类和父类不在同一个包中的继承性:例题: 结论:public包访问protectedprivate可被相同包中的子类继承后直接访问YESYESYESNO可被不同包中的子类继承后直接访问YESNOYESNO(四)变量的继承和隐藏例题:class Parent int a=3; int m=2;public class Su

4、bClass extends Parent int a=4; int b=1; public static voidmain(String args) SubClass my=new SubClass(); System.out.println(a=+my.a+,b=+my.b+,m=+my.m); 三、多态性方法的重载例题:public classA int x=0; void test(int x) System.out.println(test(int):+x); void test(long x) System.out.println(test(long):+x); void test

5、(double x) System.out.println(test(double):+x); public static void main(String args) Aa1=new A(); a1.test(5.0); a1.test(5); 方法的覆盖例题:class A int x=0; voidtest(int x) System.out.println(inA.test(int):+x); voidtest(long x) System.out.println(inA.test(long):+x); voidtest(double x) System.out.println(inA

6、.test(double):+x); public class B extends A voidtest(int x) System.out.println(inB.test(int)+x); voidtest(String x,int y) System.out.println(inB.test(String,int):+x+,+y); publicstatic void main(String args) B b1=new B(); b1.test(abcd,10); b1.test(5); b1.test(5.0); 运行时刻多态例题:class Animal voidcry()clas

7、s Cat extends Animal voidcry() System.out.println(喵喵.); class Dog extends Animal voidcry() System.out.println(汪汪.); public class TestCry publicstatic void main(String args) Animal a=new Cat(); a.cry(); a=new Dog(); a.cry(); 四、this和super(一)关键字this关键字this表示“当前对象”。有下面几种用法:(1)调用当前对象的其他方法或访问当前对象的实例变量;例如:

8、class TestThis1 int x=1; void f() void g() int a=this.x+1;/此时this可以省略。 this.f();/此时this可以省略。 public static voidmain(String args) (2)当做方法的返回值返回或作为参数传递;例题:当作方法的返回值:public class Leaf int i = 0;Leaf increment() i+; return this;void print() System.out.println(i= + i);public static void main(Stringargs) L

9、eaf x = new Leaf(); x.increment().increment().increment().increment().increment().print();例题:作为参数传递:class A void f() System.out.println(I am f in classA); A() new TestThis2(this); ;public class TestThis2 TestThis2(A a) a.f(); public static voidmain(String args) new A(); ;(3)区分成员变量和局部变量同名的情况;例题:class

10、 TestThis3 int x; public TestThis3(int x) this.x=x+10; public static voidmain(String args) TestThis3 t=newTestThis3(5); System.out.println(t.x); (4)一个构造方法中调用另外一个构造方法。例题:class TestThis4 int x,y; public TestThis4(intx,int y) this.x=x; this.y=y; public TestThis4(int x) this(x,3); public static voidmain

11、(String args) TestThis4 t=newTestThis4(1); System.out.println(t.x=+t.x); System.out.println(t.y=+t.y); (二)关键字super 通过super访问父类成员例题:public class SubClass extendsParent int a=6; void f() super.f(); a=a+super.a-3; public static void main(String args) SubClass my=newSubClass(); my.f(); System.out.printl

12、n(a=+my.a); 调用父类的构造方法例题:class Parent Parent() System.out.println(i amParent(); public class SubClass extends Parent SubClass() super(); System.out.println(i amSubClass(); publicstatic void main(String args) SubClass my=new SubClass(); 五、final修饰符 final修饰的类:不能有子类。 final修饰的函数:不能被重写。 final修饰的域:不能被修改。 fi

13、nal修饰的数组(引用):不能被修改,但所指向的数组(对象)中的内容可以被修改。空白final:必须在域的定义处或者每个构造器中用表达式对final进行赋值。六、抽象类和抽象方法由abstract修饰的类为抽象类,由abstract修饰的方法为抽象方法。性质:(1)抽象类不能制造对象。(2)抽象类中可以包含一般函数和抽象函数,可以没有抽象函数,但抽象函数必须在抽象类中定义。(接口是完全抽象类)(3)如果抽象类的子类不是抽象类,它必须实现父类的所有抽象函数。(4)如果抽象类的子类也是抽象类,那么它可以实现一部分或者不实现父类的抽象函数。七、接口(1)接口的说明例题:interfaceMyInte

14、rfacepublic static final int I=1;public abstract void f();接口中的变量是常量,默认的修饰符为public staticfinal,可以省略。接口中的方法是抽象方法,默认的修饰符为public abstract,可以省略。一个要实现某个接口,它必须实现接口中的所有抽象方法,并且必须加上public的访问权限。(2)接口回调例题:interfaceShowMessage void 显示商标(String s);class TVimplements ShowMessage public void 显示商标(String s) System.o

15、ut.println(s); class PCimplements ShowMessage public void 显示商标(String s) System.out.println(s); public classExample4_27 public static void main(String args) ShowMessage sm; sm=new TV(); sm.显示商标(长城牌电视机); sm=new PC(); sm.显示商标(联想奔月5008PC机); (3)接口作为参数例题:interfaceSpeakHello void speakHello();class Chines

16、eimplements SpeakHello public void speakHello() System.out.println(中国人习惯问候语:你好,吃饭了吗? ); class Englishimplements SpeakHello public void speakHello() System.out.println(英国人习惯问候语:你好,天气不错 ); class KindHello public void lookHello(SpeakHello hello) hello.speakHello(); public classExample4_29 public static

17、 void main(String args) KindHello kindHello=new KindHello(); kindHello.lookHello(new Chinese(); kindHello.lookHello(new English(); 八、内嵌类(1)成员类例题:public class OuterOne private int x=3; InnerOne ino=newInnerOne(); /成员类开始 public class InnerOne private int y=5; public voidinnerMethod() System.out.printl

18、n(y is +y); public voidinnerMethod2() System.out.println(x2 is +x); ;/成员类结束 public void outerMethod() System.out.println(x is +x); ino.innerMethod(); ino.innerMethod2(); public static voidmain(String args) OuterOne my=newOuterOne(); my.outerMethod(); 从上面的程序可以看出,内嵌类和外层类的其它成员处于同级别位置,所以也称为成员类。在内嵌类中可以访问

19、外围类的成员。成员类中不能定义静态方法。在外围类中访问内嵌类的方法:在外围类的成员定义处创建内嵌类的对象。在外围类的某个成员方法中创建内嵌类的对象,然后通过该对象访问内嵌类的成员。在外围类的静态方法中不能直接创建内嵌类的对象,此时,必须先创建外围类的对象,然后通过外围类的对象创建内嵌类的对象。例如:public static void main(String args) OuterOne.InnerOnei=new OuterOne().new InnerOne(); i.innerMethod(); 在内嵌类中使用关键字this:在内嵌类中,this是指内嵌类的对象,要访问外层类的当前对象必

20、须加上外层类的名作为前缀。例题:public class A private int x=3; /内嵌类 public class B private int x=5; public void M(intx) System.out.println(x=+x); System.out.println(this.x=+this.x); System.out.println(A.this.x=+A.this.x); ; public static voidmain(String args) A a=new A(); A.B b=a.new B(); b.M(6); (2)嵌套类:内嵌类可以定义为静态

21、的,也称为静态内部类。例题:public class OuterTwo private static int x=3; private int y=5; /静态内部类 public static classInnerTwo public staticvoid Method() System.out.println(x is +x); public voidMethod2() System.out.println(x is +x); /System.out.println(yis +y); ;/静态内部类结束 public static voidmain(String args) OuterTw

22、o.InnerTwo.Method(); newOuterTwo.InnerTwo().Method2(); 静态内部类中可以定义实例方法和静态方法。静态方法可以通过类名直接访问,而实例方法必须通过内部类的对象访问。(3)局部内部类方法中的内部类例题:public classOuterThree private int x=3; public void outerMethod(int m) final int n=x+2; /局部内部类 class InnerThree private int y=5; public void innerMethod() System.out.println(y is +y); System.out.println(nis +n); /System.out.println(mis +m); System.out.println(xis +x); ;/局部内部类结束 In

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2