當前位置:首頁 » 密碼管理 » 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語言中的temp 發布:2025-02-05 02:43:08 瀏覽:123
阿里雲伺服器共享電腦 發布:2025-02-05 02:42:18 瀏覽:417
伺服器有多少台電腦 發布:2025-02-05 02:40:41 瀏覽:447
安卓手機為什麼最新微信安裝不了 發布:2025-02-05 02:31:03 瀏覽:106
安卓手機什麼時候開售 發布:2025-02-05 02:14:15 瀏覽:660
編程車模型 發布:2025-02-05 02:09:55 瀏覽:680
雅馬哈天劍哪個配置好 發布:2025-02-05 02:00:35 瀏覽:170
我的世界國際服推薦118伺服器 發布:2025-02-05 01:50:48 瀏覽:46
普通電腦做伺服器怎麼操作 發布:2025-02-05 01:46:22 瀏覽:628
原神為什麼同伺服器加不起好友 發布:2025-02-05 01:41:03 瀏覽:337