由于保护级别,无法访问

本文关键字:访问 于保护 保护 | 更新日期: 2023-09-27 18:08:28

这里还有一个问题我想解释一下。在第47行,我得到错误说:

错误CS0122: 'PROPERTY NAMES'是不可访问的,由于其保护级别(CS0122)

问题是为什么会出现错误,我如何避免这个错误?如果我没做错的话。我应该调用属性还是变量呢?我猜是属性。

p。代码尚未完成

注意!我的教授给了我们一个类图,告诉我们应该有哪些属性,哪些应该有get/set,哪些只是set。

代码问题:

    public void display()
    {
        Console.Write("{0}'n{1}'n{2}'n{3}", Title, getAuthorName(), PublisherName, Price);
    }
整个代码:

using System;
namespace Lab_3
{
    class BookTest
    {
        static void Main(string[] args)
        {
            Book book1 = new Book();
            Book book2 = new Book("Advenced C#", "Joe", "Robertson", 29.99f, "PUC Press");
        }
    }
    public class Book
    {
        string authorFirstName;
        string authorLastName;
        float price;
        string publisherName;
        string title;
        public Book()
        {
            Console.Write("Enter book title: ");
            Title = Console.ReadLine();
            Console.Write("Enter author's first name: ");
            AuthorFirstName = Console.ReadLine();
            Console.Write("Enter author's last name: ");
            AuthorLastName = Console.ReadLine();
            Console.Write("Enter price: ");
            Price = float.Parse(Console.ReadLine());
            Console.Write("Enter publisher's name: ");
            PublisherName = Console.ReadLine();
        }
        public Book(string bookTitle, string firstName, string lastName, float bookPrice, string publisher)
        {
            authorFirstName = firstName;
            authorLastName = lastName;
            price = bookPrice;
            publisherName = publisher;
            title = bookTitle;
        }
        public void display()
        {
            Console.Write("{0}'n{1}'n{2}'n{3}", Title, getAuthorName, PublisherName, Price);
        }
        public string getAuthorName()
        {
            return AuthorFirstName + AuthorLastName;
        }
        public string AuthorFirstName
        {
            get
            {
                return authorFirstName;
            }
            set
            {
                authorFirstName = value;
            }
        }
        public string AuthorLastName
        {
            get
            {
                return authorLastName;
            }
            set
            {
                authorLastName = value;
            }
        }
        public float Price
        {
            set
            {
                price = value;
            }
        }
        public string PublisherName
        {
            set
            {
                publisherName = value;
            }
        }
        public string Title
        {
            set
            {
                title = value;
            }
        }
    }
}

编辑:解决!谢谢大家的帮助。

总之,我不能使用属性,因为有些是只读的,这给我带来了问题。所以我需要使用私有变量来显示它们

由于保护级别,无法访问

因为你是在display()方法所在的类中,所以不用麻烦使用属性,只使用数据成员本身,因为这可能是你的教授想要的。属性用于向类的用户公开数据。在类中,您可以自由地使用数据成员,而不需要getter或setter:

Console.Write("{0}'n{1}'n{2}'n{3}", title, getAuthorName(), publisherName, price);

问题是您的属性缺少getter,即

public string Title
{
    set
    {
        title = value;
    }
    get
    {
        return title;
    }
}

编辑:您的display()方法应该如下所示:

public void display()
{
    Console.Write("{0}'n{1}'n{2}'n{3}", title, getAuthorName(), publisherName, price);
}    

注意为arg# 3调用getAuthorName()方法。

Price, PublisherName和Title没有getter,只有setter。

public string Title { private get; set; } // Private getter, public setter
public string Title { set { title=value; }} // No getter, access...
public void Display() { Console.WriteLine(title); }//not Title, use lowercase member variable

它不起作用的原因是属性Price, PublisherName和Title没有getter,它们只有setter。也许他要求将这些属性设置为只读,而不是只读?