通过继承无法在主类中使用方法 [英] Unable to a method in main class through inheritance
问题描述
您好我有继承的DisplayData方法,并且我成功地能够在继承的类中调用此方法。但不幸的是,我无法在主类中调用此方法,我已经创建了继承类的对象,并尝试访问未显示的DisplayData方法。我知道当你继承另一个类中的任何类时,你将能够访问所有方法和成员字段,但我无法做到..请澄清我。我的要求是我可以使用系统访问主类
Hi I have method DisplayData which is inherited and i am succefully able to call this method in inherited class. But unfortunately I am unable to call this method in main class i have created the object of for inherited class and tried accessing the DisplayData method its not shown. I am aware that when you inherit any class in another class you will be able to access all the methods and member fields but i am unable to do.. Please clarify me. My requirement is can i access the DisplayData() method in main class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class student
{
protected int _stdID;
protected string _stringName;
Protected void DisplayData()
{
Console.WriteLine("Name of the student {0} and id {1}", _stringName, _stdID);
}
}
class Csstudent : student
{
private int _sub1, _sub2;
public Csstudent(int id, string name, int sub1, int sub2)
{
_stdID = id;
_stringName = name;
_sub1 = sub1;
_sub2 = sub2;
}
public void StudentDetails()
{
//DisplayData();
Console.WriteLine("Subject marks1 {0} and subject marks2 {1}",_sub1,_sub2);
}
}
class Program
{
static void Main(string[] args)
{
Csstudent cs = new Csstudent(101, "Sha", 56, 96);
cs.StudentDetails();
}
}
推荐答案
我的要求是可以访问主类中的DisplayData()方法
My requirement is can i access the DisplayData() method in main class
你实际可以,以下代码应该(并且在我的系统上)编译并运行正常。
You actually can, the following code should (and it does, on my system) compile and run fine.
static void Main(string[] args)
{
Csstudent cs = new Csstudent(101, "Sha", 56, 96);
cs.StudentDetails();
cs.DisplayData();
}
这篇关于通过继承无法在主类中使用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!