【问题标题】:Abstract classes and virtual methods: cannot access certain variables抽象类和虚方法:不能访问某些变量
【发布时间】:2012-10-10 11:48:57
【问题描述】:

我正在使用抽象类和虚拟方法。目前,我有一个由按钮单击和两个多行文本框组成的窗口窗体,我在其中显示结果。基类有一个默认构造函数和一个接受基类所需的所有必要数据的构造函数。有两个类TreesTomatoes,它们有一个默认构造函数和一个传递所有数据的构造函数。我可以在 textBox1 中显示项目 namenumber in stockprice

但是,我无法为 Trees 调用和显示名为 tree_height 的高度变量以及名为 tomatoes_perplattomatoes_size 的每个平台的大小和西红柿变量 Tomatoes。它给了我错误不包含这些变量的定义。我该如何解决?

代码

namespace plant_shop
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public abstract class Plants
        {
            public string the_name;
            public double num_stock;
            public double price_peritem;
            public double total_item_value;

            public Plants(string new_name, int new_stock, double new_price)
            {
                the_name = new_name;
                num_stock = new_stock;
                price_peritem = new_price;
            }

            public override string ToString()
            {
                return "";
            }

            public virtual double Get_Value()
            {
                double s = 0;
                return s;
            }


        }

        public class Trees : Plants
        {
            double tree_height;
            public Trees(string new_name, int new_stock, double new_price, double new_height)
                : base(new_name, new_stock, new_price)
            {
                tree_height = new_height;
                total_item_value = num_stock * price_peritem;
            }

            public override string ToString()
            {
                string s = "Tree" + "     " + num_stock + "      " +  the_name + "     " + price_peritem + "    " + tree_height ;
                return s;
            }

            public override double Get_Value()
            {
                total_item_value = num_stock * price_peritem;
                return total_item_value;
            }

        }

        public class Tomatoes : Plants
        {

            string sizeoftomato;
            int tomatoesinpat;

            public Tomatoes(string new_name, int new_stock, double new_price, int tomatoes_perplat, string tomatoes_size)
                : base(new_name, new_stock, new_price)
            {
                tomatoesinpat = tomatoes_perplat;
                sizeoftomato = tomatoes_size;  
                total_item_value = num_stock * price_peritem; 
            }

            public override string ToString()
            {
                string s = "Tomatoes" + "     " + num_stock + "      " + the_name + "     " + price_peritem;
                return s;
            }

            public override double Get_Value()
            {

                total_item_value = num_stock * price_peritem;
                return total_item_value;
            }
        }



        public void Report()
        {

             Trees trees1 = new Trees("Oak", 3, 14.40, 2);
             const string format = "{0,-26} {1,-25} {2,-25} {3,-25}";
             string trees1_result = String.Format(format, "Tree", trees1.the_name, trees1.num_stock, trees1.price_peritem, trees1.tree_height); //not showing `tree_height`            
             textBox1.AppendText(trees1_result + Environment.NewLine);
             textBox2.AppendText(trees1.Get_Value() + Environment.NewLine);            

             Tomatoes tomatoes1 = new Tomatoes("Big Boy", 30, 10, 12, "Large");
             const string format2 = "{0,-26} {1,-25} {2,-25} {3,-25}";
             string tomatoes1_result = String.Format(format2, "Tomatoe", tomatoes1.the_name, tomatoes1.num_stock, tomatoes1.price_peritem, tomatoes1.tomatoes_perplat, tomatoes1.tomatoes_size); //not showing `tomatoes_perplat` or `tomatoes_size`
             textBox1.AppendText(tomatoes1_result + Environment.NewLine);
             textBox2.AppendText(tomatoes1.Get_Value() + Environment.NewLine);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Report();
        }

    }
}

【问题讨论】:

  • 如果: 1) 您遵循 .NET 命名约定,那将非常有帮助; 2) 您创建了一个简短但完整的程序,准备好尝试编译。 3)你没有使用嵌套类,这不必要地使事情复杂化。 4)您创建了一个控制台应用程序而不是 GUI(简单得多,并且可以轻松显示语言问题 5)您说过错误在哪里。无论如何我会看看,但你本可以帮助我们帮助你......

标签: c#


【解决方案1】:

好的,现在我已经编译了一些代码,它更清晰了......

您无法访问 trees1.tree_height,因为它在 Trees 类中是私有的,并且您正尝试从 Form1 类中的方法访问它。

您无法从该方法访问 tomatos_perplat,因为在 Tomatoes 类中没有这样的变量 - 有 tomatoesinpat,但这也是私有的。

同上tomatoes_size - 该字段称为sizeoftomato,并且是私有的。

将您的字段保密是一个的主意 - 我不建议将它们公开。相反,您应该公开properties 以公开类之间的数据。您可以使用automatically implemented properties 作为实现非常简单属性的简单方法。

强烈鼓励你在你的组织工作:

  • 不要对所有东西都使用嵌套类;它们偶尔会很方便,但通常最好有单独的顶级类

  • 阅读.NET naming conventions并关注他们

  • 想想你的方法试图达到什么目的 - 像你的 Get_Value 方法这样的事情目前毫无意义

  • 我会敦促您尝试使用控制台应用程序。它们不像 GUI 那样“令人兴奋”,但更易于理解和试验。

【讨论】:

  • +1 完美解释。你能告诉我如何使用我的示例程序公开属性以在类之间公开数据吗?
【解决方案2】:

您在没有指定access modifier 的类中定义fields。默认情况下,这些字段将是私有的。

您应该使用public properties 来公开这些字段。您还应该考虑auto-implemented properties

【讨论】:

    【解决方案3】:

    这将解决它:

    public string sizeoftomato;             
    public int tomatoesinpat;
    

    这些字段默认为私有

    请注意,字段通常不适合您的工作。你真的想要属性:

    public string SizeOfTomato { get; set; }
    public int TomatoesInPlant { get; set; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 2021-02-21
      • 2012-04-27
      相关资源
      最近更新 更多