[英]Calling the base class constructor from the derived class constructor
我有个问题:
假设我最初有这些我无法更改的类(假设它们是从我正在使用的库中获取的):
class Animal_
{
public:
Animal_();
int getIdA()
{
return idA;
};
string getNameA()
{
return nameA;
}
private:
string nameA;
int idA;
}
class Farm
{
public :
Farm()
{
sizeF=0;
}
Animal_* getAnimal_(int i)
{
return animals_[i];
}
void addAnimal_(Animal_* newAnimal)
{
animals_[sizeF]=newAnimal;
sizeF++;
}
private:
int sizeF;
Animal_* animals_[max];
}
但是后来我需要一个只添加几个字段的类,所以我这样做了:
class PetStore : public Farm
{
public :
PetStore()
{
idF=0;
};
private:
int idF;
string nameF;
}
但是,我无法初始化我的派生类。 我的意思是我做了这个继承,所以我可以将animals
添加到我的PetStore
但现在因为sizeF
是私有的,我该怎么做? 我在想也许在PetStore
默认构造函数中我可以调用Farm()
......所以有什么想法吗?
PetStore
的构造函数将调用Farm
的构造函数; 你没有办法阻止它。 如果您什么都不做(就像您所做的那样),它将调用默认构造函数( Farm()
); 如果需要传递参数,则必须在初始化列表中指定基类:
PetStore::PetStore()
: Farm( neededArgument )
, idF( 0 )
{
}
(类似地, PetStore
的构造函数将调用nameF
的构造函数。类的构造函数总是调用其所有基类及其所有成员的构造函数。)
首先, PetStore
不是农场。
让我们过去吧。 您实际上不需要访问私有成员,您可以在公共接口中获得所需的一切:
Animal_* getAnimal_(int i);
void addAnimal_(Animal_* newAnimal);
这些是您有权访问的方法,这些是您应该使用的方法。
我的意思是我做了这个继承,所以我可以将动物添加到我的 PetStore 但现在因为 sizeF 是私有的,我该怎么做?
很简单,你调用addAnimal
。 它是public
,它也会增加sizeF
。
另外,请注意
PetStore()
{
idF=0;
};
相当于
PetStore() : Farm()
{
idF=0;
};
即调用基构造函数,初始化基成员。
派生类构造函数已经自动调用了基类构造函数。 在 C++ 中,如果基类有一个默认构造函数(不带参数,可以由编译器自动生成!),并且派生类构造函数没有在其初始化列表中调用另一个基类构造函数,则默认构造函数将叫做。 即你的代码相当于:
class PetStore: public Farm
{
public :
PetStore()
: Farm() // <---- Call base-class constructor in initialision list
{
idF=0;
};
private:
int idF;
string nameF;
}
但我无法初始化我的派生类,我的意思是我做了这个继承,所以我可以将动物添加到我的 PetStore 但现在因为 sizeF 是私有的,我该怎么做? 所以我在想也许在 PetStore 默认构造函数中我可以调用 Farm() ......所以有什么想法吗???
不要惊慌。
农场构造函数将在的PetStore的构造函数被调用,自动。
参见基类继承调用规则: 调用超类构造函数的规则是什么?
Base Class Constructor takes three arguments. 注意:如果我们不使用super()关键字,它将调用基类的默认构造函数,这种情况下是非法的,因为 基类构造函数带有三个参数。
因此,有必要使用带有所需参数的super()关键字。
记得:
called with new operator. 1)。从类外部, 使用new运算符调用构造函数。
2)。 从类内部,可以通过this()关键字或super()关键字调用它。
3)。 . this()关键字可用于调用同一类的另一个构造方法 。
4)。与其他方法(即functions())不同,构造函数不会继承 。
5)。如果您命名基类的方法名称与派生类方法名称相同,则将不会调用基类 。
of Base Class runs first(See program below). 6)。无论何时创建派生类的对象, 基类的 都会首先运行(请参见下面的程序)。
class demo
{
public static void main(String args[])
{
derived1 d1=new derived1("Tom","Dad",21,"Programming","Cooking");
derived2 d2=new derived2("Tom","Dad",21,500);
d1.baseDisplay();//Calling Base class's baseDisplay() via derived class object
d1.display();
d2.baseDisplay();//Calling Base class's baseDisplay() via derived class object
d2.display();
}
}
class base
{
private
String name;
String fatherName;
int age;
String interest;
String hobby;
int piggyBankAmount;
base(String name,String fatherName,int age)
{
this.name=name;
this.fatherName=fatherName;
this.age=age;
}
public void baseDisplay()
{
System.out.println("Name:"+name+"\nFather's Name:"+fatherName+"\nAge:"+age);
}
}
class derived1 extends base
{ /* String interest; Note we inherited these data members from Base Class
String hobby; */
derived1(String name,String fatherName,int age,String interest,String hobby)
{
super(name,fatherName,age);
this.interest=interest;
this.hobby=hobby;
}
public void display()
{
System.out.println("Hobby:"+hobby+"\nInterest:"+interest);
}
}
class derived2 extends base
{ //int piggyBankAmount; Note we inherited this data member from Base Class
derived2(String name,String fatherName,int age,int piggyBankAmount)
{
super(name,fatherName,age);
this.piggyBankAmount=piggyBankAmount;
}
public void display()
{
System.out.println("piggyBankAmount:"+piggyBankAmount);
}
}
输出:
Name:Tom
Father's Name:Dad
Age:21
Hobby:Cooking
Interest:Programming
Name:Tom
Father's Name:Dad
Age:21
piggyBankAmount:500
class demo
{
public static void main(String args[])
{
derived1 d1=new derived1("Tom","Dad",21,"Programming","Cooking");
derived2 d2=new derived2("Tom","Dad",21,500);
d1.display();
d2.display();
}
}
class base
{
private
String name;
String fatherName;
int age;
String interest;
String hobby;
int piggyBankAmount;
base(String name,String fatherName,int age)
{
this.name=name;
this.fatherName=fatherName;
this.age=age;
System.out.println("Name:"+name+"\nFather's Name:"+fatherName+"\nAge:"+age);//See Constructor of Base class runs first
}
}
class derived1 extends base
{ /* String interest; Note we inherited these data members from Base Class
String hobby; */
derived1(String name,String fatherName,int age,String interest,String hobby)
{
super(name,fatherName,age);
this.interest=interest;
this.hobby=hobby;
}
public void display()
{
System.out.println("Hobby:"+hobby+"\nInterest:"+interest);
}
}
class derived2 extends base
{ //int piggyBankAmount; Note we inherited this data member from Base Class
derived2(String name,String fatherName,int age,int piggyBankAmount)
{
super(name,fatherName,age);
this.piggyBankAmount=piggyBankAmount;
}
public void display()
{
System.out.println("piggyBankAmount:"+piggyBankAmount);
}
}
输出:
Name:Tom
Father's Name:Dad
Age:21
Name:Tom
Father's Name:Dad
Age:21
Hobby:Cooking
Interest:Programming
piggyBankAmount:500
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.