简体   繁体   English

为什么抽象类可以像这样实例化?

[英]Why can abstract class be instantiated like this?

I read that the major difference between Class and Abstract Class is , abstract class cannot be instantiated, 我读到Class和Abstract Class之间的主要区别是,抽象类无法实例化,

but i can create object for abstract class 但我可以为抽象类创建对象

public abstract class Earth {

    abstract void sand();

    void land() {

    }
}

and using new key word i created the object, for the abstract 并使用新的关键词我创建了对象,用于抽象

    Earth mEarth = new Earth() {

        @Override
        void sand() {

        }
   };

I had some questions on it which have not proper answer on Inet, 我有一些问题在Inet上没有正确答案,

1) is new keyword is used to instance the class ? 1) 是新关键字用于实例类?

2) is instance is nothing but object ? 2) 实例只是对象吗?

3) is mEarth is called object (instance of Earth) ? 3) mEarth被称为对象(地球的实例)?

now i can call any method (as callback or as value return) mEarth.sand(); 现在我可以调用任何方法(作为回调或值返回) mEarth.sand(); mEarth.land(); mEarth.land(); using earth object 使用地球物体

You can't create an abstract class : 您无法创建抽象类

  new Earth(); // <- that's compile time error

However you can create non-abstract derived class as you do 但是,您可以像创建非抽象派生类一样创建

  // New non-abstract (local) class that extends Earth 
  new Earth() {
    // You have to override an abstract method
    @Override
    void sand() {
      ...
    }
  }

The answers for questions: 问题的答案:

  1. Yes, the new keyword creates new instance . 是的, new关键字创建新实例
  2. No; 没有; the created instance is an object that extends Earth class, not just Object 创建的实例是一个扩展 Earth类的对象,而不仅仅是Object
  3. mEarth field declared as Earth and contains an object that extends class Earth , so you call any methods of Earth . mEarth字段声明为Earth并包含一个扩展类Earth的对象,因此您可以调用Earth任何方法。
 Earth mEarth = new Earth() {

    @Override
    void sand() {

    }
};

mEarth --- reference varible holding , sub class object (anonymous sub/inner class of Earth) mEarth ---参考变量控制,子类对象(地球的匿名子/内部类)
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

 new Earth() {

    @Override
    void sand() {

    }
};

Is Class without name 没有名字的班级

1) is new keyword is used to instance the class ? 1)是新关键字用于实例类?
A) Yes A)是的

2) is instance is nothing but object ? 2)实例只是对象吗?
A)Yes A)是的

3) is mEarth is called object (instance of Earth) ? 3)mEarth被称为对象(地球的实例)?
A) mEarth is reference varible holding sub class(anonymous implementation) object A)mEarth是引用变量保持子类(匿名实现)对象

May this help you: 愿这可以帮到你:

No buddy, you are not creating the instance of your Abstract Class here. 没有好友,你不是在这里创建Abstract Class的实例。
Instead you are creating an instance of an Anonymous Subclass of your Abstract Class . 相反,您正在创建Abstract ClassAnonymous Subclass的实例。
And then you are invoking the method on your abstract class reference pointing to subclass object. 然后,您在指向子类对象的抽象类引用上调用该方法。

= new Earth() {}; means that there is an anonymous implementation, not simple instantiation of an object, and object should have been like = new Earth(); 意味着存在匿名实现,而不是对象的简单实例化,而对象应该像= new Earth();
An abstract type is defined largely as one that can't be created. 抽象类型主要定义为无法创建的抽象类型。 We can create subtypes of it, but not of that type itself. 我们可以创建它的子类型,但不能创建它的子类型。

1) Yes it does. 1)是的确如此。 Although it's not the only way around its the most frequent (and safe I think). 虽然它不是最常见的唯一方式(我认为安全)。

2) As far as I know also yes. 2)据我所知也是。 Instance means you have a special aspect of a class meaning with values to its instance members (local variables) etc. These values are specific for every instance that is object. 实例意味着您具有类的特殊方面,其含义为其实例成员(局部变量)等。这些值特定于每个对象实例。

3) In here you are creating an object of an anonymous subclass of Earth (since Earth is abstract and cannot be instantiated). 3)在这里,你创建了一个Earth匿名子类的对象(因为Earth是抽象的,无法实例化)。 This means you don't specifically give a name to your subclass just provide the implementation of sand() in order to be concrete (and be instantiatable). 这意味着你没有专门给你的子类命名只是提供sand()的实现,以便具体(并且可以实例化)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM