- 2
- 0
- 约1.09万字
- 约 13页
- 2017-06-18 发布于湖北
- 举报
一、访问控制符(1)类的访问权限:①公共访问权限(public)。②包访问权限。(2)成员的访问权限① public② private③ protected④默认包访问★结论:????public包访问protectedprivate相同包中的非子类中被访问YESYESYESNO相同包中的子类中被访问YESYESYESNO不同包中的非子类中被访问YESNONONO不同包中的子类中被访问YESNONONO二、继承(一)Java中继承的实现① extends关键字例题:class Person {? ?? ? String??name;? ?? ? int age;? ?? ? void walk(){? ?? ?? ???System.out.println(会走路);? ?? ? }}class Student extends Person{? ?? ? String no;? ?? ? void takeLessons(){? ?? ?? ? System.out.println(我喜欢所有的课程);? ?? ? }}(二)构造方法在继承中的作用①若子类没有定义构造方法,创建对象时将无条件的调用父类的无参构造方法。②对于父类的有参构造方法,子类可以在自己的构造方法中使用关键字super来调用它,但super语句必须是子类构造方法的第一条语句。③子类在自己的构造方法中如果没有使用super明确调用父类的构造方法,则在创建对象时,将自动先执行父类的无参构造方法,然后再执行自己定义的构造方法。例题:class Parent{? ?? ? String my;? ?? ? public Parent(String x){? ?? ?? ? my=x;? ?? ? }}public class??SubClassextendsParent{? ?? ? public static voidmain(String[] args){? ?? ?? ?? ???? ?? ? }}(三)子类的继承性①子类和父类在同一包中的继承性:例题:②子类和父类不在同一个包中的继承性:例题:★结论:????public包访问protectedprivate可被相同包中的子类继承后直接访问YESYESYESNO可被不同包中的子类继承后直接访问YESNOYESNO(四)变量的继承和隐藏例题:class Parent{? ?? ? int a=3;? ?? ? int m=2;}public class SubClass 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(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.test(double):+x);? ?? ?}}public class B extends A{? ?? ?voidtest(int x){? ?? ?? ?System.out.println(inB.test(int)+x)
原创力文档

文档评论(0)