我们可以创建抽象类的对象吗? [英] can we create object of abstract class?

查看:130
本文介绍了我们可以创建抽象类的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对抽象类概念感到困惑。请清除我的怀疑。 Abstract 类的定义表示我们无法创建此类的对象,因此我们称之为 A a = new A(){} 。下面的示例

I am just confused about abstract class concept. Please clear my doubt. Definition of Abstract class says we can not create object of such class, then what we called like A a=new A(){ } . Example is below

  public abstract class AbstractTest {
    public abstract void onClick();
    public void testClick(){

    }
 }

  public class A {
       AbstractTest test = new AbstractTest(){

        @Override
        public void onClick() {

        }
    };
  }

然后 test 是一个对象还是什么?

Then test is a object or what?

推荐答案

test 是匿名对象 AbstractTest 的具体子类(请注意,它实现了 AbstractTest 的所有抽象方法),这就是为什么该子类

test is an object of an anonymous concrete sub-class of AbstractTest (note that it implements all the abstract methods of AbstractTest), which is why this sub-class can be instantiated.

另一方面,

AbstractTest test = new AbstractTest();

不会通过编译,因为那是实例化抽象类的尝试。

wouldn't pass compilation, since that would be an attempt to instantiate an abstract class.

这篇关于我们可以创建抽象类的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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