本文介绍了访问类外的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 查看代码Have a look at the codeusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication3{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Hello a = new Hello(); a.aa = "string"; //correct a.bb = "String"; //correct a.cc = "SB"; //error } } public class Hello { public string aa; public String bb; StringBuilder cc = new StringBuilder(); }} 在此,我可以访问 string 和 String 值。这两者有什么区别? 为什么我无法访问 StringBuilder 对象?In this, I am able to access string and String values. What is the difference between the two ?And, why cant i access the StringBuilder object ?推荐答案访问修改器是您可以访问字符串/字符串字段而不是StringBuilder的原因。 阅读本文: 访问修饰符@ MSDN [ ^ ] 这是一个非常基本的概念,在你进入任何合理的开发之前应该被理解,因为它提供了对象如何交互的基础。 至于关于字符串的问题和String,没有什么区别。 string是String(全名System.String)的别名,因此在声明对象时,您可以使用它们。但是,如果你希望在String类中使用静态方法,最好使用String而不是string。Access Modifers are the reason you can access your "String/string" fields and not the StringBuilder.Read this article:Access Modifiers @ MSDN[^]This is a very basic concept and should be understood before you venture into any reasonable development as it provides the basis for how objects can interact.As for your question regarding string and String, there isn't really a difference. string is an alias for String (Full name System.String) so when declaring objects you can use either. However if you wish to use static methods off of the String class you are better to use String rather than string.关于差异的问题根本没有意义。差异是什么意思?甚至表达访问字符串和字符串值也不正确。没有访问价值这样的概念。您正在访问该类的两个成员,他们碰巧具有上述值。看到区别? 关于第二个问题,答案很简单:你正在尝试分配 a.cc 到SB,但SB是一个字符串对象, a.cc 不是,这是一个的实例StringBuilder 类。这两种类型不是赋值兼容。此外,只有当其访问修饰符为 internal 或更易于访问时,才能从类外的某些代码(而不是派生类)访问该成员。默认情况下,它是 private ,它不允许外部访问(除非你使用反射,但这是一个单独的主题)。 怎么办?回到您的编程研究的一些更基本的主题。注意类型,变量和类/结构成员的概念,引用类型及其实例,理解引用对象和引用对象。了解如何使用字符串(字符串类是一种非常不寻常的引用类型)。 -SAThe question about "difference" simply makes no sense. What do you mean by difference? Even the expression "to access string and String values" isn't correct. There is no such concept as "accessing values". You are accessing two members of the class, and they happen to have the said values. See the difference?As to the second question, the answer is way to simple: you are trying to assign a.cc to "SB", but "SB" is a string object, and a.cc is not, this is the instance of a StringBuilder class. Those two types are not assignment-compatible. Besides, the member becomes accessible from some code outside class (and not from derived class) only if its access modifier is internal or more accessible. By default, it is private, which does not allow for outside access (unless you use reflection, but this is a separate topic).What to do? Get back to some more basic topics of your study of programming. Pay attention for the concepts of type, variables and members of classes/structures, for reference types and their instances, understand the reference objects and referenced objects. Learn how to work with strings (the String class is a very unusual reference type).—SA你不能直接为StringBuilder对象赋值。 你必须使用成员函数追加()做同样的事情。You cannot directly assign a value to StringBuilder objects.You have to use member function Append() to do the same.using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication3{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Hello a = new Hello(); a.aa = "string"; //correct a.bb = "String"; //correct a.cc.Append("SB")//error } } public class Hello { public string aa; public String bb; StringBuilder cc = new StringBuilder(); }} 也许这会起作用Maybe this will work 这篇关于访问类外的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-27 14:59