C# 无法访问受保护的成员?
我正在学习C#,我写了下面的代码,但它不起作用 受保护的成员可以通过继承类来访问,但是在下面的代码中,它不起作用。有人能告诉我哪里出错了吗C# 无法访问受保护的成员?,c#,C#,我正在学习C#,我写了下面的代码,但它不起作用 受保护的成员可以通过继承类来访问,但是在下面的代码中,它不起作用。有人能告诉我哪里出错了吗 class ProtectedDemo { protected string name; public void Print() { Console.WriteLine("Name is: {0}", name); } } class Demo : ProtectedDemo { static
class ProtectedDemo
{
protected string name;
public void Print()
{
Console.WriteLine("Name is: {0}", name);
}
}
class Demo : ProtectedDemo
{
static void Main(string[] args)
{
ProtectedDemo p = new ProtectedDemo();
Console.Write("Enter your Name:");
p.name = Console.ReadLine(); //Here i am getting the error.
p.Print();
Console.ReadLine();
}
}
Protected仅可用于派生类或实际类本身 下面是一篇关于访问修饰符的文章: 如果要设置它,可以将其公开,也可以创建一个将字符串作为参数的方法并在那里设置它。像这样的
class ProtectedDemo
{
protected string name;
public void Print()
{
Console.WriteLine("Name is: {0}", name);
}
public void SetName(string newName)
{
name = newName;
}
}
static void Main(string[] args)
{
ProtectedDemo p = new ProtectedDemo();
Console.Write("Enter your Name:");
p.SetName(Console.ReadLine());
p.Print();
Console.ReadLine();
}
或者如果您想在派生类上设置它。像这样做
class Demo : ProtectedDemo
{
static void Main(string[] args)
{
Demo test = new Demo();
Console.Write("Enter your Name:");
test.name = Console.ReadLine(); // create an instance of the deriving class,
// you can only access the name if you're in the current class created
test.Print();
Console.ReadLine();
}
}
Protected仅可用于派生类或实际类本身 下面是一篇关于访问修饰符的文章: 如果要设置它,可以将其公开,也可以创建一个将字符串作为参数的方法并在那里设置它。像这样的
class ProtectedDemo
{
protected string name;
public void Print()
{
Console.WriteLine("Name is: {0}", name);
}
public void SetName(string newName)
{
name = newName;
}
}
static void Main(string[] args)
{
ProtectedDemo p = new ProtectedDemo();
Console.Write("Enter your Name:");
p.SetName(Console.ReadLine());
p.Print();
Console.ReadLine();
}
或者如果您想在派生类上设置它。像这样做
class Demo : ProtectedDemo
{
static void Main(string[] args)
{
Demo test = new Demo();
Console.Write("Enter your Name:");
test.name = Console.ReadLine(); // create an instance of the deriving class,
// you can only access the name if you're in the current class created
test.Print();
Console.ReadLine();
}
}
Protected仅可用于派生类或实际类本身 下面是一篇关于访问修饰符的文章: 如果要设置它,可以将其公开,也可以创建一个将字符串作为参数的方法并在那里设置它。像这样的
class ProtectedDemo
{
protected string name;
public void Print()
{
Console.WriteLine("Name is: {0}", name);
}
public void SetName(string newName)
{
name = newName;
}
}
static void Main(string[] args)
{
ProtectedDemo p = new ProtectedDemo();
Console.Write("Enter your Name:");
p.SetName(Console.ReadLine());
p.Print();
Console.ReadLine();
}
或者如果您想在派生类上设置它。像这样做
class Demo : ProtectedDemo
{
static void Main(string[] args)
{
Demo test = new Demo();
Console.Write("Enter your Name:");
test.name = Console.ReadLine(); // create an instance of the deriving class,
// you can only access the name if you're in the current class created
test.Print();
Console.ReadLine();
}
}
Protected仅可用于派生类或实际类本身 下面是一篇关于访问修饰符的文章: 如果要设置它,可以将其公开,也可以创建一个将字符串作为参数的方法并在那里设置它。像这样的
class ProtectedDemo
{
protected string name;
public void Print()
{
Console.WriteLine("Name is: {0}", name);
}
public void SetName(string newName)
{
name = newName;
}
}
static void Main(string[] args)
{
ProtectedDemo p = new ProtectedDemo();
Console.Write("Enter your Name:");
p.SetName(Console.ReadLine());
p.Print();
Console.ReadLine();
}
或者如果您想在派生类上设置它。像这样做
class Demo : ProtectedDemo
{
static void Main(string[] args)
{
Demo test = new Demo();
Console.Write("Enter your Name:");
test.name = Console.ReadLine(); // create an instance of the deriving class,
// you can only access the name if you're in the current class created
test.Print();
Console.ReadLine();
}
}
从
受保护关键字是成员访问修饰符。受保护的成员
可在其类内和由派生类实例访问
因此,可以从protectedemo
中或从继承类Demo
然后是他们的例子
class A
{
protected int x = 123;
}
class B : A
{
static void Main()
{
A a = new A();
B b = new B();
// Error CS1540, because x can only be accessed by
// classes derived from A.
// a.x = 10;
// OK, because this class derives from A.
b.x = 10;
}
}
所以把你的班级改成
class Demo : ProtectedDemo
{
static void Main(string[] args)
{
//ProtectedDemo p = new ProtectedDemo();
Demo p = new Demo(); //NOTE HERE
Console.Write("Enter your Name:");
p.name = Console.ReadLine(); //Here i am getting the error.
p.Print();
Console.ReadLine();
}
}
从
受保护关键字是成员访问修饰符。受保护的成员
可在其类内和由派生类实例访问
因此,可以从protectedemo
中或从继承类Demo
然后是他们的例子
class A
{
protected int x = 123;
}
class B : A
{
static void Main()
{
A a = new A();
B b = new B();
// Error CS1540, because x can only be accessed by
// classes derived from A.
// a.x = 10;
// OK, because this class derives from A.
b.x = 10;
}
}
所以把你的班级改成
class Demo : ProtectedDemo
{
static void Main(string[] args)
{
//ProtectedDemo p = new ProtectedDemo();
Demo p = new Demo(); //NOTE HERE
Console.Write("Enter your Name:");
p.name = Console.ReadLine(); //Here i am getting the error.
p.Print();
Console.ReadLine();
}
}
从
受保护关键字是成员访问修饰符。受保护的成员
可在其类内和由派生类实例访问
因此,可以从protectedemo
中或从继承类Demo
然后是他们的例子
class A
{
protected int x = 123;
}
class B : A
{
static void Main()
{
A a = new A();
B b = new B();
// Error CS1540, because x can only be accessed by
// classes derived from A.
// a.x = 10;
// OK, because this class derives from A.
b.x = 10;
}
}
所以把你的班级改成
class Demo : ProtectedDemo
{
static void Main(string[] args)
{
//ProtectedDemo p = new ProtectedDemo();
Demo p = new Demo(); //NOTE HERE
Console.Write("Enter your Name:");
p.name = Console.ReadLine(); //Here i am getting the error.
p.Print();
Console.ReadLine();
}
}
从
受保护关键字是成员访问修饰符。受保护的成员
可在其类内和由派生类实例访问
因此,可以从protectedemo
中或从继承类Demo
然后是他们的例子
class A
{
protected int x = 123;
}
class B : A
{
static void Main()
{
A a = new A();
B b = new B();
// Error CS1540, because x can only be accessed by
// classes derived from A.
// a.x = 10;
// OK, because this class derives from A.
b.x = 10;
}
}
所以把你的班级改成
class Demo : ProtectedDemo
{
static void Main(string[] args)
{
//ProtectedDemo p = new ProtectedDemo();
Demo p = new Demo(); //NOTE HERE
Console.Write("Enter your Name:");
p.name = Console.ReadLine(); //Here i am getting the error.
p.Print();
Console.ReadLine();
}
}
您只能访问类内的受保护成员或使用
继承父类的子类(演示)上的对象
类(protectedemo)
您可以像这样访问它
Demo d = new Demo();
d.name = Console.ReadLine();
您只能访问类内的受保护成员或使用
继承父类的子类(演示)上的对象
类(protectedemo)
您可以像这样访问它
Demo d = new Demo();
d.name = Console.ReadLine();
您只能访问类内的受保护成员或使用
继承父类的子类(演示)上的对象
类(protectedemo)
您可以像这样访问它
Demo d = new Demo();
d.name = Console.ReadLine();
您只能访问类内的受保护成员或使用
继承父类的子类(演示)上的对象
类(protectedemo)
您可以像这样访问它
Demo d = new Demo();
d.name = Console.ReadLine();
你犯了什么错误?例如,它是语法错误还是运行时错误?
p
不是从protectedemo
继承的,我得到以下错误:无法通过类型为“AccessSpecifiers.protectedemo”的限定符访问受保护成员“AccessSpecifiers.protectedemo.name”;限定符必须是“AccessSpecifiers.Demo”类型(或从中派生的类型),为什么会发生这种情况?请参阅:[Here][1][1]:您遇到了什么错误?例如,它是语法错误还是运行时错误?p
不是从protectedemo
继承的,我得到以下错误:无法通过类型为“AccessSpecifiers.protectedemo”的限定符访问受保护成员“AccessSpecifiers.protectedemo.name”;限定符必须是“AccessSpecifiers.Demo”类型(或从中派生的类型),为什么会发生这种情况?请参阅:[Here][1][1]:您遇到了什么错误?例如,它是语法错误还是运行时错误?p
不是从protectedemo
继承的,我得到以下错误:无法通过类型为“AccessSpecifiers.protectedemo”的限定符访问受保护成员“AccessSpecifiers.protectedemo.name”;限定符必须是“AccessSpecifiers.Demo”类型(或从中派生的类型),为什么会发生这种情况?请参阅:[Here][1][1]:您遇到了什么错误?例如,它是语法错误还是运行时错误?p
不是从protectedemo
继承的,我得到以下错误:无法通过类型为“AccessSpecifiers.protectedemo”的限定符访问受保护成员“AccessSpecifiers.protectedemo.name”;限定符必须是“AccessSpecifiers.Demo”类型(或从中派生的类型),为什么会发生这种情况?请参阅:[Here][1][1]:hmmm您需要p
?我现在明白了,我必须为“Demo”类(而不是“protectedemo”)创建一个对象,并从中访问基类成员。hmmm您需要p
?我现在明白了,我必须为“Demo”类(而不是“protectedemo”)创建一个对象从这一点我可以访问基类成员。嗯,您需要p
?我现在明白了,我必须为“Demo”类(而不是“protectedemo”)创建一个对象,然后从中我可以访问基类成员