不同的对象和引用 [英] Different object and reference

查看:48
本文介绍了不同的对象和引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 Tutorials Point 学习 Java.所以,可能这个问题会如此基本.但我真的被困在这里.我不知道用什么谷歌来解决它.

I was trying to learn Java from Tutorials Point. So, may be this question would be so basic. But I am really stuck here. And I don't know what to google to get it resolved.

请看看这个程序.

class Animal{

   public void move(){
      System.out.println("Animals can move");
   }
}

class Dog extends Animal{

   public void move(){
      System.out.println("Dogs can walk and run");
   }
}

public class TestDog{

   public static void main(String args[]){
      Animal a = new Animal(); 
      Animal b = new Dog(); 

      a.move();// runs the method in Animal class

      b.move();//Runs the method in Dog class
   }
}

请看一下对象创建.

Animal a = new Animal(); 
Animal b = new Dog(); 

这两者有什么区别?我对第一个很熟悉.有人能简单解释一下当以第二种方式定义对象时会发生什么吗?

What is the difference between these two? I am familiar with the first one. Could someone explain in simple terms what happens when an object is defined in the second way?

推荐答案

  • Animal a = new Animal(); --> 创建一个 Animal 的实例,作为 Animal 引用.只有 Animal 方法可用于从 a 调用.
  • Animal b = new Dog(); --> 由于Dog 扩展了Animal,创建了一个Dog 引用为 Animal.只有Animal 方法(即没有只属于Dog 的方法)可用于从b 调用,但虚方法调用机制将解析方法在覆盖 Animal 的时候,在运行时调用 Dog 的实现.
    • Animal a = new Animal(); --> creates an instance of Animal referenced as an Animal. Only Animal methods will be available to invoke from a.
    • Animal b = new Dog(); --> since Dog extends Animal, creates an instance of Dog referenced as an Animal. Only Animal methods (i.e. no methods pertaining only to Dog) will be available to invoke from b, but the virtual method invocation mechanism will resolve method invocation to Dog's implementations at runtime, when overriding Animal's.
    • 注意

      Object 方法(equalshashCodewait 重载、notifynotifyAlltoStringgetClass) 可用于所有对象.

      Object methods (equals, hashCode, wait overloads, notify, notifyAll, toString and getClass) are available to all objects.

      完整注释示例

      package test;
      
      public class Main {
      
          public static void main(String[] args) {
              /*
               * Abstract class - using anonymous idiom here
               * Can invoke:
               * move
               * forAnimalAndChildren
               * all Object methods
               */
              Animal animal = new Animal(){}; 
              /*
               * Instance type is Dog but reference is Animal
               * Can invoke:
               * move
               * forAnimalAndChildren
               * all Object methods
               * Note that invoking "move" will 
               * resolve to Dog's "move" implementation at runtime, 
               * if any is provided in class Dog
               */
              Animal dogReferencedAsAnimal = new Dog();
              /*
               * Instance and reference types are Dog
               * Can invoke:
               * move
               * forAnimalAndChildren
               * onlyDog
               * all Object methods
               */
              Dog dog = new Dog();
      
          }
          /*
           * Setting this up as an abstract class, as no concrete "animals" can exist - only more specific types. 
           */
          static abstract class Animal {
              void move() {
                  System.out.println("Animal moving...");
              }
              void forAnimalAndChildren() {
                  System.out.println("Anyone extending Animal can invoke me!");
              }
          }
          static class Dog extends Animal {
              @Override
              void move() {
                  System.out.println("Dog moving...");
              }
              void onlyDog() {
                  System.out.println("Only for dogs!");
              }
          }
      }
      

      这篇关于不同的对象和引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆