简体   繁体   English

创建抽象类的对象-错误

[英]Creating an object of an abstract class - Error

I have an abstract class called A . 我有一个称为A的抽象类。

public abstract class A {
 protected String name;
 A(String name){
  this.name=name;
 }
}

The above class is in the package com.goog.product.demo package 上面的类在com.goog.product.demo包中

Now from another package i need to load some test data, by creating an object as shown below; 现在,我需要从另一个包中通过创建一个对象来加载一些测试数据,如下所示;

The following class is in the package com.goog.product.test . com.goog.product.testcom.goog.product.test包中。

public class Test {

 public void loadData(){
  A a =new A("NAME");
 }
}

But, I end up getting an error: 但是,我最终得到一个错误:

Cannot instantiate the type A 无法实例化类型A

The reason is because; 原因是因为; you can't create an object of abstract class. 您不能创建抽象类的对象。 Is there any other way I could load some test values? 还有其他方法可以加载一些测试值吗?

Abstract classes cannot be instantiated. 抽象类无法实例化。 You should read this for a better understanding of it. 您应该阅读此书以更好地理解它。

You can extend your abstract class or have it non-abstract. 您可以扩展抽象类,也可以使其非抽象。

I believe what you're trying to do is Construct a new class. 我相信您要尝试的是构建一个新类。 In that case, All you have to do is this: 在这种情况下,您要做的就是:

public class Aclass {

    private String name;

    public Aclass(String name) {
        this.name = name;
    }
}

try to extends your class..... 尝试扩展您的课程.....

public class B extends A { 公共类B扩展了A {

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

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