繁体   English   中英

C#OOP的说明

[英]Clarification on C# OOP

从基本看一下在main方法中运行的测试器类,我们已经获得了构造面向对象类的任务。

using System;
using school; 

namespace testschool{

public class Tester {
   static void Main(){ 
    Faculty scienceFac=University.createFaculty("Science");
    Department compSciDept=  scienceFac.openNewDepartment("Computer Science"); 
    Department physicsDept=  scienceFac.openNewDepartment("Physics"); 
    Console.WriteLine(Object.ReferenceEquals(
        scienceFac, physicsDept.Faculty)); //expected to return scienceFac object
    Console.WriteLine(University.numberOfFaculties());

//..... MORE CODE

我认为他使代码尽可能地令人困惑,并且确实变得令人困惑。 我刚刚开始,但已经陷入困境,但是到目前为止,这是我所拥有的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace testschool
{
    class University
    {
        List<Faculty> all_faculty = new List<Faculty>();

        public Faculty createFaculty(string faculty_name)
        {
            Faculty new_faculty = new Faculty(faculty_name);
            all_faculty.Add(new_faculty);
            return new_faculty;
        }

        public int numberOfFaculties()
        {
            return all_faculty.Count;
        }
    }

    class Faculty
    {
        string faculty_name;
        List<Department> all_departments = new List<Department>();

        public Faculty(string faculty_name)
        {
            this.faculty_name = faculty_name;
        }

        public Department openNewDepartment(string department_name)
        {
            Department new_department = new Department(department_name, this);
            all_departments.Add(new_department);
            return new_department;
        }


    }

    class Department
    {
        string department_name;
        Faculty parent_faculty;
        public Department(string department_name, Faculty faculty)
        {
            this.department_name = department_name;
            parent_faculty = faculty;
        }

        public Faculty Faculty
        {
            get { return parent_faculty; }
        }
    }
}

到目前为止,我有两个问题:

第一:这行代码: Faculty scienceFac = University.createFaculty("Science"); 似乎只是在没有对象引用的情况下立即调用大学。 我宣布大学为一类,因为它内部似乎也有.createFaculty(“ Science”)和.numberOfFaculties()之类的方法。 那可能是个错误吗? 还是大学实际上是一个名称空间或其他具有自己方法的东西?

第二:一所大学有系,一所大学有系,最终到了有学生和课程的地步。 从代码中可以看出,我使用了列表,但是我们没有涉及到列表。 仅数组(是的,我知道列表也是数组,但是我只想坚持使用数组)。 是否可以在不声明预定义大小的情况下使用数组? (我总是可以将大小设置为999之类的大字,但实际上并不实用)

问题的具体答案:

  1. University.createFaculty()是静态方法。 使用类型名称而不是实例来调用它。

  2. 要创建数组实例,必须指定大小。 但是变量本身是在没有任何大小信息的情况下声明的。

我认为这可以解决您的具体要求。 当然,在教育环境中,如果有空的话,最好与您的老师协商。 这可以帮助您获得更适合该课程的答案,以及老师希望您学习的内容,还可以帮助老师了解每个学生在理解课程和作业方面所处的位置。

1-正如@Peter Duniho提到的那样, University.CreateFaculty()是静态方法

public class University
{
    public static void CreateFaculty()
    {
        // a static method declared inside a class can be called without creating an instance of the object
    } 
}

2-要调整数组的大小,可以执行以下操作

int[] data=new int[2];
data[0]=1;
data[2]=2;

data=ResizeData(data,3); // this function will return new array with array with length=5

public int[] ResizeData(int[] data,int number)
{
    int[] newArray=new int[number]();
    for(int i=0;i<data.Length;i++) // copy the data from dataArray to the newArray
       newArray[i]=data[i];
    return newArray;
}

希望这个能对您有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM