开发者

C# Class reference into another Class but Why?

i am beginner in c# i did't understand why put class reference into another class, here is the code.

  interface IDB
{
    void AddDoctor(Doctor doc);
    void AddPatient(Patient pat);
    void AddWard(Ward ward);
}

    class DataBaseManager 
{
    private IDB db;

    public DataBaseManager(IDB dbase)
    {
        this.db = dbase;
    }


class ComputerOperator
{
    private string name;
    private int age;
    private DataBaseManager dbm;

    public ComputerOperator(string name, int age,SQLDB sqdata)
    {
        this.name = name;
        this.age = age;
        dbm = new DataBaseManager(sqdata);
    }

    class BillingDepartment
    {
    private ComputerOperator operater;

    public ComputerOperator Operater
    {
        get { return operater; }
        set { operater = value; }
    }
   }

When we put the class into another class is called NESTED CLASSES, then me what is the name of this thing in c# "PUT THE CLASS REFERENCE INTO ANOTHER CLASS."

Edited to add sir, i am asking about why we need "a class reference into another class", and is that any alternate if we dont put a class reference into another class??? for example,

      namespace myNameSpace                                                                                          开发者_运维百科                                                                                                                                              
      {
       public class class1
      {
        //methods
      }

      public class class2
     { 
      private class1 myclass1;
       //methods
     }


A reference to another class is a different concept of a nested class.

  • Reference: is a field or property that holds an instance of another class.
  • Nested class: is a class declaration that exists inside another class declaration.

Nested classes are mainly used when you want to hide the existence of that class from the world. A nested private class, can only be used by the class that owns it.

Also, nested classes know all the private things that exists in the owner class.


I'm guessing what is meant (because it isn't very clear from the context or your wording) that you have a field containing a reference to another instance of a class. It has a reference to an object whose type is that of the referenced class.

public class A
{
    public class Nested
    {
    }

    public Nested _nestedFieldRefrence;
    public B _otherClass;
}

public class B
{
}

In class A you have * A nested class called Nested * A reference to an instance of the nested class called _nestedFieldReference * A reference to another class called _otherClass

Does that help your understanding?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜