在C#中,如何从子类访问基类变量值
基类在C#中,如何从子类访问基类变量值,c#,inheritance,C#,Inheritance,基类 class TestBase { protected int a; protected int b; public TestBase(int i) { a = i; } protected TestBase() { } public void Update(int i) { a = i; TestChild child = new TestChild()
class TestBase
{
protected int a;
protected int b;
public TestBase(int i)
{
a = i;
}
protected TestBase()
{
}
public void Update(int i)
{
a = i;
TestChild child = new TestChild();
child.Update("Hello World ");
}
class TestChild:TestBase
{
private string msg;
public void Update (string s)
{
msg = s+ a.ToString();
Console.WriteLine("msg=" + msg);
}
}
儿童班
class TestBase
{
protected int a;
protected int b;
public TestBase(int i)
{
a = i;
}
protected TestBase()
{
}
public void Update(int i)
{
a = i;
TestChild child = new TestChild();
child.Update("Hello World ");
}
class TestChild:TestBase
{
private string msg;
public void Update (string s)
{
msg = s+ a.ToString();
Console.WriteLine("msg=" + msg);
}
}
呼叫
private void btnTest_Click(object sender, EventArgs e)
{
TestBase t = new TestBase(1);
t.Update(100);
}
结果
你好,世界0
问题
我希望得到Hello World 100。显然,子类没有访问基类变量int a。我该怎么做呢?测试库中的子对象不知道它放在哪里。它不知道它在一个类中
当您创建子对象时,您还创建了一个完全不同的TestBase对象,它与包含子对象的对象不同。创建该对象时,将初始化为默认值,在本例中为0
如果你想把a的值传给另一个对象,你应该把它给它。例如,通过构造函数
这样称呼:
TestChild child = new TestChild(a);
public TestChild(int x) : base(x){}
static protected int a;
static protected int b;
创建一个如下所示的构造函数:
TestChild child = new TestChild(a);
public TestChild(int x) : base(x){}
static protected int a;
static protected int b;
不幸的是,你的概念到处都在流血,所以它在这种形式下不起作用。我建议你多学习。首先,不清楚你想完成什么 这段代码有很多问题。首先,您不应该在TestBase类方法内创建TestChild的实例。如果您在btnTest_Click中创建一个TestBase实例,那么您将无法访问任何TestChild的方法或数据(除非您在TestBase中创建一个TestChild实例,这是一个糟糕的做法)
继承的目的通常是扩展基类的数据/方法,而不是在基类内使用继承的类。另外,通过一组继承类,您可以实现多态性,这是面向对象编程的另一个核心原则。您应该更好地理解这些原则,然后代码将变得更有意义。您在
TestBtn\u单击中创建的对象与您在TestBase.Update
中创建的对象无关。它们是两个不同的对象,因此每个对象都有自己的具有不同值的字段
要生成预期的输出,需要在更新中将子.a
设置为此.a
:
public void Update(int i)
{
a = i;
TestChild child = new TestChild();
child.a = this.a;
child.Update("Hello World ");
}
这里需要理解的重要一点是继承不会影响对象。继承只影响类。基类成员也将出现在子类中。就这样即使objA
的类型继承自objB
,但除非它们是相同的对象,否则它们是不相关的。您应该这样调用基类
class BaseClass
{
int a;
int b;
protected BaseClass()
{ }
protected BaseClass(int i)
{
a = i;
}
protected void Update(int i)
{
a = i;
Console.Write("Hello World ");
}
}
class TestChild : BaseClass
{
public TestChild(int i) : base(i) //send your constuctor to your base class
{ }
public TestChild()
{ }
public void Update(int i)
{
base.Update(i);
Console.Write(i.ToString());
}
}
private void btnTest_Click(object sender, EventArgs e)
{
TestChild t = new TestChild(); // create instance as your child
t.Update(100);
}
创建对象作为子类,然后调用方法并让它调用基类
如果你想从你的孩子那里访问基类中的a或b
public int a;
public
it,以便您可以从创建的实例访问它
protected
将只允许子类内部访问它
public void Update(int i)
{
a = i;
TestChild child = new TestChild();
child.Update("Hello World ");
}
这是您为变量a赋值的地方,之后您实例化了TestChild类的另一个不同实例。
在这个实例化之后,您必须分配如下值
TestChild child = new TestChild();
child.a = i;
child.Update("Hello World ");
然后你会得到你想要的结果这就是如何使用继承:
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog("Dalmata", "Fuffy", 7);
}
}
class Animal
{
public string Name { get; set; }
public int Age { get; set; }
public Animal(string name, int age)
{
Name = name;
Age = age;
}
}
class Dog : Animal
{
public string Race { get; set; }
public Dog(string race, string name, int age) : base(name, age)
{
Race = race;
}
}
通过这种方式,您也将访问名称和年龄,即使它们是在动物中声明的。如果您希望获得预期结果,请在a
和/或b
变量前面放置一个static
修饰符,如下所示:
TestChild child = new TestChild(a);
public TestChild(int x) : base(x){}
static protected int a;
static protected int b;
在此之后,如果按下该按钮,则会显示:
msg=Hello World 100
为什么要在TestBase.Update
中创建TestChild
实例?这是没有道理的。你应该在你的主类中创建TestChild
int,而不是在你的基类中为什么要进行下一票?他清楚地回答了自己的问题:到目前为止他做了什么,错误的结果是什么,他期待什么。如果你不喜欢他编写代码的方式,那么你的答案有一个很好的主题。停止偷别人的分数。