So I am making an employee abstract class for a company.
public abstract class Employee
{
private string name;
private int age;
private string title;
private double salary;
public string Name { get; set; }
public int Age { get; set; }
public string Title { get; set; }
public double Salary { get; set; }
}
With each different type of employee class
class Owner : Employee
{
public Owner(Employee name)
{
this.Name = Name;
}
}
class Accountant: Employee
{
public Accountant(Employee name)
{
this.Name = Name;
}
}
}
etc. on class types for different type of employees.
How (if I even setup the classes right) would I then input names, ages, titles, etc. for the different types. I know I don't have all the methods called in the classes.. but I was trying to just start with setting/getting names.
class Program
{
static void Main(string[] args)
{
Owner Oname = new Owner(name: Oname = "Tom");
Accountant Aname = new Accountant(name: Aname ="Tim");
Console.WriteLine("Owner name: " Oname.Name);
Console.WriteLine("Account name: " Aname.Name);
}
}
CodePudding user response:
Firstly, you don't need the private fields. You aren't setting them.
So your models would look like this ...
public abstract class Employee
{
public string Name { get; set; }
public int Age { get; set; }
public string Title { get; set; }
public double Salary { get; set; }
}
public class Owner : Employee
{}
public class Accountant: Employee
{}
Since all of the properties are public you can simply do this ...
new Employee() {
Name = "Employee Name",
Age = 30,
Title = "Foo",
Salary = 50000,
}
new Accountant() {
Name = "Accountant Name",
Age = 50,
Title = "Bar",
Salary = 70000,
}
If you wanted to make the other fields protected, you'd have to do something like ...
public abstract class Employee
{
protected Employee(int age, string title, double salary) {
this.Age = age;
this.Title = title;
this.Salary = salary
}
public string Name { get; set; }
protected int Age { get; set; }
protected string Title { get; set; }
protected double Salary { get; set; }
}
public class Owner : Employee
{
public Owner(int age, string title, double salary)
: base(age, title, salary)
}
public class Accountant: Employee
{
public Accountant(int age, string title, double salary)
: base(age, title, salary)
}
Since all of the properties are public you can simply do this ...
new Employee(30, "Foo", 50000) {
Name = "Employee Name",
}
new Owner(50, "Bar", 70000) {
Name = "Owner Name",
}
CodePudding user response:
Your usage of some data types and syntax here is wrong.
Classes (sub class) which inherit from another class (super/base class) will inherit all of the non-private properties that exist in the super class as though they existed in the sub class. The structure of your classes look fine, there's just a few syntactical / compile-time issues.
In your constructor for Accountant
and Owner
, you're expecting an Employee
type, which you call name
. You're assigning it to the Name
property of the instance, which is a string, so you'll need to either pass a string or change what you're assigning the parameter to.
class Owner : Employee
{
public Owner(string name)
{
this.Name = name;
}
}
These lines in your Main method are also syntactically incorrect:
Owner Oname = new Owner(name: Oname = "Tom");
The correct syntax here is
Owner Oname = new Owner(name: "Tom");
// or more simply
Owner Oname = new Owner("Tom");
The same would apply for all the other properties you have. You can set the properties by assigning to them: Oname.Age = 18;
or just adding another parameter in the constructor and assign it in there. There's plenty of options, that's just two examples.
CodePudding user response:
From your sample, it seems like you only require the Name field, so make that the only required constructor parameter. I've done it here with an interface, but you can certainly do it with an abstract class as well.
public interface IEmployee
{
string Name { get; set; }
int? Age { get; set; }
string? Title { get; set; }
decimal? Salary { get; set; }
}
public class Owner : IEmployee
{
public string Name { get; set; }
public int? Age { get; set; }
public string? Title { get; set; }
public decimal? Salary { get; set; }
public Owner(string name, string? title = null, int? age = null, decimal? salary = null)
{
Name = name;
Age = age;
Title = title;
Salary = salary;
}
}
var owner = new Owner("person", salary: 1000m);
CodePudding user response:
at first you have to fix your abstract class
public abstract class Employee
{
private string name;
private int age;
private string title;
private double salary;
public string Name {
get {return name;}
set {name=value}
//or probably
private set {name=value}
}
... and so on
}
or remove all private fields, since they are not used now
and you have 2 choices in concrete class
class Owner : Employee
{
public Owner(string name)
{
name = name;
}
}
or
public Owner(Employee employee)
{
name = employee.Name;
... and so on
}