随着子类填充基类? [英] Populate base class along with child class?

查看:129
本文介绍了随着子类填充基类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要,我有一些新的字段添加到现有的类及其所有现有油田/属性。

所以每当我的派生类填充DAL,我会填基类的所有领域也是如此。目前,我做它像这样,但不知道这是正确的方式?请给我一个例子。另外,我不知道基类对象是否会是一个派生类初始化一个新的每一次?

 公共类员工
    {
        私人诠释_id;
        私人诠释_name;
        公众诠释ID
        {
            集合{_id =值;}
            {返回_id;}
        }
        公众诠释名称
        {
            集合{_name =值;}
            {返回_name;}
        }
        保护无效SETNAME()
        {
            _name =价值;
        }
        保护无效SETID()
        {
            _id =价值;
        }
    }
公共类EmployeeWithDepartmentName:员工
        {
           私人字符串_deptName;
           公共字符串DEPTNAME
            {
                集合{_deptName =价值; }
            }
            公共setBaseEmpName()
            {
               base.SetName();
            }
        公共setBaseID()
            {
               base.SetID();
            }
        }
 

解决方案

一切都在一个基类可以自动的从派生类访问,而不会doiing什么,就直接使用属性/方法名。

 公共类MyBase
{
    公共字符串用户名{获取;集;}
}

公共类MyClass的:MyBase
{
  公共无效DoSomething的()
  {
     Console.WriteLine(用户名:{0},用户名);
     用户名=安德斯;
  }
}
 

您也可以做到这一点:

  MyClass的MyClass的=新MyClass的();
myClass.UserName =符文;
 

受保护的,只有派生类可以访问属性/方法的手段。公共意味着每个人都可以访问属性/方法。

另外,我不知道基类对象是否将每个派生类进行初始化时是新的?

这不是两个对象,它是一个对象,从两个不同的类(这是继承是如何工作的)。

创建

阅读这篇文章,关于继承: http://www.csharp-station.com/教程/ lesson08.aspx

I have a need where I have to add some new fields to an existing class along with all its existing fields/attributes.

So whenever my derived class is filled by DAL, I will be filling all fields of base class as well. Currently, I am doing it like this but not sure this is the right way ? Please give me an example. Also I am not sure whether the base class object will be a new one each time a derived class is initialized ?

public class Employee
    {
        private int _id;
        private int _name;
        public int ID
        { 
            set { _id=value;}
            get { return _id;}
        }
        public int Name
        { 
            set { _name=value;}
            get { return _name;}
        }
        protected void SetName ()
        {
            _name=value;
        }
        protected void SetID()
        {
            _id=value;
        }
    }
public class EmployeeWithDepartmentName:Employee
        {
           private string _deptName;
           public string DeptName 
            {
                set { _deptName=value; }
            }
            public setBaseEmpName()
            {
               base.SetName();
            }
        public setBaseID()
            {
               base.SetID();
            }
        }

解决方案

Everything in a base class can automagically be accessed from derived classes without doiing anything, just use the property/method name directly.

public class MyBase
{
    public string UserName {get;set;}
}

public class MyClass : MyBase
{
  public void DoSomething()
  {
     Console.WriteLine("UserName: {0}", UserName);
     UserName = "Anders";
  }
}

You can also do this:

MyClass myClass = new MyClass();
myClass.UserName = "Rune";

Protected means that only derived classes can access the property/method. Public means that everyone can access the properties/methods.

Also I am not sure whether the base class object will be a new one each time a derived class is initialized ?

It's not two objects, it's one object created from two different classes (that's how inheritance works).

Read this article about inheritance: http://www.csharp-station.com/Tutorials/lesson08.aspx

这篇关于随着子类填充基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆