I have a product.aspx class
public partial class _Products : Product
{
protected void Page_Load(object sender, EventArgs e)
{
Product p1 = new Product();
p1.m1(); ///here I am facing problem y intelligence system not allowing me access the product class method m1();
}
new virtual int m1()
{
return 10;
}
}
and in my AppCode I have a class product.cs
public class Product
{
public Product()
{
//
// TODO: Add constructor logic here
开发者_C百科 //
}
protected void m1()
{
}
public void m2()
{
}
Tthe problem is, p1.m1(); here I am facing problem my intelligence system not allowing me access the product class method m1();
If you instantiate a new Product you cannot access it's protected member from another class, even if it derives from Product
You don't state he problem, but I expect you mean:
(base-class)
protected abstract int m1();
(sub-class)
protected override int m1() {
return 10;
}
Also; your page is a product; you shouldn't need to create an additional product in the constructor. So Page_Load should probably be invoking virtual mehods on the current instance ("this").
only private methods can onot be called from subclass , protected method can be called from the subclass BUT if you create the instance of the class where protected method defind , you woun't be able to get the protected method
try below example
public class P : Product
{
public P()
{
m1();
}
}
public class Product
{
public Product()
{ // // TODO: Add constructor logic here
}
protected void m1() { }
public void m2() { }
}
m1() method in Product (parent) class returns void but m1() method in _Products (child) class returns int. They should return the same type. Also you probably want to use virtual key word for m1() method in the parent class and use override (or new) keyword for m1() method in the child class.
精彩评论