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

  • 相关阅读:
    ubuntu下安装maven
    159.Longest Substring with At Most Two Distinct Characters
    156.Binary Tree Upside Down
    155.Min Stack
    154.Find Minimum in Rotated Sorted Array II
    153.Find Minimum in Rotated Sorted Array
    152.Maximum Product Subarray
    151.Reverse Words in a String
    150.Evaluate Reverse Polish Notation
    149.Max Points on a Line
  • 原文地址:https://www.cnblogs.com/xuange1/p/9926151.html
Copyright © 2011-2022 走看看