【问题标题】:Why I can't call the class in the C# composition [closed]为什么我不能在 C# 组合中调用类[关闭]
【发布时间】:2014-05-06 15:54:59
【问题描述】:

你好,我有一个使用类组合的 OOP Homewrok,我创建了两个类 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
                 */
            }

        }
    }
}

【问题讨论】:

  • 阅读并报告“不会运行”的含义。还要花时间搜索所述错误消息/症状。
  • 您的意思是employeeDate 中的一个嵌套类?我认为“员工”在任何概念上都不是“日期”的属性。
  • 员工类嵌套在日期类中。提取它。并从程序中提取日期和员工。您必须新建一个员工实例。如果你不嵌套类,你会得到更少的命名空间和范围奇怪。
  • 为什么需要Date 类?为什么不使用内置的Date 类?
  • @user3608930 显然不是因为你不能编译它。

标签: c# class oop methods


【解决方案1】:

您的 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();
    }
}

【讨论】:

  • WOOW 伙计谢谢你我忘了把 {} 关闭日期类关闭对不起伙计们我的错:/
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 2018-11-17
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    相关资源
    最近更新 更多