如何从子类对象创建基类对象

时间:2016-02-04 06:13:31

标签: c# class inheritance

我有两节课。一个是Base类,它在Child类中继承。

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Manager : Employee
{
    public decimal Salary { get; set; }
    public Employee GetEmployee()
    {
        // return employee object.
    }
}

现在,我需要通过Employee类对象获取Manager类对象。

Manager manager = new Manager();
manager.FirstName = "A";
manager.LastName = "B";
manager.Salary = 25000;

Employee employee = manager.GetEmployee();

我不确定,如果这是可能的,但我不想逐一分配特定的财产。

请建议更好的选择。

4 个答案:

答案 0 :(得分:2)

****Console Error:**** [TestNG] Running: /tmp/testng-eclipse-1773582615/testng-customsuite.xml FAILED CONFIGURATION: @BeforeTest setUp org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Command failed: /bin/sh -c "/usr/bin/adb" -s adb server is out of date. killing... wait-for-device ADB server didn't ACK * could not start server * ) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 14.00 seconds Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52' System info: host: 'rishabhambre-Lenovo-B50-70', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.19.0-25-generic', java.version: '1.8.0_66' Driver info: org.openqa.selenium.remote.RemoteWebDriver at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:247) at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:129) at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:156) at android.SimpleAndroidCalcTest.setUp(SimpleAndroidCalcTest.java:29) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86) at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142) at org.testng.TestRunner.beforeRun(TestRunner.java:656) at org.testng.TestRunner.run(TestRunner.java:624) at org.testng.SuiteRunner.runTest(SuiteRunner.java:366) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319) at org.testng.SuiteRunner.run(SuiteRunner.java:268) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244) at org.testng.TestNG.runSuitesLocally(TestNG.java:1169) at org.testng.TestNG.run(TestNG.java:1064) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177) SKIPPED: SKIP =============================================== Default test Tests run: 1, Failures: 0, Skips: 1 Configuration Failures: 1, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Failures: 0, Skips: 1 Configuration Failures: 1, Skips: 0 =============================================== [TestNG] Time taken by org.testng.reporters.JUnitReportReporter@2a098129: 15 ms [TestNG] Time taken by org.testng.reporters.EmailableReporter2@16b4a017: 5 ms [TestNG] Time taken by org.testng.reporters.jq.Main@68f7aae2: 17 ms [TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@2f410acf: 7 ms [TestNG] Time taken by org.testng.reporters.XMLReporter@1996cd68: 6 ms [TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 2 ms Manager,因此您只需返回Employee

this

方法public Employee GetEmployee() { return this; } 的目的尚不完全清楚,因为您可以在任何需要GetEmployee的地方使用Manager

Employee

答案 1 :(得分:1)

您已拥有Employee对象的Manager对象。例如,如果您有一个方法打印员工的全名,例如

void PrintFullName(Employee e)
{
  Console.WriteLine("{0} {1}", e.FirstName, e.LastName));
}

您只需使用管理员对象调用此函数,例如

Manager m = new Manager();
m.FirstName = "M";
m.LastName = "S";
m.Salary = 10000;
PrintFullName(m);

基本继承,我不明白你为什么需要GetEmployee方法。

答案 2 :(得分:0)

您不必向Manager类添加方法,如果需要获取没有manager属性的直接Employee类型引用,则可以使用as运算符将其强制转换。

var employee = manager as Employee;

或者如果你有一个功能

public void DoStuff(Employee employee)
{
}

你可以用

来调用它
DoStuff(new Manager())

答案 3 :(得分:0)

以下是使用反射的通用方法

public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class Manager : Employee
    {
        public decimal Salary { get; set; }
        public T GetEmployee<T>()
        {
            var emp = Activator.CreateInstance<T>();
            var properties = emp.GetType().GetProperties();
            foreach (var property in properties)
            {
                property.SetValue(emp, property.GetValue(this));
            }
            return emp;
        }
    }

使用

调用它
Employee employee = manager.GetEmployee<Employee>();