zoukankan      html  css  js  c++  java
  • 动手动脑4

    (1)继承中基类构造函数的调用:
    源代码:

    public class a {
    public a()
    {
    System.out.println("a的构造方法被调用");
    }
    }
    public class b extends a{
    public b()
    {
    super();
    System.out.println("b的构造方法被调用");
    }
    }
    public class c extends b{
    public c()
    {
    System.out.println("c的构造方法被调用");
    }
    }
    public class m {
    
    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO 自动生成的方法存根
    c child=new c();
    }
    }

    构造函数的作用是用来初始化,当对子类的对象进行初始化时一定会需要对其继承的基类的所有字段等进行初始化,在进行对子类的对象进行初始化之前会调用基类的构造函数,并且按照继承顺序依次调用。


    运行结果:


    (2)源代码

    public class main {
    /**
    * @param args
    */
    public String toString()
    {
    return "ok";
    }
    public static void main(String[] args) {
    // TODO 自动生成的方法存根
    main a=new main();
    System.out.println("a=" + a);
    }
    }


    “+”号运算会调用其toString()方法,默认情况下,此方法返回“类名 @ + hashCode”。子类可以重写toString()方法。

    截图:

    (3)super关键字

    源代码

    public class a {
        public void way()
        {
            System.out.println("父类的way方法被调用");
        }
    
    }
    public class b extends a{
        public void way()
        {
            super.way();
            System.out.println("子类的way方法被调用");
        }
    
    }
    public class c {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO 自动生成的方法存根
            b k=new b();
            k.way();
        }
    }
    

      运行结果

    (4)

     源代码:

    public class mammal {}
    public class dog extends mammal {}
    public class cat extends mammal {}
    public class m {
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO 自动生成的方法存根
            mammal i=null;
            dog j=new dog();
            cat k=new cat();
            i=j;
             j=i;
             j=(dog)i;
             j=k;
             k=(cat)i;
        }
    }
    

      (5)源代码

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

  • 相关阅读:
    Linux系统排查1——内存篇
    (原创)Python字符串系列(1)——str对象
    (原创)Python文件与文件系统系列(2)——os模块对文件、文件系统操作的支持
    (原创)Python文件与文件系统系列(5)——stat模块
    (原创)Python文件与文件系统系列(4)——文件描述字操作
    CenterOS7——使用yum命令报错Could not retrieve mirrorlist
    awk 查询某一列大于1900的值
    /bin/bash^M: bad interpreter: No such file or directory
    linux 设置时间同步
    CentOS7 服务器分析挖矿病毒,清理挖矿病毒 tor2web
  • 原文地址:https://www.cnblogs.com/xuange1/p/9926151.html
Copyright © 2011-2022 走看看