zoukankan      html  css  js  c++  java
  • 多态与异常处理

    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;

             }

    }

    D其中d=m d=c 会出错  d=m是把基类对象赋值给子类需要强制类型转换  d=c:d和c是两个不同的类的对象不能进行赋值运算

    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 printValue1 () {

                       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);

             }

    }

    运行结果 100 200 200 201

    上面代码中父类和子类有相同名字的方法,并且把子类的对象赋值给了子类的对象,这是父类的parent其实是子类的类型,所以value++是加在了父类中而调用的函数是子类的所以输出的是子类的value为200

    public class CatchWho2 {

        public static void main(String[] args) {

            try {

                try {

                    throw new ArrayIndexOutOfBoundsException();

                }

                catch(ArithmeticException e) {

                    System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");

                }

                throw new ArithmeticException();

            }

            catch(ArithmeticException e) {

                System.out.println("发生ArithmeticException");

            }

            catch(ArrayIndexOutOfBoundsException e) {

                System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");

            }

        }

    }

    结果:ArrayIndexOutOfBoundsExceptio外层try-catch

    在第二个try中抛出的警告被外层接受并处理

  • 相关阅读:
    FireFox浏览器的下载和安装、借助RamDisk让你的FireFox飞起来
    XXX is not in the sudoers file. This incident will be reported 的问题解决方案
    UVA How Big Is It?
    CentOS配置smaba与Windows共享文件
    博客说明
    linux 下安装jdk及配置jdk环境图解
    swift 笔记 (十二) —— 下标
    依据先中序序列或后中序序列确定二叉树
    opencv2使用形态学滤波对图像进行边缘及角点检測
    $POST 、$HTTP_RAW_POST_DATA、php://input三者之间的差别
  • 原文地址:https://www.cnblogs.com/chengez/p/4953979.html
Copyright © 2011-2022 走看看