zoukankan      html  css  js  c++  java
  • 继承与多态动手动脑总结

    一:继承条件下的构造方法调用

    程序代码:

    class Grandparent 
    {
    
    
        public Grandparent()
         {
    
                System.out.println("GrandParent Created.");
        
    }
    
    
        public Grandparent(String string) 
        {
    
                System.out.println("GrandParent Created.String:" + string);
        
     }
    
    }
    
    
    
    class Parent extends Grandparent
    {
    
    
        public Parent()
         {
    
                //super("Hello.Grandparent.");
    
                System.out.println("Parent Created");
        
           // super("Hello.Grandparent.");
    
          }
    
    }
    
    
    
    class Child extends Parent 
    {
    
    
        public Child()
         {
        
            System.out.println("Child Created");
    
          }
    
    }
    
    
    
    public class TestInherits 
    {
    
    
        public static void main(String args[])
         {
    
                Child c = new Child();
        
      }
    
    }

    程序截图:

     

    结论:(1)通过 super 调用基类构造方法,必须是子类构造方法中的第一个语句。

    (2)当我们去运行代码的时候,父类的构造函数会先于子类的构造函数。然后,在当我们把子类中的父类的构造方法取消注释,我们会发现同样的结果,然后当我们把父类的构造方法放入子类的构造方法之后,我们会发现,代码无法编译通过,由此得到结论。

     

    二:怎样判断对象是否可以转换?

     

    可以使用instanceof运算符判断一个对象是否可以转换为指定的类型:

     

    Object obj="Hello";

     

    if(obj instanceof String)

     

    System.out.println("obj对象可以被转换为字符串");

     

    程序代码:

    public class TestInstanceof
    {
        public static void main(String[] args) 
        {
            //声明hello时使用Object类,则hello的编译类型是Object,Object是所有类的父类
            //但hello变量的实际类型是String
            Object hello = "Hello";
            //String是Object类的子类,所以返回true。
            System.out.println("字符串是否是Object类的实例:" + (hello instanceof Object));
            //返回true。
            System.out.println("字符串是否是String类的实例:" + (hello instanceof String));
            //返回false。
            System.out.println("字符串是否是Math类的实例:" + (hello instanceof Math));
            //String实现了Comparable接口,所以返回true。
            System.out.println("字符串是否是Comparable接口的实例:" + (hello instanceof Comparable));
            String a = "Hello";
            //String类既不是Math类,也不是Math类的父类,所以下面代码编译无法通过
            //System.out.println("字符串是否是Math类的实例:" + (a instanceof Math));
        }
    }

    程序截图:

    三:“类型转换”

     程序代码:

    package test;
    
    class Mammal{}
    class Dog extends Mammal {}
    class Cat extends Mammal{}
    
    public class TestCast
    {
        public static void main(String args[])
        {
            Mammal m;
            Dog d=new Dog();
            Cat c=new Cat();
            m=d;
            //d=m;
            d=(Dog)m;
            //d=c;
            //c=(Cat)m;
    
        }
    }

    结论:

    子类对象可以直接赋给基类变量。

    基类对象要赋给子类对象变量,必须执行类型转换,

    其语法是: 子类对象变量=(子类名)基类对象名;

    四:子类父类拥有同名的方法时

     

    当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。 这个特性实际上就是面向对象“多态”特性的具体表现。

    程序代码:

    public class ParentChildTest {
        public static void main(String[] args) {
            Parent parent=new Parent();
            parent.printValue();
            Child child=new Child();
            child.printValue();
            
            parent=child;
            parent.printValue();
            
            parent.myValue++;
            parent.printValue();
            
            ((Child)parent).myValue++;
            parent.printValue();
            
        }
    }
    
    class Parent{
        public int myValue=100;
        public void printValue() {
            System.out.println("Parent.printValue(),myValue="+myValue);
        }
    }
    class Child extends Parent{
        public int myValue=200;
        public void printValue() {
            System.out.println("Child.printValue(),myValue="+myValue);
        }
    }

    程序截图:

    结论1:当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。

      结论2:如果子类与父类有相同的字段,则子类中的字段会代替或隐藏父类的字段,子类方法中访问的是子类中的字段(而不是父类中的字段)。如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它。

  • 相关阅读:
    spring ConfigurationProperties 注解
    MySQL安装
    Linux虚机密码破解
    spring cloud zuul 配置(Robbin 和 熔断)
    Oracel官网下载各类版本的JDK
    spring @Configuration
    IDEA debug
    Spring Boot @ControllerAdvice+@ExceptionHandler处理controller异常
    redis-day1
    Mysql进阶-day3
  • 原文地址:https://www.cnblogs.com/lishengming00/p/9903251.html
Copyright © 2011-2022 走看看