为什么我无法在C#组合中调用该类

时间:2014-05-06 15:55:00

标签: c# class oop methods

HEllo我有一个使用类组合的OOP Homewrok,我创建了两个Class 1.date,我可以在主2.employee中调用它,但是我不能称之为创建一个对象,这里是代码

namespace ConsoleApplication1
{
    class Program
    {
        public class date
        {
            private int day;
            private int mounth;
            private int year;

            public date(int day, int mounth, int year)
            {
                this.day = day;
                this.mounth = mounth;
                this.year = year;

            }
            public void Display()
            {

                Console.WriteLine("{0}/{1}/{2}", day, mounth, year);

            }

            public class employee
            {
                private string name;
                private date hire_date;
                private date Emplye_birth;
                private int number;

                public employee(int number, string name, date bd, date hd)
                {
                    this.name = name;
                    this.number = number;
                    hire_date = hd;
                    Emplye_birth = bd;

                }
                public void printEmplyy()
                {
                    Console.WriteLine("Employee number {0} \n name:{1}", number, name);
                    Console.WriteLine("hire date of {0}", name);
                    hire_date.Display();
                    Console.WriteLine("birthday of {0}", name);
                    Emplye_birth.Display();

                }
            }

        }
        static void Main(string[] args)
        {

            date Khaled_birth = new date(10, 10, 1995);
            date khaled_hire = new date(10, 10, 2017);
            string user;
            int number;
            Console.WriteLine("Please Enter the user and password ");
            Console.Write("USERNAME:"); user = Console.ReadLine();
            Console.Write("Password"); number = Convert.ToInt32(Console.ReadLine());
            /*
            employee khaled_info = new employee(number, user, khaled_hire, Khaled_birth); 
           that code wont to run 
             */
            if (user == "khaled" && number == 001995)
            {
                /*    khaled_info.printEmplyy(); 
               that code wont to run
                 */
            }

        }
    }
}

2 个答案:

答案 0 :(得分:0)

您的employee课程已嵌套在date课程中。如果你拔出它,那么你可以访问它。像这样:

public class date
{
    private int day;
    private int month;
    private int year;

    public date(int day, int month, int year)
    {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public void Display()
    {
        Console.WriteLine("{0}/{1}/{2}", day, month, year);
    }
}

public class employee
{
    private string name;
    private date hire_date;
    private date Employee_birth;
    private int number;

    public employee(int number, string name, date bd, date hd)
    {
        this.name = name;
        this.number = number;
        hire_date = hd;
        Employee_birth = bd;
    }

    public void printEmployee()
    {
        Console.WriteLine("Employee number {0} \n name:{1}", number, name);
        Console.WriteLine("hire date of {0}", name);
        hire_date.Display();
        Console.WriteLine("birthday of {0}", name);
        Employee_birth.Display();
    }
}

答案 1 :(得分:0)

或使用员工类的完整路径,如:

Program.date.employee khaled_info = new Program.date.employee(...);

顺便说一句,你有1个字符串格式错误。