简体   繁体   English

为什么我们需要抽象类中的构造函数和私有成员?

[英]Why do we need constructors and private members in the abstract class?

Why do we need constructors and private members in the abstract class? 为什么我们需要抽象类中的构造函数和私有成员? It is not like we are ever going to create an instance of that class. 这并不像我们要创建该类的实例。

You will create instances, just instances of a derived class. 创建实例,只是派生类的实例。 Those derived classes will still need to call constructors, and can still call members of the abstract class - which may in turn use private members. 那些派生类仍然需要调用构造函数,并且仍然可以调用抽象类的成员 - 这可能反过来使用私有成员。

Here's an example (not a terribly useful one, but just to show the basic idea...) 这是一个例子(不是一个非常有用的例子,但只是为了展示基本的想法......)

public abstract class NamedObject
{
    private final String name = name;

    protected NamedObject(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }
}

public class Computer extends NamedObject
{
    private final int processorSpeed;

    public Computer(String name, int processorSpeed)
    {
        super(name); // See, the constructor is useful
        this.processorSpeed = processorSpeed;
    }

    public String toString()
    {
        return getName() + " (" + processorSpeed + ")";
    }
}

I can't say I write abstract classes that often, generally preferring composition to inheritance, but when I do create them I certainly use constructors and private members. 我不能说我写的抽象类往往 ,一般倾向于组成继承,但是当我创建它们我当然使用构造函数和私有成员。

Abstract classes provide a partial implementation of some interface. 抽象类提供了某些接口的部分实现。 It's perfectly reasonable to consider that you might want to provide part of that implementation and disallow client code (concrete subclasses) from accessing the specifics - ie an extension of the principle of encapsulation. 考虑到您可能希望提供部分实现并禁止客户端代码(具体子类)访问特定内容(即封装原则的扩展),这是完全合理的。

Marking some members as private forces the inheriting class to call protected methods to access that partial implementation; 将某些成员标记为私有强制继承类以调用受保护的方法来访问该部分实现; providing a constructor allows for subclasses to initialise the parent's encapsulated state during their own construction. 提供构造函数允许子类在自己构造期间初始化父级的封装状态。

Unlike an interface, an abstract class that defines data fields is in fact instantiated in the sense that these data fields are allocated. 与接口不同,定义数据字段的抽象类实际上是在分配这些数据字段的意义上实例化的。 It is just that they are never instantiated on their own, they are instantiated as part of something bigger - the subclass. 只是它们永远不会自己实例化,它们被实例化为更大的东西 - 子类。 So when the subclass is built, the supertype is built as well, which is why you would need a constructor. 因此,在构建子类时,也会构建超类型,这就是为什么需要构造函数的原因。

Depending on your hierarchy, your abstract class may have a meaning and state. 根据您的层次结构,您的抽象类可能具有含义和状态。 For example, if your application is a school you may have the notion of a person (that has a name and an SSN), but you would have different subtypes for students and for faculty. 例如,如果您的申请是学校,您可能拥有一个人的概念(具有姓名和SSN),但您可以为学生和教师提供不同的子类型。 Because both types of people share certain state structure (name and SSN) you would have both classes extend the Person class. 因为两种类型的人共享某些状态结构(名称和SSN),所以两个类都会扩展Person类。 But you would never simply instantiate a person directly. 但是你永远不会直接实例化一个人。

In addition to Jon's answer, I'd like to mention that abstract classes still go well with composition, if you keep the subclass tree shallow. 除了Jon的回答之外,我还想提一下,如果你让子类树保持浅薄,那么抽象类仍然可以很好地用于组合。 Ie it is great for providing a common base class for a few closely related objects, but not for creating a gigantic tree of subclasses. 也就是说,为一些密切相关的对象提供公共基类是很好的,但不是为了创建一个巨大的子类树。

Why do you need private class? 你为什么需要私人课程? I think that you are confusing abstract classes with interfaces. 我认为你将抽象类与接口混淆。 Unlike interfaces, abstract classes can hold functionality. 与接口不同,抽象类可以保存功能。 For example: 例如:

public class AbstractBase{
    private int num;

    public AbstractBase(int number){
       this->num = number;
    }

    public int method(){
       return ( this->num * this->templateMethod());
    }

    public abstract int templateMethod();
 }

public class ConcreteDerived extends AbstractBase{ 公共类ConcreteDerived扩展AbstractBase {

public ConcreteDerived(){
  super(4);
}

public int templateMethod(){
   return number; //number is the result of some calculation
}

} }

In this example, you´ll never explicitly instantiate AbstractBase, but by declaring members and constructors, you can customize the functionality of your classes (this is called template method). 在此示例中,您永远不会显式实例化AbstractBase,但通过声明成员和构造函数,您可以自定义类的功能(这称为模板方法)。

Assuming you're doing ad hoc code or prototyping, you do instantiate abstract classes (or maybe even interfaces) from time to time. 假设您正在进行临时代码或原型设计,您会不时地实例化抽象类(甚至可能是接口)。 They're called anonymous inner classes ( one , two ) and look like this: 他们被称为匿名内部类( ),看起来像这样:

// you have this...
public abstract class SomeClass {
    public abstract String returnAString();
}

// ...and this...
public class OtherClass {
    public void operate(SomeClass c) {
        System.out.println(c.returnAString());
    }
}

// ...so you do this:    
OtherClass oc = new OtherClass();
// this is one of the reasons why you need to specify a constructor
oc.operate(new SomeClass() {
    @Override
    public String returnAString() {
        return "I'm an anonymous inner class!";
    }
});

This example is of course quite redundant but should expose the point. 这个例子当然是多余的,但应该揭示这一点。 Some existing frameworks even rely on the heavy usage of this behaviour, namely Apache Wicket at least. 一些现有的框架甚至依赖于这种行为的大量使用,至少是Apache Wicket

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

相关问题 toString()在抽象类的私有成员上定义,那么为什么打印子类对象与抽象类的私有成员一起打印? - toString() defined on private members of abstract class,then why printing subclass object prints along with private members of abstract class? 为什么我们需要抽象方法? - Why do we need abstract methods? 为什么我们不能在子 class 中覆盖超级 class 的私有成员? - Why can we not override private members of a super class in a sub class? 为什么我们需要一个私有构造函数? - Why do we need a private constructor at all? 为什么我们需要多个构造函数来用Java定义二叉树? - Why do we need multiple constructors for defining a binary tree in Java? 为什么在抽象类中需要构造函数? - Why do i need a constructor in an abstract class? 为什么在 class 中的抽象 class 中有私有访问修饰符,即使我们不能创建抽象 class 的实例? - Why is there a private access modifier in an abstract class in Java, even though we cannot create an instance of an abstract class? 装饰模式:为什么我们需要一个抽象装饰器? - Decorator Pattern : Why do we need an abstract decorator? 为什么我们需要重写接口中的抽象方法? - Why do we need to override abstract methods from an interface? Java:为什么需要构造函数来使用父类中的对象? - Java: Why do you need constructors to use objects in a parent class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM