The easiest way to do class class constructor inheritance (C #)

Yes, sorry asking for stupid question n00b. So I have a C # program. I have a class

class foo { public int bar, wee, someotherint, someyetanotherint; public foo() { this.bar = 1: this.wee = 12; this.someotherint = 1231; this.someyetanotherint = 443; } } 

And I want to create a class called spleen that inherits from foo

 class spleen : foo { } 

What is the fastest and easiest syntax to make the spleen class inherit the constructor from foo without having to copy / paste the entire constructor from foo? I do not want to hear that I already have too many parameters. I already know that. (edit: Actually, no, I'm an idiot) I know that I have to somehow call the parent constructor, but I don't know how to do this. How to do it.

Edit: Now I understand that I needed more time to write my question. It seems that I tried to ask two questions at the same time, without realizing it (how to inherit constructors without parameters, and how to inherit constructors with parameters), and somehow mixed up all this. However, the answers provided were very helpful and solved my problem. Thank you and wish I were such an idiot!

+7
inheritance constructor c #
source share
4 answers

What you are looking for is a keyword. You can use it to call the base constructor and provide the necessary arguments.

Try the following. I chose 0 by default due to lack of a better option

 class spleen : foo { public spleen() : base(0,0,0,0) { } } 

EDIT

Based on your new version of the code, the easiest way to call the constructor is to literally do nothing. The default constructor created for the spleen will automatically invoke the base empty constructor foo.

 class spleen : foo { public spleen() {} } 
+19
source share

You just create it and call the base constructor:

 public spleen(int bar, int wee, int someotherint, int someyetanotherint) : base(bar,wee,someotherint,someyetanotherint) { // Do any spleen specific stuff here } 
+7
source share

JaredPar is right, but missed something, and I don’t have enough to edit it.

 class spleen : foo { public spleen() : base() } 

If the parent class took a parameter in the constructor, then it will be

 class foo { public int bar, wee, someotherint, someyetanotherint; public foo(int bar, int wee, int someotherint, int someyetanotherint) { this.bar = bar; this.wee = wee; this.someotherint = someotherint; this.someyetanotherint = someyetanotherint; } } class spleen : foo { public spleen(int bar, int wee, int someotherint, int someyetanotherint) : base(bar, wee, someotherint, someyetanotherint) } 
+5
source share

Make the constructor secure:

 protected Operator(String s, int i, boolean flag){ operator = s; precedenceLevel = i; associative = flag; } 

//material

 class MulOperator extends Operator { protected MulOperator(String s, int precedenceLevel, boolean flag) { super(s, precedenceLevel, flag); } 

so on and so forth

EDIT: Suppose they have the same / similar constructor ...

-3
source share

All Articles