当前位置:首页 » 密码管理 » c继承访问权限

c继承访问权限

发布时间: 2023-06-14 18:23:40

⑴ 子类可以访问父类中定义的默认变量吗

int a;
没有任何修饰的属性是包访问权限。。

只要那个子类和他同包就能访问。。 如果不再同一个包中就不能访问。。。。

JAVA中有四种访问权限:private,protected,public,还有就是什么都不写。

private私有访问权限,只有在同一个类里才能访问。

public公共访问权限,所有的类都可以访问,无论在不在一个包中。

什么都不写,默认变量,是包访问权限,也就是说在同一个包类就可以访问。

protected是继承访问权限,在同一个包中的可以访问,对于不再同一个包中的类,如果该类是父类的子类,则可访问。不再同一个包中的不是继承的类就不可以访问。

默认变量和protected变量的共同点是在同一个包中都可以访问,但是如果不在同一个包中,但是有继承关系,protected变量就可以访问。但默认变量只要是不同的包,无论有没有继承关系都不能访问。

希望能帮到你~!

⑵ C++:访问权限protected的问题

#include <iostream>
using namespace std;
class Shape{
protected:
virtual double getArea() =0;
virtual double getCircumference()=0;

virtual void print(){
cout<<"Circumference = "<<getArea()<<"\n";
cout<<"Area = " <<getCircumference()<<"\n\n";
}
};

class Circle:public Shape {
public:
Circle(double radius = 0){
this->radius = radius;
}
double getArea(){
return Pi* radius* radius;
}
double getCircumference(){
return 2*Pi* radius;
}
private:
//const static double Pi =3.14;const static类型的变量,只有int能这么写,其他类型必须在类外初始化
double radius ;

};
const double Circle :: Pi = 3.14;

class Triangle:public Shape{
public:
Triangle(double width ,double height){
this->width = width;
this->height=height;
}
double getArea(){
return width * height;
}
double getCircumference(){
return 2*width + 2*height;
}
private:
double width , height;
};

int main(int argc, char *argv[])
{
cout << "Let's create a Circle .\nEnter a radius: ";
double Radius = 0;
cin >> Radius;
Circle MyCircle(Radius);
MyCircle.print();

cout << "Let's create a Triangle .\nEnter the width and height : ";
double width ,height;
cin >> width >>height;
Triangle MyTriangle(width ,height);
MyTriangle.print();
return 0;
}

在说说protect继承的问题,在主函数定义的Circle对象,cirle是从shape共有继承下来的,所有对于circle来说,print函数是保护类型的,当然他的对象是访问不了自己的保护成员的。

protected允许其子类来访问,这句话的意思是在类定义内,子类可以访问访问父类的protect
例如:
#include <iostream>
using namespace std;
class Shape{
protected:
virtual double getArea() =0;
virtual double getCircumference()=0;
virtual void print(){
cout<<"Circumference = "<<getArea()<<"\n";
cout<<"Area = " <<getCircumference()<<"\n\n";
}
};
class Circle:public Shape {
public:
Circle(double radius = 0) : radius(radius)
{
}
double getArea(){
return Pi* radius* radius;
}
double getCircumference(){
return 2*Pi* radius;
}
void print()
{
Shape::print();
}
private:
const static double Pi ;
double radius ;
};
const double Circle :: Pi = 3.14;
class Triangle:public Shape{
public:
Triangle(double width ,double height){
this->width = width;
this->height=height;
}
double getArea(){
return width * height;
}
double getCircumference(){
return 2*width + 2*height;
}
void print()
{
Shape::print();
}
private:
double width , height;
};
int main(int argc, char *argv[])
{
cout << "Let's create a Circle .\nEnter a radius: ";
double Radius = 0;
cin >> Radius;
Circle MyCircle(Radius);
MyCircle.print();
cout << "Let's create a Triangle .\nEnter the width and height : ";
double width ,height;
cin >> width >>height;
Triangle MyTriangle(width ,height);
MyTriangle.print();
return 0;
}

热点内容
c语言中的整型 发布:2025-03-16 06:40:48 浏览:183
分部数据库服务器的IP地址有效 发布:2025-03-16 06:33:40 浏览:191
安卓项目如何配置tomacat 发布:2025-03-16 06:31:13 浏览:430
写脚本测试 发布:2025-03-16 06:20:07 浏览:780
多个拨号宽带如何配置 发布:2025-03-16 05:51:35 浏览:688
管理员c语言 发布:2025-03-16 05:40:17 浏览:342
安卓软件上的图案如何更改 发布:2025-03-16 05:35:57 浏览:748
2010编译c中文乱码 发布:2025-03-16 05:33:40 浏览:550
干一杯密码箱酒多少钱一箱 发布:2025-03-16 05:31:15 浏览:358
我的零钱通密码是多少 发布:2025-03-16 05:04:36 浏览:938