使用对象如何访问功能? [英] Using object how to access the function?

查看:48
本文介绍了使用对象如何访问功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Student   {

       public string Student_Name{ get; set; }

       public bool getting_StudentMark(string StudentMark)
       {
           Marks s = new Marks();
           s.English= StudentMark;
           return true ;
       }


       public class Marks
        {
          public string English{get;set;}
        } 
   }


...................................................


.........................................

Student  d=new Student();
Console.WriteLine("  Student_Name         : " + d.Student_Name);
Console.WriteLine("  Student_Mark        : " + d.StudentMark);
//i dont know to access the value from the function getting_StudentMark


访问该值
我不知道从功能geting_StudentMark访问学生分数.



I dont know to access the student mark from the function getting_StudentMark.

推荐答案

这将是
Student d = new Student();
//...
bool mark = getting_StudentMark(/* ... */);
// improve naming, don't violate Microsoft naming conventions;
// use something like GetStudentMark


的东西
该函数的语义似乎没有多大意义.由于我不知道您要达到什么目标,因此我无法提供任何建议.看来使用属性代替功能可能会更清楚.

看,在阅读整个语言和平台手册之前开发一些代码毫无意义.您的问题表明您几乎没有阅读任何内容.采用这种方法,无论您问多少问题,您都不会走得很远...

—SA



The semantic of this function does not seem to make much sense. As I don''t know what you want to achieve, I cannot advice anything certain. It looks like using property instead of function could be more clear.

Look, developing some code before you read the whole language and platform manual make little sense. Your question shows that you did not read nearly anything. With such approach, you won''t go far, no matter how much questions you ask…

—SA


这不是很好的代码,您知道-为什么当方法"getting_StudentMark"没有得到"时,为什么要调用它呢?任何东西-它总是返回布尔值,与标记无关(实际上始终是真的)?称它为混乱,应该避免.而且你甚至都没有称呼它...

而且由于多种原因,您无法访问该值:
首先是它不存在.您已经在方法中创建了Marks类的新实例,并设置了它的English属性.但是,在方法结束时,它超出了范围,并被丢弃了,因此您将无法再使用它.
第二个是即使它被保存了,您也没有提供任何访问它的系统:它需要在Student类中专门声明,以便可以对其进行访问.试试这个:
That''s really not very good code, you know - why are you calling a method "getting_StudentMark" when it doesn''t "get" anything - it always returns a bool value, which has nothing to do with the mark, (and is in fact always true)? Calling it that is just confusing, and should be avoided. And you don''t even call it...

And you can''t access the value for a number of reasons:
The first is that it doesn''t exist. You have created a new instance of the Marks class in the method, and set it''s English property. But it then goes out of scope at the end of the method, and is disposed, so you can''t use it ever again.
The second is the even it is was saved, you don''t provide any system for accessing it: it needs to be specifically declared within the Student class so that it can be accessed. Try this:
public class Student
    {
    private Marks _Marks = new Marks();
    public string Student_Name { get; set; }
    public Marks Marks
        {
        get { return _Marks; }
        private set { _Marks = value; }
        }
    }
public class Marks
    {
    public string English { get; set; }
    }
...
            Student d = new Student();
            d.Marks.English = "Hello";
            Console.WriteLine("  Student_Name         : " + d.Student_Name);
            Console.WriteLine("  Student_Mark        : " + d.Marks.English);


这篇关于使用对象如何访问功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆