简体   繁体   English

从抽象类继承

[英]inheritance from abstract class

is it any different to inheriting from a normal superclass that can be implemented? 与从可以实现的普通超类继承有什么不同?

atm I have an abstract class called abstractcar and I have bigcar / smallcar extending this abstract class . atm我有一个叫做abstractcar的抽象类,并且我有bigcar / smallcar扩展了这个abstract class

public abstract class AbstractCar{
    private int capacity;
}

class BigCar extends AbstractCar{}

class SmallCar extends AbstractCar{}

one of the fields in abstractcar is set as "private int capacity" abstractcar的字段之一设置为“私有int容量”

but in the subclass "smallcar" when I type "capacity" as a variable to be used in the constructor, it says "capacity has private access in abstractcar" 但是在子类“ smallcar”中,当我键入“ capacity”作为要在构造函数中使用的变量时,它说“ capacity在abstractcar中具有私有访问权限”

I thought that : 我认为 :

  1. fields are always private and 字段始终是私有的

  2. a subclass inherits all fields and methods of superclass? 子类继承超类的所有字段和方法?

how should I proceed now? 我现在应该如何进行?

Instance methods and fields are inherited by the derived classes from the super class but it does not mean they are accessible also. 实例方法和字段由super类的派生类继承 ,但这并不意味着它们也可访问

Field marked as private in super class and inherited by sub class is part of the object of sub class but it is not accessible for modification in sub class 标示为现场privatesuper类和继承的sub类的对象的一部分sub类,但它是不适合的修改访问sub

[Non recommended solution] [不推荐的解决方案]

Instance fields should always be declared as 'private'. 实例字段应始终声明为“私有”。 Not doing so is a BAD Practice. 不这样做是一种不良做法。 But for understanding point of view I present below 3 points followed by recommend way. 但为了理解观点,我在下面提出3点,然后提出建议的方式。

You need to declare the field as non private so that sub class can access it. 您需要将该字段声明为非私有字段,以便子类可以访问它。

  1. You can declare the field in super class as protected. 您可以将超类中的字段声明为受保护的。 Protected fields are accessible in sub class. 在子类中可以访问受保护的字段。
  2. If the sub class and super class share the same package then no modifier (default package level) can be applied. 如果子类和超类共享同一程序包,则不能应用修饰符(默认程序包级别)。 It will make the field accessible to the sub class. 它将使子类可以访问该字段。
  3. You can declare the field as public. 您可以将该字段声明为公共字段。 In this case the field will be accessible to every class. 在这种情况下,每个班级都可以访问该字段。

[Recommended solution] [推荐解决方案]

Add a way to set the field in super class. 添加一种在超类中设置字段的方法。 If the field is part of the object's life (essential field) then you can create a constructor in Super class, else you can have setter methods and can call them. 如果该字段是对象生命的一部分(必需字段),则可以在Super类中创建构造函数,否则可以使用setter方法并可以调用它们。

public abstract class AbstractCar{
    private int capacity;
    public AbstractCar(int capacity) {
        this.capacity = capacity;
    }
}

class BigCar extends AbstractCar{

    public BigCar() {
        super(6);
    }
}

class SmallCar extends AbstractCar{
    public SmallCar() {
        super(4);
    }
}

Also you can have the setter methods defined and can have protected modifier. 您还可以定义setter方法,并可以使用protected修饰符。 You can call those setter methods to set the fields in super class. 您可以调用这些setter方法来设置超类中的字段。

First of all what are the difference between Abstract class and a normal class? 首先,抽象类和普通类有什么区别? You can not instantiate an Abstract class so it is design especially for a supper class only. 您不能实例化一个Abstract类,因此它是专门为晚餐类设计的。 A normal class can be instantiate any time. 普通类可以随时实例化。

Now inheritance, what is it? 现在继承,是什么? Inheritance means you can inherits all the data members and methods of an already design class into another class and utilize its functionality/behaviours as an single entity. 继承意味着您可以将已经设计的类的所有数据成员和方法继承到另一个类中,并将其功能/行为作为单个实体使用。 When you inherit a supper class irrespective of whether it is a abstract or a regular class all its data members and methods inherits as it is, means all the private members as private and protective members as protective and public as public and same access mechanism will be implemented. 当您继承一个超级类时,无论它是抽象类还是常规类,其所有数据成员和方法均按原样继承,这意味着将所有作为私有成员的私有成员和作为保护对象的公共成员和作为公共对象的保护成员,并且将使用相同的访问机制实现。 In this case, either you have to declare data member as protective or public in supper class to directly access from sub class or otherwise you have to implement setter and getter method inside your supper class to access private members. 在这种情况下,要么必须在超级类中将数据成员声明为保护成员或公共成员,以直接从子类进行访问,要么必须在超级类内部实现setter和getter方法以访问私有成员。

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

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