當前位置:首頁 » 密碼管理 » 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;
}

熱點內容
什麼手機的游戲配置最好 發布:2025-03-17 00:52:58 瀏覽:261
區域網內如何搭建資料庫伺服器 發布:2025-03-17 00:45:04 瀏覽:31
c語言求正整數的位數 發布:2025-03-17 00:38:06 瀏覽:746
動態窗演算法 發布:2025-03-17 00:25:25 瀏覽:345
怎麼找回k寶密碼 發布:2025-03-17 00:17:23 瀏覽:246
方舟有電腦如何做伺服器 發布:2025-03-17 00:02:08 瀏覽:728
升級fw用ftp伺服器 發布:2025-03-16 23:27:35 瀏覽:345
汽車安全配置哪些好 發布:2025-03-16 23:16:42 瀏覽:176
vcmfc源碼 發布:2025-03-16 23:14:17 瀏覽:505
如何設置禁止訪問伺服器ip 發布:2025-03-16 23:14:07 瀏覽:502