错误 1 可访问性不一致: 基类“BaseDemo.BClass”比类“BaseDemo.DClass”的可访问性低

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 
 8 namespace BaseDemo
 9 {
10     class BClass
11     {
12         public BClass()
13         {
14             Console.WriteLine("Welcome to China");
15         }
16 
17         public BClass(string a , string b)
18         {
19             Console.WriteLine( "Name:{0]",a);
20             Console.WriteLine( "Location:{0}",b);
21         }
22     }
23 
24     //派生类
25     public class DClass:BClass
26     { 
27         //这个构造函数会调用默认构造函数
28         public DClass()
29             : base()
30         { 
31         }
32 
33         //这个构造函数会调用有参构造函数
34         public DClass(string x, string y)
35             : base(x, y)
36         { 
37         }
38     }
39 
40     class Program
41     {
42         static void Main(string[] args)
43         {
44             DClass d = new DClass();
45             DClass d1 = new DClass("Suresh Dasari","Hyderabad");
46             Console.WriteLine("\nPress Enter Key to Exit...");
47             Console.ReadLine();
48         }
49     }
50 }
复制代码

 

 出现可访问性低的错误问题,是因为上面代码中第10行“class BClass”默认是private,而派生类public class DClass:BClass使用的是public修饰符,造成了派生类的可访问范围比基类的范围还大,所以出现了报错!

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 
 8 namespace BaseDemo
 9 {
10     public class BClass
11     {
12         public BClass()
13         {
14             Console.WriteLine("Welcome to China");
15         }
16 
17         public BClass(string a , string b)
18         {
19             Console.WriteLine("Name:{0]", a);
20             Console.WriteLine("Location:{0}", b);
21         }
22     }
23 
24     //派生类
25     public class DClass:BClass
26     { 
27         //这个构造函数会调用默认构造函数
28         public DClass()
29             : base()
30         { 
31         }
32 
33         //这个构造函数会调用有参构造函数
34         public DClass(string x, string y)
35             : base(x, y)
36         { 
37         }
38     }
39 
40     class Program
41     {
42         static void Main(string[] args)
43         {
44             DClass d = new DClass();
45             DClass d1 = new DClass("Suresh","Hyderabad");
46             Console.WriteLine("\nPress Enter Key to Exit...");
47             Console.ReadLine();
48         }
49     }
50 }
复制代码

 

 运行之后又出现了上面的错误,结果查找,长时间查找,才发现第19行代码中0的右边是]

这就是错误所在,细微之处的错误找起来非常麻烦;

将]更改为}后运行就正常了,运行正确的代码如下:

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 
 8 namespace BaseDemo
 9 {
10     public class BClass
11     {
12         public BClass()
13         {
14             Console.WriteLine("Welcome to China");
15         }
16 
17         public BClass(string a , string b)
18         {
19             Console.WriteLine("Name:{0}", a);
20             Console.WriteLine("Location:{0}", b);
21         }
22     }
23 
24     //派生类
25     public class DClass:BClass
26     { 
27         //这个构造函数会调用默认构造函数
28         public DClass()
29             : base()
30         { 
31         }
32 
33         //这个构造函数会调用有参构造函数
34         public DClass(string x, string y)
35             : base(x, y)
36         { 
37         }
38     }
39 
40     class Program
41     {
42         static void Main(string[] args)
43         {
44             DClass d = new DClass();
45             DClass d1 = new DClass("Suresh","Hyderabad");
46             Console.WriteLine("\nPress Enter Key to Exit...");
47             Console.ReadLine();
48         }
49     }
50 }
复制代码

 

posted @   chenlight  阅读(147)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!
点击右上角即可分享
微信分享提示