Ok so I've been searching about for a while trying to find an anwer to this question but it is difficult to phrase so I am asking here.
I'm inheriting a class like
Class A (int a, int b, int c)
public A(int a, int b, int c)
{
}
Class B : A
public B(int a, int b, int c) base: (a, b, c)
public void blah(int something)
{
}
Why can't I do this and then do :
B classb = new B(1,2,3);
classb.blah(4);
Instead I have to do
public virtual void blah(int something)
{
}
In class A, then in class B:
public override void blah(int something)
{
//method to be used in B but not A.
}
So even though I have no intention of ever using the method in class A I still have to declare the virtual? So if i'm inheriting class C : B then what? I have to declare stuff in A for C?
Your assumption has no any meaning, at least as much as I understood.
Consider following example:
public class Base {}
public class Derived : Base {
public void DerivedSpecificMethod() {
}
}
if you do
Derived d = new Derived(); //as you specify in code example
d.DerivedSpecificMethod(); //you CAN do this.
The virtual
may be need in case when you write:
Base b = new Derived(); //note, I use a Base here on left side of equation
b.DerivedSpecificVirtualMethod(); //virtual method call
Why can't I do this and then do :
B classb = new B(1,2,3);
classb.blah(4);
You absolutely can. There's no problem with that at all, other than the syntactic errors in your code. Here's a complete example which compiles and runs with no issues. (I've fixed the naming convention violation at the same time for the blah
method.)
using System;
class A
{
public A(int a, int b, int c)
{
}
}
class B : A
{
public B(int a, int b, int c) : base(a, b, c)
{
}
public void Blah(int something)
{
Console.WriteLine("B.Blah");
}
}
class Test
{
static void Main()
{
B b = new B(1, 2, 3);
b.Blah(10);
}
}
There's nothing wrong with this, except that your code is not actually valid C#. I think this is what you want:
class A
{
public A(int a, int b, int c)
{
}
}
class B : A
{
public B(int a, int b, int c) : base(a, b, c)
{
}
public void blah(int something)
{
}
}
Then this is no problem:
B classb = new B(1,2,3);
classb.blah(4);
Your code won't compile. You don't have to use virtual
specifier, this works as expected:
class App
{
static void Main()
{
B classb = new B(1,2,3);
classb.blah(4);
}
}
class A
{
public A(int a, int b, int c)
{
}
}
class B : A
{
public B(int a, int b, int c): base (a, b, c)
{
}
public void blah(int something)
{
Console.WriteLine(something);
}
}
Ok so I've been searching about for a while trying to find an anwer to this question but it is difficult to phrase so I am asking here.
I'm inheriting a class like
Class A (int a, int b, int c)
public A(int a, int b, int c)
{
}
Class B : A
public B(int a, int b, int c) base: (a, b, c)
public void blah(int something)
{
}
Why can't I do this and then do :
B classb = new B(1,2,3);
classb.blah(4);
Instead I have to do
public virtual void blah(int something)
{
}
In class A, then in class B:
public override void blah(int something)
{
//method to be used in B but not A.
}
So even though I have no intention of ever using the method in class A I still have to declare the virtual? So if i'm inheriting class C : B then what? I have to declare stuff in A for C?
Your assumption has no any meaning, at least as much as I understood.
Consider following example:
public class Base {}
public class Derived : Base {
public void DerivedSpecificMethod() {
}
}
if you do
Derived d = new Derived(); //as you specify in code example
d.DerivedSpecificMethod(); //you CAN do this.
The virtual
may be need in case when you write:
Base b = new Derived(); //note, I use a Base here on left side of equation
b.DerivedSpecificVirtualMethod(); //virtual method call
Why can't I do this and then do :
B classb = new B(1,2,3);
classb.blah(4);
You absolutely can. There's no problem with that at all, other than the syntactic errors in your code. Here's a complete example which compiles and runs with no issues. (I've fixed the naming convention violation at the same time for the blah
method.)
using System;
class A
{
public A(int a, int b, int c)
{
}
}
class B : A
{
public B(int a, int b, int c) : base(a, b, c)
{
}
public void Blah(int something)
{
Console.WriteLine("B.Blah");
}
}
class Test
{
static void Main()
{
B b = new B(1, 2, 3);
b.Blah(10);
}
}
There's nothing wrong with this, except that your code is not actually valid C#. I think this is what you want:
class A
{
public A(int a, int b, int c)
{
}
}
class B : A
{
public B(int a, int b, int c) : base(a, b, c)
{
}
public void blah(int something)
{
}
}
Then this is no problem:
B classb = new B(1,2,3);
classb.blah(4);
Your code won't compile. You don't have to use virtual
specifier, this works as expected:
class App
{
static void Main()
{
B classb = new B(1,2,3);
classb.blah(4);
}
}
class A
{
public A(int a, int b, int c)
{
}
}
class B : A
{
public B(int a, int b, int c): base (a, b, c)
{
}
public void blah(int something)
{
Console.WriteLine(something);
}
}
0 commentaires:
Enregistrer un commentaire