问题描述
大家好,
我在这里很困惑,不确定是否有合适的地方澄清,但这里有。该代码是来自20483#course的Microsoft Lab练习的一部分
我有这个课程: -
公共班学生:IComparable< Student>
{
public int StudentID {get;组; }
公共字符串FirstName {get;组; }
公共字符串LastName {get;组; }
//初始化新学生
公共学生的属性的构造函数(int studentID,string firstName,string lastName)
{
StudentID = studentID;
FirstName = firstName;
LastName = lastName;
}
//默认构造函数
public Student()
{
StudentID = 0;
FirstName = String.Empty;
LastName = String.Empty;
}
//根据LastName和FirstName属性比较学生对象
//所以我们实际上是在传递学生被命名为其他
//对象的对象也是实例
public int CompareTo(Student other)
{
//连接此学生的LastName和FirstName
string thisStudentsFullName = LastName + FirstName;
//连接"其他"的LastName和FirstName。 student
string otherStudentsFullName = other.LastName + other.FirstName;
//使用String.Compare比较连接的名称并返回结果
return(String.Compare(thisStudentsFullName,otherStudentsFullName));
}
注意CompareTo方法,我知道我在这里实现了接口,但我的问题是关于"Student other",Student的对象作为方法中的参数传递。
对象是类权利的实例,并使用"NEW"创建。关键词。那么当没有创建新实例时,该方法如何将对象作为参数?我迷失在这里。我是新手。
Hi all,
I am kind of confused here, not sure if the right place to clarify, but here goes. The code is part of the Microsoft Lab exercise from the 20483# course
I have got this class : -
public class Student: IComparable<Student> { public int StudentID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } // Constructor to initialize the properties of a new Student public Student(int studentID, string firstName, string lastName) { StudentID = studentID; FirstName = firstName; LastName = lastName; } // Default constructor public Student() { StudentID = 0; FirstName = String.Empty; LastName = String.Empty; } // Compare Student objects based on their LastName and FirstName properties // so we are actually passing the student object which is named as other // objects are also instances public int CompareTo(Student other) { // Concatenate the LastName and FirstName of this student string thisStudentsFullName = LastName + FirstName; // Concatenate the LastName and FirstName of the "other" student string otherStudentsFullName = other.LastName + other.FirstName; // Use String.Compare to compare the concatenated names and return the result return(String.Compare(thisStudentsFullName, otherStudentsFullName)); }
Notice the CompareTo method, I know I am implementing the interface here, but my question is on the "Student other", the object of Student which is passed as a parameter in the method.
Object is an instance of a class right and is created by using the "NEW" keyword. How is that then the method takes the object as a parameter when no new instance has been created? I am lost here. I am newbie.
这篇关于需要说明 - 将对象作为方法参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!