OOP面向对象编程四

十六. super关键字

    1. 为什么要使用super关键字?

       1)子类中要访问父类方法或变量。
       2) 子类中调用父类的构造器
    2. 使用注意事项:
 
       a. 只能在构造方法或实例方法内使用super关键字,在静态方法和静态代码块内不能使用super关键字。
       b. 在子类构造方法中如没有使用this关键字,会隐式调用父类的无参构造方法;

          class Father() { 
            public Father() {
               //这里第一行隐含一句代码: super();
                       System.out.println("In Father()");
                }
          }

          class Son extends Father {
          }

          public class Test {
                 public static void main(String[] args) {
                        new Son(); //打印输出 In Father()
                 }
          }

          -----------------------------------------------------------------------

          class Father() { 
            public Father() {
                       System.out.println("In Father()");
                }
          }

          class Son extends Father {
                public Son() {
                       System.out.println("In Son()");    
                }    
          }

          public class Test {
                 public static void main(String[] args) {
                        new Son(); 
                        //打印输出 In Father()
                        //         In Son()
                 }
          }
          
          -----------------------------------------------------------------------

          class Father() { 
            public Father(String name) {
                       System.out.println("In Father() " + name);
                }
          }

          class Son extends Father {
                public Son() {
                       System.out.println("In Son()");    
                }    
          }

          public class Test {
                 public static void main(String[] args) {
                        new Son();  //编译出错, 子类会隐式调用父类中无参构造方法,而此时父类中不存在无参构造方法。
                 }
          }

          -----------------------------------------------------------------------

          class Father() { 
            public Father(String name) {
                       System.out.println("In Father() " + name);
                }
          }

          class Son extends Father {
                public Son() {
                       super("zs");
                       System.out.println("In Son()");    
                }    
          }

          public class Test {
                 public static void main(String[] args) {
                        new Son();  
                        //打印输出:
                        //In Father() zs
                        //In Son()
                 }
          }

          -----------------------------------------------------------------------

          class Father() { 
            public Father() {
                       System.out.println("In Father()");
                }

            public Father(String name) {
                       System.out.println("In Father(String name)");
                }
          }

          class Son extends Father {
                public Son() {
                       this("zs");
                       System.out.println("In Son()");    
                }

                public Son(String name) {
                       System.out.println("In Son(String name)");    
                }    
          }

          public class Test {
                 public static void main(String[] args) {
                        new Son();  
                        //打印输出:
                        //In Father()
                        //In Son(String name)
                        //In Son()
                 }
          }
                
       c. 构造方法中this(...)和super(...)不能同时出现;

十七. 多态
    多态的俩中描述:
    1.一个父类类型的引用可以指向他任何一个子类的对象
    2.[相同]类域的[不同]对象执行[同一]方法的时候会有[不同]的表现

    多态是出现在具有继承关系的两个类的对象之间,所以它不像方法重载(发生在一个类中)在编译期间发生(也就是确定下来),而是在运行期间发生(确定下来)。
    Java中的方法是在代码运行时动态绑定的。
    

十八. 类型转换

    转换:
    1) 先使用instanceof 识别类型
    2) 子类型隐式地扩展到父类型(自动转换)
    3) 父类型必须显式地缩小到子类型

    转换规则:被转换的实际对象类型一定是转换以后对象类型的自身或者子类。
    Person p = new Person();
    Student s = (Student)p;    编译不会错,运行时错误

    Person p2 = new Student();
    Student s = (Student)p2 或者 Person p = (Student)p2;    正确

猜你喜欢

转载自blog.csdn.net/bifuguo/article/details/81365854