• 动手动脑10.21


    1.

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

    运行截图:

     

    2.

     运行截图:

     实际调用的是Object类的public void println(Objext x)

    这一方法调用了String类的valueOf方法

    valueOf方法内部又调用Object.toString方法

    public String toString(){

    return get Class().getName()+"@"+Integer.toHexString(bashCode);

    }

     hashCode方法是本地方法,由JVM设计者实现

    public native int hashCode();

    3.

    运行Fruit.java

    public class Fruit
    {
            
        public String toString()
        {
            return "Fruit toString.";
        }
    
        public static void main(String args[])
        {
            Fruit f=new Fruit();
            System.out.println("f="+f);
        //    System.out.println("f="+f.toString());
        }
    }

    运行截图

     

     4.TestInstanceof.java

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

    运行截图

     5.运行

    ParentChildTest.java
    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);
        }
    }

    运行截图

    当子类与父类拥有一样的方法时,并且让一个父类变量引用一个子类对象时,看自己的类型,对象时子类型则调用子类型方法,若对象是父类型则调用父类型方法。

    如果子类与父类有相同的字段,则子类中的字段回代替或隐藏父类的字段,子类方法中访问的是子类中的字段,如果子类方法想访问父类中隐藏的同名字段,可以用super来访问

    如果子类被当作弗雷使用,则通过子类访问的字段是父类的

  • 相关阅读:
    火狐浏览器推荐插件原创
    vue转义字符转换成普通字符
    vue中关于插槽的使用
    vue调取摄像头的方法(可以直接使用)
    vue调用andriod方法
    vue中格式化时间戳(可直接使用)
    vue关于$router的使用
    轻松了解Spring中的控制反转和依赖注入(二)
    更加优雅地配置Spring Securiy(使用Java配置和注解)
    更加优雅地搭建SSH框架(使用java配置)
  • 原文地址:https://www.cnblogs.com/feng747/p/13855199.html
Copyright © 2020-2023  润新知